extents.c 113 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345
  1. /*
  2. * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
  3. * Written by Alex Tomas <alex@clusterfs.com>
  4. *
  5. * Architecture independence:
  6. * Copyright (c) 2005, Bull S.A.
  7. * Written by Pierre Peiffer <pierre.peiffer@bull.net>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public Licens
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
  21. */
  22. /*
  23. * Extents support for EXT4
  24. *
  25. * TODO:
  26. * - ext4*_error() should be used in some situations
  27. * - analyze all BUG()/BUG_ON(), use -EIO where appropriate
  28. * - smart tree reduction
  29. */
  30. #include <linux/module.h>
  31. #include <linux/fs.h>
  32. #include <linux/time.h>
  33. #include <linux/jbd2.h>
  34. #include <linux/highuid.h>
  35. #include <linux/pagemap.h>
  36. #include <linux/quotaops.h>
  37. #include <linux/string.h>
  38. #include <linux/slab.h>
  39. #include <linux/falloc.h>
  40. #include <asm/uaccess.h>
  41. #include <linux/fiemap.h>
  42. #include "ext4_jbd2.h"
  43. #include "ext4_extents.h"
  44. #include <trace/events/ext4.h>
  45. static int ext4_split_extent(handle_t *handle,
  46. struct inode *inode,
  47. struct ext4_ext_path *path,
  48. struct ext4_map_blocks *map,
  49. int split_flag,
  50. int flags);
  51. static int ext4_ext_truncate_extend_restart(handle_t *handle,
  52. struct inode *inode,
  53. int needed)
  54. {
  55. int err;
  56. if (!ext4_handle_valid(handle))
  57. return 0;
  58. if (handle->h_buffer_credits > needed)
  59. return 0;
  60. err = ext4_journal_extend(handle, needed);
  61. if (err <= 0)
  62. return err;
  63. err = ext4_truncate_restart_trans(handle, inode, needed);
  64. if (err == 0)
  65. err = -EAGAIN;
  66. return err;
  67. }
  68. /*
  69. * could return:
  70. * - EROFS
  71. * - ENOMEM
  72. */
  73. static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
  74. struct ext4_ext_path *path)
  75. {
  76. if (path->p_bh) {
  77. /* path points to block */
  78. return ext4_journal_get_write_access(handle, path->p_bh);
  79. }
  80. /* path points to leaf/index in inode body */
  81. /* we use in-core data, no need to protect them */
  82. return 0;
  83. }
  84. /*
  85. * could return:
  86. * - EROFS
  87. * - ENOMEM
  88. * - EIO
  89. */
  90. static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
  91. struct ext4_ext_path *path)
  92. {
  93. int err;
  94. if (path->p_bh) {
  95. /* path points to block */
  96. err = ext4_handle_dirty_metadata(handle, inode, path->p_bh);
  97. } else {
  98. /* path points to leaf/index in inode body */
  99. err = ext4_mark_inode_dirty(handle, inode);
  100. }
  101. return err;
  102. }
  103. static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
  104. struct ext4_ext_path *path,
  105. ext4_lblk_t block)
  106. {
  107. int depth;
  108. if (path) {
  109. struct ext4_extent *ex;
  110. depth = path->p_depth;
  111. /*
  112. * Try to predict block placement assuming that we are
  113. * filling in a file which will eventually be
  114. * non-sparse --- i.e., in the case of libbfd writing
  115. * an ELF object sections out-of-order but in a way
  116. * the eventually results in a contiguous object or
  117. * executable file, or some database extending a table
  118. * space file. However, this is actually somewhat
  119. * non-ideal if we are writing a sparse file such as
  120. * qemu or KVM writing a raw image file that is going
  121. * to stay fairly sparse, since it will end up
  122. * fragmenting the file system's free space. Maybe we
  123. * should have some hueristics or some way to allow
  124. * userspace to pass a hint to file system,
  125. * especially if the latter case turns out to be
  126. * common.
  127. */
  128. ex = path[depth].p_ext;
  129. if (ex) {
  130. ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
  131. ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
  132. if (block > ext_block)
  133. return ext_pblk + (block - ext_block);
  134. else
  135. return ext_pblk - (ext_block - block);
  136. }
  137. /* it looks like index is empty;
  138. * try to find starting block from index itself */
  139. if (path[depth].p_bh)
  140. return path[depth].p_bh->b_blocknr;
  141. }
  142. /* OK. use inode's group */
  143. return ext4_inode_to_goal_block(inode);
  144. }
  145. /*
  146. * Allocation for a meta data block
  147. */
  148. static ext4_fsblk_t
  149. ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
  150. struct ext4_ext_path *path,
  151. struct ext4_extent *ex, int *err, unsigned int flags)
  152. {
  153. ext4_fsblk_t goal, newblock;
  154. goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
  155. newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
  156. NULL, err);
  157. return newblock;
  158. }
  159. static inline int ext4_ext_space_block(struct inode *inode, int check)
  160. {
  161. int size;
  162. size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
  163. / sizeof(struct ext4_extent);
  164. if (!check) {
  165. #ifdef AGGRESSIVE_TEST
  166. if (size > 6)
  167. size = 6;
  168. #endif
  169. }
  170. return size;
  171. }
  172. static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
  173. {
  174. int size;
  175. size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
  176. / sizeof(struct ext4_extent_idx);
  177. if (!check) {
  178. #ifdef AGGRESSIVE_TEST
  179. if (size > 5)
  180. size = 5;
  181. #endif
  182. }
  183. return size;
  184. }
  185. static inline int ext4_ext_space_root(struct inode *inode, int check)
  186. {
  187. int size;
  188. size = sizeof(EXT4_I(inode)->i_data);
  189. size -= sizeof(struct ext4_extent_header);
  190. size /= sizeof(struct ext4_extent);
  191. if (!check) {
  192. #ifdef AGGRESSIVE_TEST
  193. if (size > 3)
  194. size = 3;
  195. #endif
  196. }
  197. return size;
  198. }
  199. static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
  200. {
  201. int size;
  202. size = sizeof(EXT4_I(inode)->i_data);
  203. size -= sizeof(struct ext4_extent_header);
  204. size /= sizeof(struct ext4_extent_idx);
  205. if (!check) {
  206. #ifdef AGGRESSIVE_TEST
  207. if (size > 4)
  208. size = 4;
  209. #endif
  210. }
  211. return size;
  212. }
  213. /*
  214. * Calculate the number of metadata blocks needed
  215. * to allocate @blocks
  216. * Worse case is one block per extent
  217. */
  218. int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
  219. {
  220. struct ext4_inode_info *ei = EXT4_I(inode);
  221. int idxs, num = 0;
  222. idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
  223. / sizeof(struct ext4_extent_idx));
  224. /*
  225. * If the new delayed allocation block is contiguous with the
  226. * previous da block, it can share index blocks with the
  227. * previous block, so we only need to allocate a new index
  228. * block every idxs leaf blocks. At ldxs**2 blocks, we need
  229. * an additional index block, and at ldxs**3 blocks, yet
  230. * another index blocks.
  231. */
  232. if (ei->i_da_metadata_calc_len &&
  233. ei->i_da_metadata_calc_last_lblock+1 == lblock) {
  234. if ((ei->i_da_metadata_calc_len % idxs) == 0)
  235. num++;
  236. if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
  237. num++;
  238. if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
  239. num++;
  240. ei->i_da_metadata_calc_len = 0;
  241. } else
  242. ei->i_da_metadata_calc_len++;
  243. ei->i_da_metadata_calc_last_lblock++;
  244. return num;
  245. }
  246. /*
  247. * In the worst case we need a new set of index blocks at
  248. * every level of the inode's extent tree.
  249. */
  250. ei->i_da_metadata_calc_len = 1;
  251. ei->i_da_metadata_calc_last_lblock = lblock;
  252. return ext_depth(inode) + 1;
  253. }
  254. static int
  255. ext4_ext_max_entries(struct inode *inode, int depth)
  256. {
  257. int max;
  258. if (depth == ext_depth(inode)) {
  259. if (depth == 0)
  260. max = ext4_ext_space_root(inode, 1);
  261. else
  262. max = ext4_ext_space_root_idx(inode, 1);
  263. } else {
  264. if (depth == 0)
  265. max = ext4_ext_space_block(inode, 1);
  266. else
  267. max = ext4_ext_space_block_idx(inode, 1);
  268. }
  269. return max;
  270. }
  271. static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
  272. {
  273. ext4_fsblk_t block = ext4_ext_pblock(ext);
  274. int len = ext4_ext_get_actual_len(ext);
  275. return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
  276. }
  277. static int ext4_valid_extent_idx(struct inode *inode,
  278. struct ext4_extent_idx *ext_idx)
  279. {
  280. ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
  281. return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
  282. }
  283. static int ext4_valid_extent_entries(struct inode *inode,
  284. struct ext4_extent_header *eh,
  285. int depth)
  286. {
  287. struct ext4_extent *ext;
  288. struct ext4_extent_idx *ext_idx;
  289. unsigned short entries;
  290. if (eh->eh_entries == 0)
  291. return 1;
  292. entries = le16_to_cpu(eh->eh_entries);
  293. if (depth == 0) {
  294. /* leaf entries */
  295. ext = EXT_FIRST_EXTENT(eh);
  296. while (entries) {
  297. if (!ext4_valid_extent(inode, ext))
  298. return 0;
  299. ext++;
  300. entries--;
  301. }
  302. } else {
  303. ext_idx = EXT_FIRST_INDEX(eh);
  304. while (entries) {
  305. if (!ext4_valid_extent_idx(inode, ext_idx))
  306. return 0;
  307. ext_idx++;
  308. entries--;
  309. }
  310. }
  311. return 1;
  312. }
  313. static int __ext4_ext_check(const char *function, unsigned int line,
  314. struct inode *inode, struct ext4_extent_header *eh,
  315. int depth)
  316. {
  317. const char *error_msg;
  318. int max = 0;
  319. if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
  320. error_msg = "invalid magic";
  321. goto corrupted;
  322. }
  323. if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
  324. error_msg = "unexpected eh_depth";
  325. goto corrupted;
  326. }
  327. if (unlikely(eh->eh_max == 0)) {
  328. error_msg = "invalid eh_max";
  329. goto corrupted;
  330. }
  331. max = ext4_ext_max_entries(inode, depth);
  332. if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
  333. error_msg = "too large eh_max";
  334. goto corrupted;
  335. }
  336. if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
  337. error_msg = "invalid eh_entries";
  338. goto corrupted;
  339. }
  340. if (!ext4_valid_extent_entries(inode, eh, depth)) {
  341. error_msg = "invalid extent entries";
  342. goto corrupted;
  343. }
  344. return 0;
  345. corrupted:
  346. ext4_error_inode(inode, function, line, 0,
  347. "bad header/extent: %s - magic %x, "
  348. "entries %u, max %u(%u), depth %u(%u)",
  349. error_msg, le16_to_cpu(eh->eh_magic),
  350. le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
  351. max, le16_to_cpu(eh->eh_depth), depth);
  352. return -EIO;
  353. }
  354. #define ext4_ext_check(inode, eh, depth) \
  355. __ext4_ext_check(__func__, __LINE__, inode, eh, depth)
  356. int ext4_ext_check_inode(struct inode *inode)
  357. {
  358. return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode));
  359. }
  360. #ifdef EXT_DEBUG
  361. static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
  362. {
  363. int k, l = path->p_depth;
  364. ext_debug("path:");
  365. for (k = 0; k <= l; k++, path++) {
  366. if (path->p_idx) {
  367. ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
  368. ext4_idx_pblock(path->p_idx));
  369. } else if (path->p_ext) {
  370. ext_debug(" %d:[%d]%d:%llu ",
  371. le32_to_cpu(path->p_ext->ee_block),
  372. ext4_ext_is_uninitialized(path->p_ext),
  373. ext4_ext_get_actual_len(path->p_ext),
  374. ext4_ext_pblock(path->p_ext));
  375. } else
  376. ext_debug(" []");
  377. }
  378. ext_debug("\n");
  379. }
  380. static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
  381. {
  382. int depth = ext_depth(inode);
  383. struct ext4_extent_header *eh;
  384. struct ext4_extent *ex;
  385. int i;
  386. if (!path)
  387. return;
  388. eh = path[depth].p_hdr;
  389. ex = EXT_FIRST_EXTENT(eh);
  390. ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
  391. for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
  392. ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
  393. ext4_ext_is_uninitialized(ex),
  394. ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
  395. }
  396. ext_debug("\n");
  397. }
  398. static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
  399. ext4_fsblk_t newblock, int level)
  400. {
  401. int depth = ext_depth(inode);
  402. struct ext4_extent *ex;
  403. if (depth != level) {
  404. struct ext4_extent_idx *idx;
  405. idx = path[level].p_idx;
  406. while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
  407. ext_debug("%d: move %d:%llu in new index %llu\n", level,
  408. le32_to_cpu(idx->ei_block),
  409. ext4_idx_pblock(idx),
  410. newblock);
  411. idx++;
  412. }
  413. return;
  414. }
  415. ex = path[depth].p_ext;
  416. while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
  417. ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
  418. le32_to_cpu(ex->ee_block),
  419. ext4_ext_pblock(ex),
  420. ext4_ext_is_uninitialized(ex),
  421. ext4_ext_get_actual_len(ex),
  422. newblock);
  423. ex++;
  424. }
  425. }
  426. #else
  427. #define ext4_ext_show_path(inode, path)
  428. #define ext4_ext_show_leaf(inode, path)
  429. #define ext4_ext_show_move(inode, path, newblock, level)
  430. #endif
  431. void ext4_ext_drop_refs(struct ext4_ext_path *path)
  432. {
  433. int depth = path->p_depth;
  434. int i;
  435. for (i = 0; i <= depth; i++, path++)
  436. if (path->p_bh) {
  437. brelse(path->p_bh);
  438. path->p_bh = NULL;
  439. }
  440. }
  441. /*
  442. * ext4_ext_binsearch_idx:
  443. * binary search for the closest index of the given block
  444. * the header must be checked before calling this
  445. */
  446. static void
  447. ext4_ext_binsearch_idx(struct inode *inode,
  448. struct ext4_ext_path *path, ext4_lblk_t block)
  449. {
  450. struct ext4_extent_header *eh = path->p_hdr;
  451. struct ext4_extent_idx *r, *l, *m;
  452. ext_debug("binsearch for %u(idx): ", block);
  453. l = EXT_FIRST_INDEX(eh) + 1;
  454. r = EXT_LAST_INDEX(eh);
  455. while (l <= r) {
  456. m = l + (r - l) / 2;
  457. if (block < le32_to_cpu(m->ei_block))
  458. r = m - 1;
  459. else
  460. l = m + 1;
  461. ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
  462. m, le32_to_cpu(m->ei_block),
  463. r, le32_to_cpu(r->ei_block));
  464. }
  465. path->p_idx = l - 1;
  466. ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
  467. ext4_idx_pblock(path->p_idx));
  468. #ifdef CHECK_BINSEARCH
  469. {
  470. struct ext4_extent_idx *chix, *ix;
  471. int k;
  472. chix = ix = EXT_FIRST_INDEX(eh);
  473. for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
  474. if (k != 0 &&
  475. le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
  476. printk(KERN_DEBUG "k=%d, ix=0x%p, "
  477. "first=0x%p\n", k,
  478. ix, EXT_FIRST_INDEX(eh));
  479. printk(KERN_DEBUG "%u <= %u\n",
  480. le32_to_cpu(ix->ei_block),
  481. le32_to_cpu(ix[-1].ei_block));
  482. }
  483. BUG_ON(k && le32_to_cpu(ix->ei_block)
  484. <= le32_to_cpu(ix[-1].ei_block));
  485. if (block < le32_to_cpu(ix->ei_block))
  486. break;
  487. chix = ix;
  488. }
  489. BUG_ON(chix != path->p_idx);
  490. }
  491. #endif
  492. }
  493. /*
  494. * ext4_ext_binsearch:
  495. * binary search for closest extent of the given block
  496. * the header must be checked before calling this
  497. */
  498. static void
  499. ext4_ext_binsearch(struct inode *inode,
  500. struct ext4_ext_path *path, ext4_lblk_t block)
  501. {
  502. struct ext4_extent_header *eh = path->p_hdr;
  503. struct ext4_extent *r, *l, *m;
  504. if (eh->eh_entries == 0) {
  505. /*
  506. * this leaf is empty:
  507. * we get such a leaf in split/add case
  508. */
  509. return;
  510. }
  511. ext_debug("binsearch for %u: ", block);
  512. l = EXT_FIRST_EXTENT(eh) + 1;
  513. r = EXT_LAST_EXTENT(eh);
  514. while (l <= r) {
  515. m = l + (r - l) / 2;
  516. if (block < le32_to_cpu(m->ee_block))
  517. r = m - 1;
  518. else
  519. l = m + 1;
  520. ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
  521. m, le32_to_cpu(m->ee_block),
  522. r, le32_to_cpu(r->ee_block));
  523. }
  524. path->p_ext = l - 1;
  525. ext_debug(" -> %d:%llu:[%d]%d ",
  526. le32_to_cpu(path->p_ext->ee_block),
  527. ext4_ext_pblock(path->p_ext),
  528. ext4_ext_is_uninitialized(path->p_ext),
  529. ext4_ext_get_actual_len(path->p_ext));
  530. #ifdef CHECK_BINSEARCH
  531. {
  532. struct ext4_extent *chex, *ex;
  533. int k;
  534. chex = ex = EXT_FIRST_EXTENT(eh);
  535. for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
  536. BUG_ON(k && le32_to_cpu(ex->ee_block)
  537. <= le32_to_cpu(ex[-1].ee_block));
  538. if (block < le32_to_cpu(ex->ee_block))
  539. break;
  540. chex = ex;
  541. }
  542. BUG_ON(chex != path->p_ext);
  543. }
  544. #endif
  545. }
  546. int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
  547. {
  548. struct ext4_extent_header *eh;
  549. eh = ext_inode_hdr(inode);
  550. eh->eh_depth = 0;
  551. eh->eh_entries = 0;
  552. eh->eh_magic = EXT4_EXT_MAGIC;
  553. eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
  554. ext4_mark_inode_dirty(handle, inode);
  555. ext4_ext_invalidate_cache(inode);
  556. return 0;
  557. }
  558. struct ext4_ext_path *
  559. ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
  560. struct ext4_ext_path *path)
  561. {
  562. struct ext4_extent_header *eh;
  563. struct buffer_head *bh;
  564. short int depth, i, ppos = 0, alloc = 0;
  565. eh = ext_inode_hdr(inode);
  566. depth = ext_depth(inode);
  567. /* account possible depth increase */
  568. if (!path) {
  569. path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
  570. GFP_NOFS);
  571. if (!path)
  572. return ERR_PTR(-ENOMEM);
  573. alloc = 1;
  574. }
  575. path[0].p_hdr = eh;
  576. path[0].p_bh = NULL;
  577. i = depth;
  578. /* walk through the tree */
  579. while (i) {
  580. int need_to_validate = 0;
  581. ext_debug("depth %d: num %d, max %d\n",
  582. ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
  583. ext4_ext_binsearch_idx(inode, path + ppos, block);
  584. path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
  585. path[ppos].p_depth = i;
  586. path[ppos].p_ext = NULL;
  587. bh = sb_getblk(inode->i_sb, path[ppos].p_block);
  588. if (unlikely(!bh))
  589. goto err;
  590. if (!bh_uptodate_or_lock(bh)) {
  591. trace_ext4_ext_load_extent(inode, block,
  592. path[ppos].p_block);
  593. if (bh_submit_read(bh) < 0) {
  594. put_bh(bh);
  595. goto err;
  596. }
  597. /* validate the extent entries */
  598. need_to_validate = 1;
  599. }
  600. eh = ext_block_hdr(bh);
  601. ppos++;
  602. if (unlikely(ppos > depth)) {
  603. put_bh(bh);
  604. EXT4_ERROR_INODE(inode,
  605. "ppos %d > depth %d", ppos, depth);
  606. goto err;
  607. }
  608. path[ppos].p_bh = bh;
  609. path[ppos].p_hdr = eh;
  610. i--;
  611. if (need_to_validate && ext4_ext_check(inode, eh, i))
  612. goto err;
  613. }
  614. path[ppos].p_depth = i;
  615. path[ppos].p_ext = NULL;
  616. path[ppos].p_idx = NULL;
  617. /* find extent */
  618. ext4_ext_binsearch(inode, path + ppos, block);
  619. /* if not an empty leaf */
  620. if (path[ppos].p_ext)
  621. path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
  622. ext4_ext_show_path(inode, path);
  623. return path;
  624. err:
  625. ext4_ext_drop_refs(path);
  626. if (alloc)
  627. kfree(path);
  628. return ERR_PTR(-EIO);
  629. }
  630. /*
  631. * ext4_ext_insert_index:
  632. * insert new index [@logical;@ptr] into the block at @curp;
  633. * check where to insert: before @curp or after @curp
  634. */
  635. static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
  636. struct ext4_ext_path *curp,
  637. int logical, ext4_fsblk_t ptr)
  638. {
  639. struct ext4_extent_idx *ix;
  640. int len, err;
  641. err = ext4_ext_get_access(handle, inode, curp);
  642. if (err)
  643. return err;
  644. if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
  645. EXT4_ERROR_INODE(inode,
  646. "logical %d == ei_block %d!",
  647. logical, le32_to_cpu(curp->p_idx->ei_block));
  648. return -EIO;
  649. }
  650. if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
  651. >= le16_to_cpu(curp->p_hdr->eh_max))) {
  652. EXT4_ERROR_INODE(inode,
  653. "eh_entries %d >= eh_max %d!",
  654. le16_to_cpu(curp->p_hdr->eh_entries),
  655. le16_to_cpu(curp->p_hdr->eh_max));
  656. return -EIO;
  657. }
  658. len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
  659. if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
  660. /* insert after */
  661. if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
  662. len = (len - 1) * sizeof(struct ext4_extent_idx);
  663. len = len < 0 ? 0 : len;
  664. ext_debug("insert new index %d after: %llu. "
  665. "move %d from 0x%p to 0x%p\n",
  666. logical, ptr, len,
  667. (curp->p_idx + 1), (curp->p_idx + 2));
  668. memmove(curp->p_idx + 2, curp->p_idx + 1, len);
  669. }
  670. ix = curp->p_idx + 1;
  671. } else {
  672. /* insert before */
  673. len = len * sizeof(struct ext4_extent_idx);
  674. len = len < 0 ? 0 : len;
  675. ext_debug("insert new index %d before: %llu. "
  676. "move %d from 0x%p to 0x%p\n",
  677. logical, ptr, len,
  678. curp->p_idx, (curp->p_idx + 1));
  679. memmove(curp->p_idx + 1, curp->p_idx, len);
  680. ix = curp->p_idx;
  681. }
  682. ix->ei_block = cpu_to_le32(logical);
  683. ext4_idx_store_pblock(ix, ptr);
  684. le16_add_cpu(&curp->p_hdr->eh_entries, 1);
  685. if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
  686. EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
  687. return -EIO;
  688. }
  689. err = ext4_ext_dirty(handle, inode, curp);
  690. ext4_std_error(inode->i_sb, err);
  691. return err;
  692. }
  693. /*
  694. * ext4_ext_split:
  695. * inserts new subtree into the path, using free index entry
  696. * at depth @at:
  697. * - allocates all needed blocks (new leaf and all intermediate index blocks)
  698. * - makes decision where to split
  699. * - moves remaining extents and index entries (right to the split point)
  700. * into the newly allocated blocks
  701. * - initializes subtree
  702. */
  703. static int ext4_ext_split(handle_t *handle, struct inode *inode,
  704. unsigned int flags,
  705. struct ext4_ext_path *path,
  706. struct ext4_extent *newext, int at)
  707. {
  708. struct buffer_head *bh = NULL;
  709. int depth = ext_depth(inode);
  710. struct ext4_extent_header *neh;
  711. struct ext4_extent_idx *fidx;
  712. int i = at, k, m, a;
  713. ext4_fsblk_t newblock, oldblock;
  714. __le32 border;
  715. ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
  716. int err = 0;
  717. /* make decision: where to split? */
  718. /* FIXME: now decision is simplest: at current extent */
  719. /* if current leaf will be split, then we should use
  720. * border from split point */
  721. if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
  722. EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
  723. return -EIO;
  724. }
  725. if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
  726. border = path[depth].p_ext[1].ee_block;
  727. ext_debug("leaf will be split."
  728. " next leaf starts at %d\n",
  729. le32_to_cpu(border));
  730. } else {
  731. border = newext->ee_block;
  732. ext_debug("leaf will be added."
  733. " next leaf starts at %d\n",
  734. le32_to_cpu(border));
  735. }
  736. /*
  737. * If error occurs, then we break processing
  738. * and mark filesystem read-only. index won't
  739. * be inserted and tree will be in consistent
  740. * state. Next mount will repair buffers too.
  741. */
  742. /*
  743. * Get array to track all allocated blocks.
  744. * We need this to handle errors and free blocks
  745. * upon them.
  746. */
  747. ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
  748. if (!ablocks)
  749. return -ENOMEM;
  750. /* allocate all needed blocks */
  751. ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
  752. for (a = 0; a < depth - at; a++) {
  753. newblock = ext4_ext_new_meta_block(handle, inode, path,
  754. newext, &err, flags);
  755. if (newblock == 0)
  756. goto cleanup;
  757. ablocks[a] = newblock;
  758. }
  759. /* initialize new leaf */
  760. newblock = ablocks[--a];
  761. if (unlikely(newblock == 0)) {
  762. EXT4_ERROR_INODE(inode, "newblock == 0!");
  763. err = -EIO;
  764. goto cleanup;
  765. }
  766. bh = sb_getblk(inode->i_sb, newblock);
  767. if (!bh) {
  768. err = -EIO;
  769. goto cleanup;
  770. }
  771. lock_buffer(bh);
  772. err = ext4_journal_get_create_access(handle, bh);
  773. if (err)
  774. goto cleanup;
  775. neh = ext_block_hdr(bh);
  776. neh->eh_entries = 0;
  777. neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
  778. neh->eh_magic = EXT4_EXT_MAGIC;
  779. neh->eh_depth = 0;
  780. /* move remainder of path[depth] to the new leaf */
  781. if (unlikely(path[depth].p_hdr->eh_entries !=
  782. path[depth].p_hdr->eh_max)) {
  783. EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
  784. path[depth].p_hdr->eh_entries,
  785. path[depth].p_hdr->eh_max);
  786. err = -EIO;
  787. goto cleanup;
  788. }
  789. /* start copy from next extent */
  790. m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
  791. ext4_ext_show_move(inode, path, newblock, depth);
  792. if (m) {
  793. struct ext4_extent *ex;
  794. ex = EXT_FIRST_EXTENT(neh);
  795. memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
  796. le16_add_cpu(&neh->eh_entries, m);
  797. }
  798. set_buffer_uptodate(bh);
  799. unlock_buffer(bh);
  800. err = ext4_handle_dirty_metadata(handle, inode, bh);
  801. if (err)
  802. goto cleanup;
  803. brelse(bh);
  804. bh = NULL;
  805. /* correct old leaf */
  806. if (m) {
  807. err = ext4_ext_get_access(handle, inode, path + depth);
  808. if (err)
  809. goto cleanup;
  810. le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
  811. err = ext4_ext_dirty(handle, inode, path + depth);
  812. if (err)
  813. goto cleanup;
  814. }
  815. /* create intermediate indexes */
  816. k = depth - at - 1;
  817. if (unlikely(k < 0)) {
  818. EXT4_ERROR_INODE(inode, "k %d < 0!", k);
  819. err = -EIO;
  820. goto cleanup;
  821. }
  822. if (k)
  823. ext_debug("create %d intermediate indices\n", k);
  824. /* insert new index into current index block */
  825. /* current depth stored in i var */
  826. i = depth - 1;
  827. while (k--) {
  828. oldblock = newblock;
  829. newblock = ablocks[--a];
  830. bh = sb_getblk(inode->i_sb, newblock);
  831. if (!bh) {
  832. err = -EIO;
  833. goto cleanup;
  834. }
  835. lock_buffer(bh);
  836. err = ext4_journal_get_create_access(handle, bh);
  837. if (err)
  838. goto cleanup;
  839. neh = ext_block_hdr(bh);
  840. neh->eh_entries = cpu_to_le16(1);
  841. neh->eh_magic = EXT4_EXT_MAGIC;
  842. neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
  843. neh->eh_depth = cpu_to_le16(depth - i);
  844. fidx = EXT_FIRST_INDEX(neh);
  845. fidx->ei_block = border;
  846. ext4_idx_store_pblock(fidx, oldblock);
  847. ext_debug("int.index at %d (block %llu): %u -> %llu\n",
  848. i, newblock, le32_to_cpu(border), oldblock);
  849. /* move remainder of path[i] to the new index block */
  850. if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
  851. EXT_LAST_INDEX(path[i].p_hdr))) {
  852. EXT4_ERROR_INODE(inode,
  853. "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
  854. le32_to_cpu(path[i].p_ext->ee_block));
  855. err = -EIO;
  856. goto cleanup;
  857. }
  858. /* start copy indexes */
  859. m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
  860. ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
  861. EXT_MAX_INDEX(path[i].p_hdr));
  862. ext4_ext_show_move(inode, path, newblock, i);
  863. if (m) {
  864. memmove(++fidx, path[i].p_idx,
  865. sizeof(struct ext4_extent_idx) * m);
  866. le16_add_cpu(&neh->eh_entries, m);
  867. }
  868. set_buffer_uptodate(bh);
  869. unlock_buffer(bh);
  870. err = ext4_handle_dirty_metadata(handle, inode, bh);
  871. if (err)
  872. goto cleanup;
  873. brelse(bh);
  874. bh = NULL;
  875. /* correct old index */
  876. if (m) {
  877. err = ext4_ext_get_access(handle, inode, path + i);
  878. if (err)
  879. goto cleanup;
  880. le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
  881. err = ext4_ext_dirty(handle, inode, path + i);
  882. if (err)
  883. goto cleanup;
  884. }
  885. i--;
  886. }
  887. /* insert new index */
  888. err = ext4_ext_insert_index(handle, inode, path + at,
  889. le32_to_cpu(border), newblock);
  890. cleanup:
  891. if (bh) {
  892. if (buffer_locked(bh))
  893. unlock_buffer(bh);
  894. brelse(bh);
  895. }
  896. if (err) {
  897. /* free all allocated blocks in error case */
  898. for (i = 0; i < depth; i++) {
  899. if (!ablocks[i])
  900. continue;
  901. ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
  902. EXT4_FREE_BLOCKS_METADATA);
  903. }
  904. }
  905. kfree(ablocks);
  906. return err;
  907. }
  908. /*
  909. * ext4_ext_grow_indepth:
  910. * implements tree growing procedure:
  911. * - allocates new block
  912. * - moves top-level data (index block or leaf) into the new block
  913. * - initializes new top-level, creating index that points to the
  914. * just created block
  915. */
  916. static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
  917. unsigned int flags,
  918. struct ext4_ext_path *path,
  919. struct ext4_extent *newext)
  920. {
  921. struct ext4_ext_path *curp = path;
  922. struct ext4_extent_header *neh;
  923. struct buffer_head *bh;
  924. ext4_fsblk_t newblock;
  925. int err = 0;
  926. newblock = ext4_ext_new_meta_block(handle, inode, path,
  927. newext, &err, flags);
  928. if (newblock == 0)
  929. return err;
  930. bh = sb_getblk(inode->i_sb, newblock);
  931. if (!bh) {
  932. err = -EIO;
  933. ext4_std_error(inode->i_sb, err);
  934. return err;
  935. }
  936. lock_buffer(bh);
  937. err = ext4_journal_get_create_access(handle, bh);
  938. if (err) {
  939. unlock_buffer(bh);
  940. goto out;
  941. }
  942. /* move top-level index/leaf into new block */
  943. memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
  944. /* set size of new block */
  945. neh = ext_block_hdr(bh);
  946. /* old root could have indexes or leaves
  947. * so calculate e_max right way */
  948. if (ext_depth(inode))
  949. neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
  950. else
  951. neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
  952. neh->eh_magic = EXT4_EXT_MAGIC;
  953. set_buffer_uptodate(bh);
  954. unlock_buffer(bh);
  955. err = ext4_handle_dirty_metadata(handle, inode, bh);
  956. if (err)
  957. goto out;
  958. /* create index in new top-level index: num,max,pointer */
  959. err = ext4_ext_get_access(handle, inode, curp);
  960. if (err)
  961. goto out;
  962. curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
  963. curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
  964. curp->p_hdr->eh_entries = cpu_to_le16(1);
  965. curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
  966. if (path[0].p_hdr->eh_depth)
  967. curp->p_idx->ei_block =
  968. EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
  969. else
  970. curp->p_idx->ei_block =
  971. EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
  972. ext4_idx_store_pblock(curp->p_idx, newblock);
  973. neh = ext_inode_hdr(inode);
  974. ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
  975. le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
  976. le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
  977. ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
  978. neh->eh_depth = cpu_to_le16(path->p_depth + 1);
  979. err = ext4_ext_dirty(handle, inode, curp);
  980. out:
  981. brelse(bh);
  982. return err;
  983. }
  984. /*
  985. * ext4_ext_create_new_leaf:
  986. * finds empty index and adds new leaf.
  987. * if no free index is found, then it requests in-depth growing.
  988. */
  989. static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
  990. unsigned int flags,
  991. struct ext4_ext_path *path,
  992. struct ext4_extent *newext)
  993. {
  994. struct ext4_ext_path *curp;
  995. int depth, i, err = 0;
  996. repeat:
  997. i = depth = ext_depth(inode);
  998. /* walk up to the tree and look for free index entry */
  999. curp = path + depth;
  1000. while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
  1001. i--;
  1002. curp--;
  1003. }
  1004. /* we use already allocated block for index block,
  1005. * so subsequent data blocks should be contiguous */
  1006. if (EXT_HAS_FREE_INDEX(curp)) {
  1007. /* if we found index with free entry, then use that
  1008. * entry: create all needed subtree and add new leaf */
  1009. err = ext4_ext_split(handle, inode, flags, path, newext, i);
  1010. if (err)
  1011. goto out;
  1012. /* refill path */
  1013. ext4_ext_drop_refs(path);
  1014. path = ext4_ext_find_extent(inode,
  1015. (ext4_lblk_t)le32_to_cpu(newext->ee_block),
  1016. path);
  1017. if (IS_ERR(path))
  1018. err = PTR_ERR(path);
  1019. } else {
  1020. /* tree is full, time to grow in depth */
  1021. err = ext4_ext_grow_indepth(handle, inode, flags,
  1022. path, newext);
  1023. if (err)
  1024. goto out;
  1025. /* refill path */
  1026. ext4_ext_drop_refs(path);
  1027. path = ext4_ext_find_extent(inode,
  1028. (ext4_lblk_t)le32_to_cpu(newext->ee_block),
  1029. path);
  1030. if (IS_ERR(path)) {
  1031. err = PTR_ERR(path);
  1032. goto out;
  1033. }
  1034. /*
  1035. * only first (depth 0 -> 1) produces free space;
  1036. * in all other cases we have to split the grown tree
  1037. */
  1038. depth = ext_depth(inode);
  1039. if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
  1040. /* now we need to split */
  1041. goto repeat;
  1042. }
  1043. }
  1044. out:
  1045. return err;
  1046. }
  1047. /*
  1048. * search the closest allocated block to the left for *logical
  1049. * and returns it at @logical + it's physical address at @phys
  1050. * if *logical is the smallest allocated block, the function
  1051. * returns 0 at @phys
  1052. * return value contains 0 (success) or error code
  1053. */
  1054. static int ext4_ext_search_left(struct inode *inode,
  1055. struct ext4_ext_path *path,
  1056. ext4_lblk_t *logical, ext4_fsblk_t *phys)
  1057. {
  1058. struct ext4_extent_idx *ix;
  1059. struct ext4_extent *ex;
  1060. int depth, ee_len;
  1061. if (unlikely(path == NULL)) {
  1062. EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
  1063. return -EIO;
  1064. }
  1065. depth = path->p_depth;
  1066. *phys = 0;
  1067. if (depth == 0 && path->p_ext == NULL)
  1068. return 0;
  1069. /* usually extent in the path covers blocks smaller
  1070. * then *logical, but it can be that extent is the
  1071. * first one in the file */
  1072. ex = path[depth].p_ext;
  1073. ee_len = ext4_ext_get_actual_len(ex);
  1074. if (*logical < le32_to_cpu(ex->ee_block)) {
  1075. if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
  1076. EXT4_ERROR_INODE(inode,
  1077. "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
  1078. *logical, le32_to_cpu(ex->ee_block));
  1079. return -EIO;
  1080. }
  1081. while (--depth >= 0) {
  1082. ix = path[depth].p_idx;
  1083. if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
  1084. EXT4_ERROR_INODE(inode,
  1085. "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
  1086. ix != NULL ? ix->ei_block : 0,
  1087. EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
  1088. EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block : 0,
  1089. depth);
  1090. return -EIO;
  1091. }
  1092. }
  1093. return 0;
  1094. }
  1095. if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
  1096. EXT4_ERROR_INODE(inode,
  1097. "logical %d < ee_block %d + ee_len %d!",
  1098. *logical, le32_to_cpu(ex->ee_block), ee_len);
  1099. return -EIO;
  1100. }
  1101. *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
  1102. *phys = ext4_ext_pblock(ex) + ee_len - 1;
  1103. return 0;
  1104. }
  1105. /*
  1106. * search the closest allocated block to the right for *logical
  1107. * and returns it at @logical + it's physical address at @phys
  1108. * if *logical is the smallest allocated block, the function
  1109. * returns 0 at @phys
  1110. * return value contains 0 (success) or error code
  1111. */
  1112. static int ext4_ext_search_right(struct inode *inode,
  1113. struct ext4_ext_path *path,
  1114. ext4_lblk_t *logical, ext4_fsblk_t *phys)
  1115. {
  1116. struct buffer_head *bh = NULL;
  1117. struct ext4_extent_header *eh;
  1118. struct ext4_extent_idx *ix;
  1119. struct ext4_extent *ex;
  1120. ext4_fsblk_t block;
  1121. int depth; /* Note, NOT eh_depth; depth from top of tree */
  1122. int ee_len;
  1123. if (unlikely(path == NULL)) {
  1124. EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
  1125. return -EIO;
  1126. }
  1127. depth = path->p_depth;
  1128. *phys = 0;
  1129. if (depth == 0 && path->p_ext == NULL)
  1130. return 0;
  1131. /* usually extent in the path covers blocks smaller
  1132. * then *logical, but it can be that extent is the
  1133. * first one in the file */
  1134. ex = path[depth].p_ext;
  1135. ee_len = ext4_ext_get_actual_len(ex);
  1136. if (*logical < le32_to_cpu(ex->ee_block)) {
  1137. if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
  1138. EXT4_ERROR_INODE(inode,
  1139. "first_extent(path[%d].p_hdr) != ex",
  1140. depth);
  1141. return -EIO;
  1142. }
  1143. while (--depth >= 0) {
  1144. ix = path[depth].p_idx;
  1145. if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
  1146. EXT4_ERROR_INODE(inode,
  1147. "ix != EXT_FIRST_INDEX *logical %d!",
  1148. *logical);
  1149. return -EIO;
  1150. }
  1151. }
  1152. *logical = le32_to_cpu(ex->ee_block);
  1153. *phys = ext4_ext_pblock(ex);
  1154. return 0;
  1155. }
  1156. if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
  1157. EXT4_ERROR_INODE(inode,
  1158. "logical %d < ee_block %d + ee_len %d!",
  1159. *logical, le32_to_cpu(ex->ee_block), ee_len);
  1160. return -EIO;
  1161. }
  1162. if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
  1163. /* next allocated block in this leaf */
  1164. ex++;
  1165. *logical = le32_to_cpu(ex->ee_block);
  1166. *phys = ext4_ext_pblock(ex);
  1167. return 0;
  1168. }
  1169. /* go up and search for index to the right */
  1170. while (--depth >= 0) {
  1171. ix = path[depth].p_idx;
  1172. if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
  1173. goto got_index;
  1174. }
  1175. /* we've gone up to the root and found no index to the right */
  1176. return 0;
  1177. got_index:
  1178. /* we've found index to the right, let's
  1179. * follow it and find the closest allocated
  1180. * block to the right */
  1181. ix++;
  1182. block = ext4_idx_pblock(ix);
  1183. while (++depth < path->p_depth) {
  1184. bh = sb_bread(inode->i_sb, block);
  1185. if (bh == NULL)
  1186. return -EIO;
  1187. eh = ext_block_hdr(bh);
  1188. /* subtract from p_depth to get proper eh_depth */
  1189. if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
  1190. put_bh(bh);
  1191. return -EIO;
  1192. }
  1193. ix = EXT_FIRST_INDEX(eh);
  1194. block = ext4_idx_pblock(ix);
  1195. put_bh(bh);
  1196. }
  1197. bh = sb_bread(inode->i_sb, block);
  1198. if (bh == NULL)
  1199. return -EIO;
  1200. eh = ext_block_hdr(bh);
  1201. if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
  1202. put_bh(bh);
  1203. return -EIO;
  1204. }
  1205. ex = EXT_FIRST_EXTENT(eh);
  1206. *logical = le32_to_cpu(ex->ee_block);
  1207. *phys = ext4_ext_pblock(ex);
  1208. put_bh(bh);
  1209. return 0;
  1210. }
  1211. /*
  1212. * ext4_ext_next_allocated_block:
  1213. * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
  1214. * NOTE: it considers block number from index entry as
  1215. * allocated block. Thus, index entries have to be consistent
  1216. * with leaves.
  1217. */
  1218. static ext4_lblk_t
  1219. ext4_ext_next_allocated_block(struct ext4_ext_path *path)
  1220. {
  1221. int depth;
  1222. BUG_ON(path == NULL);
  1223. depth = path->p_depth;
  1224. if (depth == 0 && path->p_ext == NULL)
  1225. return EXT_MAX_BLOCKS;
  1226. while (depth >= 0) {
  1227. if (depth == path->p_depth) {
  1228. /* leaf */
  1229. if (path[depth].p_ext !=
  1230. EXT_LAST_EXTENT(path[depth].p_hdr))
  1231. return le32_to_cpu(path[depth].p_ext[1].ee_block);
  1232. } else {
  1233. /* index */
  1234. if (path[depth].p_idx !=
  1235. EXT_LAST_INDEX(path[depth].p_hdr))
  1236. return le32_to_cpu(path[depth].p_idx[1].ei_block);
  1237. }
  1238. depth--;
  1239. }
  1240. return EXT_MAX_BLOCKS;
  1241. }
  1242. /*
  1243. * ext4_ext_next_leaf_block:
  1244. * returns first allocated block from next leaf or EXT_MAX_BLOCKS
  1245. */
  1246. static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
  1247. {
  1248. int depth;
  1249. BUG_ON(path == NULL);
  1250. depth = path->p_depth;
  1251. /* zero-tree has no leaf blocks at all */
  1252. if (depth == 0)
  1253. return EXT_MAX_BLOCKS;
  1254. /* go to index block */
  1255. depth--;
  1256. while (depth >= 0) {
  1257. if (path[depth].p_idx !=
  1258. EXT_LAST_INDEX(path[depth].p_hdr))
  1259. return (ext4_lblk_t)
  1260. le32_to_cpu(path[depth].p_idx[1].ei_block);
  1261. depth--;
  1262. }
  1263. return EXT_MAX_BLOCKS;
  1264. }
  1265. /*
  1266. * ext4_ext_correct_indexes:
  1267. * if leaf gets modified and modified extent is first in the leaf,
  1268. * then we have to correct all indexes above.
  1269. * TODO: do we need to correct tree in all cases?
  1270. */
  1271. static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
  1272. struct ext4_ext_path *path)
  1273. {
  1274. struct ext4_extent_header *eh;
  1275. int depth = ext_depth(inode);
  1276. struct ext4_extent *ex;
  1277. __le32 border;
  1278. int k, err = 0;
  1279. eh = path[depth].p_hdr;
  1280. ex = path[depth].p_ext;
  1281. if (unlikely(ex == NULL || eh == NULL)) {
  1282. EXT4_ERROR_INODE(inode,
  1283. "ex %p == NULL or eh %p == NULL", ex, eh);
  1284. return -EIO;
  1285. }
  1286. if (depth == 0) {
  1287. /* there is no tree at all */
  1288. return 0;
  1289. }
  1290. if (ex != EXT_FIRST_EXTENT(eh)) {
  1291. /* we correct tree if first leaf got modified only */
  1292. return 0;
  1293. }
  1294. /*
  1295. * TODO: we need correction if border is smaller than current one
  1296. */
  1297. k = depth - 1;
  1298. border = path[depth].p_ext->ee_block;
  1299. err = ext4_ext_get_access(handle, inode, path + k);
  1300. if (err)
  1301. return err;
  1302. path[k].p_idx->ei_block = border;
  1303. err = ext4_ext_dirty(handle, inode, path + k);
  1304. if (err)
  1305. return err;
  1306. while (k--) {
  1307. /* change all left-side indexes */
  1308. if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
  1309. break;
  1310. err = ext4_ext_get_access(handle, inode, path + k);
  1311. if (err)
  1312. break;
  1313. path[k].p_idx->ei_block = border;
  1314. err = ext4_ext_dirty(handle, inode, path + k);
  1315. if (err)
  1316. break;
  1317. }
  1318. return err;
  1319. }
  1320. int
  1321. ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
  1322. struct ext4_extent *ex2)
  1323. {
  1324. unsigned short ext1_ee_len, ext2_ee_len, max_len;
  1325. /*
  1326. * Make sure that either both extents are uninitialized, or
  1327. * both are _not_.
  1328. */
  1329. if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
  1330. return 0;
  1331. if (ext4_ext_is_uninitialized(ex1))
  1332. max_len = EXT_UNINIT_MAX_LEN;
  1333. else
  1334. max_len = EXT_INIT_MAX_LEN;
  1335. ext1_ee_len = ext4_ext_get_actual_len(ex1);
  1336. ext2_ee_len = ext4_ext_get_actual_len(ex2);
  1337. if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
  1338. le32_to_cpu(ex2->ee_block))
  1339. return 0;
  1340. /*
  1341. * To allow future support for preallocated extents to be added
  1342. * as an RO_COMPAT feature, refuse to merge to extents if
  1343. * this can result in the top bit of ee_len being set.
  1344. */
  1345. if (ext1_ee_len + ext2_ee_len > max_len)
  1346. return 0;
  1347. #ifdef AGGRESSIVE_TEST
  1348. if (ext1_ee_len >= 4)
  1349. return 0;
  1350. #endif
  1351. if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
  1352. return 1;
  1353. return 0;
  1354. }
  1355. /*
  1356. * This function tries to merge the "ex" extent to the next extent in the tree.
  1357. * It always tries to merge towards right. If you want to merge towards
  1358. * left, pass "ex - 1" as argument instead of "ex".
  1359. * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
  1360. * 1 if they got merged.
  1361. */
  1362. static int ext4_ext_try_to_merge_right(struct inode *inode,
  1363. struct ext4_ext_path *path,
  1364. struct ext4_extent *ex)
  1365. {
  1366. struct ext4_extent_header *eh;
  1367. unsigned int depth, len;
  1368. int merge_done = 0;
  1369. int uninitialized = 0;
  1370. depth = ext_depth(inode);
  1371. BUG_ON(path[depth].p_hdr == NULL);
  1372. eh = path[depth].p_hdr;
  1373. while (ex < EXT_LAST_EXTENT(eh)) {
  1374. if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
  1375. break;
  1376. /* merge with next extent! */
  1377. if (ext4_ext_is_uninitialized(ex))
  1378. uninitialized = 1;
  1379. ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
  1380. + ext4_ext_get_actual_len(ex + 1));
  1381. if (uninitialized)
  1382. ext4_ext_mark_uninitialized(ex);
  1383. if (ex + 1 < EXT_LAST_EXTENT(eh)) {
  1384. len = (EXT_LAST_EXTENT(eh) - ex - 1)
  1385. * sizeof(struct ext4_extent);
  1386. memmove(ex + 1, ex + 2, len);
  1387. }
  1388. le16_add_cpu(&eh->eh_entries, -1);
  1389. merge_done = 1;
  1390. WARN_ON(eh->eh_entries == 0);
  1391. if (!eh->eh_entries)
  1392. EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
  1393. }
  1394. return merge_done;
  1395. }
  1396. /*
  1397. * This function tries to merge the @ex extent to neighbours in the tree.
  1398. * return 1 if merge left else 0.
  1399. */
  1400. static int ext4_ext_try_to_merge(struct inode *inode,
  1401. struct ext4_ext_path *path,
  1402. struct ext4_extent *ex) {
  1403. struct ext4_extent_header *eh;
  1404. unsigned int depth;
  1405. int merge_done = 0;
  1406. int ret = 0;
  1407. depth = ext_depth(inode);
  1408. BUG_ON(path[depth].p_hdr == NULL);
  1409. eh = path[depth].p_hdr;
  1410. if (ex > EXT_FIRST_EXTENT(eh))
  1411. merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
  1412. if (!merge_done)
  1413. ret = ext4_ext_try_to_merge_right(inode, path, ex);
  1414. return ret;
  1415. }
  1416. /*
  1417. * check if a portion of the "newext" extent overlaps with an
  1418. * existing extent.
  1419. *
  1420. * If there is an overlap discovered, it updates the length of the newext
  1421. * such that there will be no overlap, and then returns 1.
  1422. * If there is no overlap found, it returns 0.
  1423. */
  1424. static unsigned int ext4_ext_check_overlap(struct inode *inode,
  1425. struct ext4_extent *newext,
  1426. struct ext4_ext_path *path)
  1427. {
  1428. ext4_lblk_t b1, b2;
  1429. unsigned int depth, len1;
  1430. unsigned int ret = 0;
  1431. b1 = le32_to_cpu(newext->ee_block);
  1432. len1 = ext4_ext_get_actual_len(newext);
  1433. depth = ext_depth(inode);
  1434. if (!path[depth].p_ext)
  1435. goto out;
  1436. b2 = le32_to_cpu(path[depth].p_ext->ee_block);
  1437. /*
  1438. * get the next allocated block if the extent in the path
  1439. * is before the requested block(s)
  1440. */
  1441. if (b2 < b1) {
  1442. b2 = ext4_ext_next_allocated_block(path);
  1443. if (b2 == EXT_MAX_BLOCKS)
  1444. goto out;
  1445. }
  1446. /* check for wrap through zero on extent logical start block*/
  1447. if (b1 + len1 < b1) {
  1448. len1 = EXT_MAX_BLOCKS - b1;
  1449. newext->ee_len = cpu_to_le16(len1);
  1450. ret = 1;
  1451. }
  1452. /* check for overlap */
  1453. if (b1 + len1 > b2) {
  1454. newext->ee_len = cpu_to_le16(b2 - b1);
  1455. ret = 1;
  1456. }
  1457. out:
  1458. return ret;
  1459. }
  1460. /*
  1461. * ext4_ext_insert_extent:
  1462. * tries to merge requsted extent into the existing extent or
  1463. * inserts requested extent as new one into the tree,
  1464. * creating new leaf in the no-space case.
  1465. */
  1466. int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
  1467. struct ext4_ext_path *path,
  1468. struct ext4_extent *newext, int flag)
  1469. {
  1470. struct ext4_extent_header *eh;
  1471. struct ext4_extent *ex, *fex;
  1472. struct ext4_extent *nearex; /* nearest extent */
  1473. struct ext4_ext_path *npath = NULL;
  1474. int depth, len, err;
  1475. ext4_lblk_t next;
  1476. unsigned uninitialized = 0;
  1477. int flags = 0;
  1478. if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
  1479. EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
  1480. return -EIO;
  1481. }
  1482. depth = ext_depth(inode);
  1483. ex = path[depth].p_ext;
  1484. if (unlikely(path[depth].p_hdr == NULL)) {
  1485. EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
  1486. return -EIO;
  1487. }
  1488. /* try to insert block into found extent and return */
  1489. if (ex && !(flag & EXT4_GET_BLOCKS_PRE_IO)
  1490. && ext4_can_extents_be_merged(inode, ex, newext)) {
  1491. ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n",
  1492. ext4_ext_is_uninitialized(newext),
  1493. ext4_ext_get_actual_len(newext),
  1494. le32_to_cpu(ex->ee_block),
  1495. ext4_ext_is_uninitialized(ex),
  1496. ext4_ext_get_actual_len(ex),
  1497. ext4_ext_pblock(ex));
  1498. err = ext4_ext_get_access(handle, inode, path + depth);
  1499. if (err)
  1500. return err;
  1501. /*
  1502. * ext4_can_extents_be_merged should have checked that either
  1503. * both extents are uninitialized, or both aren't. Thus we
  1504. * need to check only one of them here.
  1505. */
  1506. if (ext4_ext_is_uninitialized(ex))
  1507. uninitialized = 1;
  1508. ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
  1509. + ext4_ext_get_actual_len(newext));
  1510. if (uninitialized)
  1511. ext4_ext_mark_uninitialized(ex);
  1512. eh = path[depth].p_hdr;
  1513. nearex = ex;
  1514. goto merge;
  1515. }
  1516. depth = ext_depth(inode);
  1517. eh = path[depth].p_hdr;
  1518. if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
  1519. goto has_space;
  1520. /* probably next leaf has space for us? */
  1521. fex = EXT_LAST_EXTENT(eh);
  1522. next = EXT_MAX_BLOCKS;
  1523. if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
  1524. next = ext4_ext_next_leaf_block(path);
  1525. if (next != EXT_MAX_BLOCKS) {
  1526. ext_debug("next leaf block - %d\n", next);
  1527. BUG_ON(npath != NULL);
  1528. npath = ext4_ext_find_extent(inode, next, NULL);
  1529. if (IS_ERR(npath))
  1530. return PTR_ERR(npath);
  1531. BUG_ON(npath->p_depth != path->p_depth);
  1532. eh = npath[depth].p_hdr;
  1533. if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
  1534. ext_debug("next leaf isn't full(%d)\n",
  1535. le16_to_cpu(eh->eh_entries));
  1536. path = npath;
  1537. goto has_space;
  1538. }
  1539. ext_debug("next leaf has no free space(%d,%d)\n",
  1540. le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
  1541. }
  1542. /*
  1543. * There is no free space in the found leaf.
  1544. * We're gonna add a new leaf in the tree.
  1545. */
  1546. if (flag & EXT4_GET_BLOCKS_PUNCH_OUT_EXT)
  1547. flags = EXT4_MB_USE_ROOT_BLOCKS;
  1548. err = ext4_ext_create_new_leaf(handle, inode, flags, path, newext);
  1549. if (err)
  1550. goto cleanup;
  1551. depth = ext_depth(inode);
  1552. eh = path[depth].p_hdr;
  1553. has_space:
  1554. nearex = path[depth].p_ext;
  1555. err = ext4_ext_get_access(handle, inode, path + depth);
  1556. if (err)
  1557. goto cleanup;
  1558. if (!nearex) {
  1559. /* there is no extent in this leaf, create first one */
  1560. ext_debug("first extent in the leaf: %d:%llu:[%d]%d\n",
  1561. le32_to_cpu(newext->ee_block),
  1562. ext4_ext_pblock(newext),
  1563. ext4_ext_is_uninitialized(newext),
  1564. ext4_ext_get_actual_len(newext));
  1565. path[depth].p_ext = EXT_FIRST_EXTENT(eh);
  1566. } else if (le32_to_cpu(newext->ee_block)
  1567. > le32_to_cpu(nearex->ee_block)) {
  1568. /* BUG_ON(newext->ee_block == nearex->ee_block); */
  1569. if (nearex != EXT_LAST_EXTENT(eh)) {
  1570. len = EXT_MAX_EXTENT(eh) - nearex;
  1571. len = (len - 1) * sizeof(struct ext4_extent);
  1572. len = len < 0 ? 0 : len;
  1573. ext_debug("insert %d:%llu:[%d]%d after: nearest 0x%p, "
  1574. "move %d from 0x%p to 0x%p\n",
  1575. le32_to_cpu(newext->ee_block),
  1576. ext4_ext_pblock(newext),
  1577. ext4_ext_is_uninitialized(newext),
  1578. ext4_ext_get_actual_len(newext),
  1579. nearex, len, nearex + 1, nearex + 2);
  1580. memmove(nearex + 2, nearex + 1, len);
  1581. }
  1582. path[depth].p_ext = nearex + 1;
  1583. } else {
  1584. BUG_ON(newext->ee_block == nearex->ee_block);
  1585. len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
  1586. len = len < 0 ? 0 : len;
  1587. ext_debug("insert %d:%llu:[%d]%d before: nearest 0x%p, "
  1588. "move %d from 0x%p to 0x%p\n",
  1589. le32_to_cpu(newext->ee_block),
  1590. ext4_ext_pblock(newext),
  1591. ext4_ext_is_uninitialized(newext),
  1592. ext4_ext_get_actual_len(newext),
  1593. nearex, len, nearex, nearex + 1);
  1594. memmove(nearex + 1, nearex, len);
  1595. path[depth].p_ext = nearex;
  1596. }
  1597. le16_add_cpu(&eh->eh_entries, 1);
  1598. nearex = path[depth].p_ext;
  1599. nearex->ee_block = newext->ee_block;
  1600. ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
  1601. nearex->ee_len = newext->ee_len;
  1602. merge:
  1603. /* try to merge extents to the right */
  1604. if (!(flag & EXT4_GET_BLOCKS_PRE_IO))
  1605. ext4_ext_try_to_merge(inode, path, nearex);
  1606. /* try to merge extents to the left */
  1607. /* time to correct all indexes above */
  1608. err = ext4_ext_correct_indexes(handle, inode, path);
  1609. if (err)
  1610. goto cleanup;
  1611. err = ext4_ext_dirty(handle, inode, path + depth);
  1612. cleanup:
  1613. if (npath) {
  1614. ext4_ext_drop_refs(npath);
  1615. kfree(npath);
  1616. }
  1617. ext4_ext_invalidate_cache(inode);
  1618. return err;
  1619. }
  1620. static int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
  1621. ext4_lblk_t num, ext_prepare_callback func,
  1622. void *cbdata)
  1623. {
  1624. struct ext4_ext_path *path = NULL;
  1625. struct ext4_ext_cache cbex;
  1626. struct ext4_extent *ex;
  1627. ext4_lblk_t next, start = 0, end = 0;
  1628. ext4_lblk_t last = block + num;
  1629. int depth, exists, err = 0;
  1630. BUG_ON(func == NULL);
  1631. BUG_ON(inode == NULL);
  1632. while (block < last && block != EXT_MAX_BLOCKS) {
  1633. num = last - block;
  1634. /* find extent for this block */
  1635. down_read(&EXT4_I(inode)->i_data_sem);
  1636. path = ext4_ext_find_extent(inode, block, path);
  1637. up_read(&EXT4_I(inode)->i_data_sem);
  1638. if (IS_ERR(path)) {
  1639. err = PTR_ERR(path);
  1640. path = NULL;
  1641. break;
  1642. }
  1643. depth = ext_depth(inode);
  1644. if (unlikely(path[depth].p_hdr == NULL)) {
  1645. EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
  1646. err = -EIO;
  1647. break;
  1648. }
  1649. ex = path[depth].p_ext;
  1650. next = ext4_ext_next_allocated_block(path);
  1651. exists = 0;
  1652. if (!ex) {
  1653. /* there is no extent yet, so try to allocate
  1654. * all requested space */
  1655. start = block;
  1656. end = block + num;
  1657. } else if (le32_to_cpu(ex->ee_block) > block) {
  1658. /* need to allocate space before found extent */
  1659. start = block;
  1660. end = le32_to_cpu(ex->ee_block);
  1661. if (block + num < end)
  1662. end = block + num;
  1663. } else if (block >= le32_to_cpu(ex->ee_block)
  1664. + ext4_ext_get_actual_len(ex)) {
  1665. /* need to allocate space after found extent */
  1666. start = block;
  1667. end = block + num;
  1668. if (end >= next)
  1669. end = next;
  1670. } else if (block >= le32_to_cpu(ex->ee_block)) {
  1671. /*
  1672. * some part of requested space is covered
  1673. * by found extent
  1674. */
  1675. start = block;
  1676. end = le32_to_cpu(ex->ee_block)
  1677. + ext4_ext_get_actual_len(ex);
  1678. if (block + num < end)
  1679. end = block + num;
  1680. exists = 1;
  1681. } else {
  1682. BUG();
  1683. }
  1684. BUG_ON(end <= start);
  1685. if (!exists) {
  1686. cbex.ec_block = start;
  1687. cbex.ec_len = end - start;
  1688. cbex.ec_start = 0;
  1689. } else {
  1690. cbex.ec_block = le32_to_cpu(ex->ee_block);
  1691. cbex.ec_len = ext4_ext_get_actual_len(ex);
  1692. cbex.ec_start = ext4_ext_pblock(ex);
  1693. }
  1694. if (unlikely(cbex.ec_len == 0)) {
  1695. EXT4_ERROR_INODE(inode, "cbex.ec_len == 0");
  1696. err = -EIO;
  1697. break;
  1698. }
  1699. err = func(inode, next, &cbex, ex, cbdata);
  1700. ext4_ext_drop_refs(path);
  1701. if (err < 0)
  1702. break;
  1703. if (err == EXT_REPEAT)
  1704. continue;
  1705. else if (err == EXT_BREAK) {
  1706. err = 0;
  1707. break;
  1708. }
  1709. if (ext_depth(inode) != depth) {
  1710. /* depth was changed. we have to realloc path */
  1711. kfree(path);
  1712. path = NULL;
  1713. }
  1714. block = cbex.ec_block + cbex.ec_len;
  1715. }
  1716. if (path) {
  1717. ext4_ext_drop_refs(path);
  1718. kfree(path);
  1719. }
  1720. return err;
  1721. }
  1722. static void
  1723. ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
  1724. __u32 len, ext4_fsblk_t start)
  1725. {
  1726. struct ext4_ext_cache *cex;
  1727. BUG_ON(len == 0);
  1728. spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
  1729. cex = &EXT4_I(inode)->i_cached_extent;
  1730. cex->ec_block = block;
  1731. cex->ec_len = len;
  1732. cex->ec_start = start;
  1733. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  1734. }
  1735. /*
  1736. * ext4_ext_put_gap_in_cache:
  1737. * calculate boundaries of the gap that the requested block fits into
  1738. * and cache this gap
  1739. */
  1740. static void
  1741. ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
  1742. ext4_lblk_t block)
  1743. {
  1744. int depth = ext_depth(inode);
  1745. unsigned long len;
  1746. ext4_lblk_t lblock;
  1747. struct ext4_extent *ex;
  1748. ex = path[depth].p_ext;
  1749. if (ex == NULL) {
  1750. /* there is no extent yet, so gap is [0;-] */
  1751. lblock = 0;
  1752. len = EXT_MAX_BLOCKS;
  1753. ext_debug("cache gap(whole file):");
  1754. } else if (block < le32_to_cpu(ex->ee_block)) {
  1755. lblock = block;
  1756. len = le32_to_cpu(ex->ee_block) - block;
  1757. ext_debug("cache gap(before): %u [%u:%u]",
  1758. block,
  1759. le32_to_cpu(ex->ee_block),
  1760. ext4_ext_get_actual_len(ex));
  1761. } else if (block >= le32_to_cpu(ex->ee_block)
  1762. + ext4_ext_get_actual_len(ex)) {
  1763. ext4_lblk_t next;
  1764. lblock = le32_to_cpu(ex->ee_block)
  1765. + ext4_ext_get_actual_len(ex);
  1766. next = ext4_ext_next_allocated_block(path);
  1767. ext_debug("cache gap(after): [%u:%u] %u",
  1768. le32_to_cpu(ex->ee_block),
  1769. ext4_ext_get_actual_len(ex),
  1770. block);
  1771. BUG_ON(next == lblock);
  1772. len = next - lblock;
  1773. } else {
  1774. lblock = len = 0;
  1775. BUG();
  1776. }
  1777. ext_debug(" -> %u:%lu\n", lblock, len);
  1778. ext4_ext_put_in_cache(inode, lblock, len, 0);
  1779. }
  1780. /*
  1781. * ext4_ext_check_cache()
  1782. * Checks to see if the given block is in the cache.
  1783. * If it is, the cached extent is stored in the given
  1784. * cache extent pointer. If the cached extent is a hole,
  1785. * this routine should be used instead of
  1786. * ext4_ext_in_cache if the calling function needs to
  1787. * know the size of the hole.
  1788. *
  1789. * @inode: The files inode
  1790. * @block: The block to look for in the cache
  1791. * @ex: Pointer where the cached extent will be stored
  1792. * if it contains block
  1793. *
  1794. * Return 0 if cache is invalid; 1 if the cache is valid
  1795. */
  1796. static int ext4_ext_check_cache(struct inode *inode, ext4_lblk_t block,
  1797. struct ext4_ext_cache *ex){
  1798. struct ext4_ext_cache *cex;
  1799. struct ext4_sb_info *sbi;
  1800. int ret = 0;
  1801. /*
  1802. * We borrow i_block_reservation_lock to protect i_cached_extent
  1803. */
  1804. spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
  1805. cex = &EXT4_I(inode)->i_cached_extent;
  1806. sbi = EXT4_SB(inode->i_sb);
  1807. /* has cache valid data? */
  1808. if (cex->ec_len == 0)
  1809. goto errout;
  1810. if (in_range(block, cex->ec_block, cex->ec_len)) {
  1811. memcpy(ex, cex, sizeof(struct ext4_ext_cache));
  1812. ext_debug("%u cached by %u:%u:%llu\n",
  1813. block,
  1814. cex->ec_block, cex->ec_len, cex->ec_start);
  1815. ret = 1;
  1816. }
  1817. errout:
  1818. if (!ret)
  1819. sbi->extent_cache_misses++;
  1820. else
  1821. sbi->extent_cache_hits++;
  1822. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  1823. return ret;
  1824. }
  1825. /*
  1826. * ext4_ext_in_cache()
  1827. * Checks to see if the given block is in the cache.
  1828. * If it is, the cached extent is stored in the given
  1829. * extent pointer.
  1830. *
  1831. * @inode: The files inode
  1832. * @block: The block to look for in the cache
  1833. * @ex: Pointer where the cached extent will be stored
  1834. * if it contains block
  1835. *
  1836. * Return 0 if cache is invalid; 1 if the cache is valid
  1837. */
  1838. static int
  1839. ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
  1840. struct ext4_extent *ex)
  1841. {
  1842. struct ext4_ext_cache cex;
  1843. int ret = 0;
  1844. if (ext4_ext_check_cache(inode, block, &cex)) {
  1845. ex->ee_block = cpu_to_le32(cex.ec_block);
  1846. ext4_ext_store_pblock(ex, cex.ec_start);
  1847. ex->ee_len = cpu_to_le16(cex.ec_len);
  1848. ret = 1;
  1849. }
  1850. return ret;
  1851. }
  1852. /*
  1853. * ext4_ext_rm_idx:
  1854. * removes index from the index block.
  1855. * It's used in truncate case only, thus all requests are for
  1856. * last index in the block only.
  1857. */
  1858. static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
  1859. struct ext4_ext_path *path)
  1860. {
  1861. int err;
  1862. ext4_fsblk_t leaf;
  1863. /* free index block */
  1864. path--;
  1865. leaf = ext4_idx_pblock(path->p_idx);
  1866. if (unlikely(path->p_hdr->eh_entries == 0)) {
  1867. EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
  1868. return -EIO;
  1869. }
  1870. err = ext4_ext_get_access(handle, inode, path);
  1871. if (err)
  1872. return err;
  1873. le16_add_cpu(&path->p_hdr->eh_entries, -1);
  1874. err = ext4_ext_dirty(handle, inode, path);
  1875. if (err)
  1876. return err;
  1877. ext_debug("index is empty, remove it, free block %llu\n", leaf);
  1878. ext4_free_blocks(handle, inode, NULL, leaf, 1,
  1879. EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
  1880. return err;
  1881. }
  1882. /*
  1883. * ext4_ext_calc_credits_for_single_extent:
  1884. * This routine returns max. credits that needed to insert an extent
  1885. * to the extent tree.
  1886. * When pass the actual path, the caller should calculate credits
  1887. * under i_data_sem.
  1888. */
  1889. int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
  1890. struct ext4_ext_path *path)
  1891. {
  1892. if (path) {
  1893. int depth = ext_depth(inode);
  1894. int ret = 0;
  1895. /* probably there is space in leaf? */
  1896. if (le16_to_cpu(path[depth].p_hdr->eh_entries)
  1897. < le16_to_cpu(path[depth].p_hdr->eh_max)) {
  1898. /*
  1899. * There are some space in the leaf tree, no
  1900. * need to account for leaf block credit
  1901. *
  1902. * bitmaps and block group descriptor blocks
  1903. * and other metadat blocks still need to be
  1904. * accounted.
  1905. */
  1906. /* 1 bitmap, 1 block group descriptor */
  1907. ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
  1908. return ret;
  1909. }
  1910. }
  1911. return ext4_chunk_trans_blocks(inode, nrblocks);
  1912. }
  1913. /*
  1914. * How many index/leaf blocks need to change/allocate to modify nrblocks?
  1915. *
  1916. * if nrblocks are fit in a single extent (chunk flag is 1), then
  1917. * in the worse case, each tree level index/leaf need to be changed
  1918. * if the tree split due to insert a new extent, then the old tree
  1919. * index/leaf need to be updated too
  1920. *
  1921. * If the nrblocks are discontiguous, they could cause
  1922. * the whole tree split more than once, but this is really rare.
  1923. */
  1924. int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
  1925. {
  1926. int index;
  1927. int depth = ext_depth(inode);
  1928. if (chunk)
  1929. index = depth * 2;
  1930. else
  1931. index = depth * 3;
  1932. return index;
  1933. }
  1934. static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
  1935. struct ext4_extent *ex,
  1936. ext4_lblk_t from, ext4_lblk_t to)
  1937. {
  1938. unsigned short ee_len = ext4_ext_get_actual_len(ex);
  1939. int flags = EXT4_FREE_BLOCKS_FORGET;
  1940. if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
  1941. flags |= EXT4_FREE_BLOCKS_METADATA;
  1942. #ifdef EXTENTS_STATS
  1943. {
  1944. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1945. spin_lock(&sbi->s_ext_stats_lock);
  1946. sbi->s_ext_blocks += ee_len;
  1947. sbi->s_ext_extents++;
  1948. if (ee_len < sbi->s_ext_min)
  1949. sbi->s_ext_min = ee_len;
  1950. if (ee_len > sbi->s_ext_max)
  1951. sbi->s_ext_max = ee_len;
  1952. if (ext_depth(inode) > sbi->s_depth_max)
  1953. sbi->s_depth_max = ext_depth(inode);
  1954. spin_unlock(&sbi->s_ext_stats_lock);
  1955. }
  1956. #endif
  1957. if (from >= le32_to_cpu(ex->ee_block)
  1958. && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
  1959. /* tail removal */
  1960. ext4_lblk_t num;
  1961. ext4_fsblk_t start;
  1962. num = le32_to_cpu(ex->ee_block) + ee_len - from;
  1963. start = ext4_ext_pblock(ex) + ee_len - num;
  1964. ext_debug("free last %u blocks starting %llu\n", num, start);
  1965. ext4_free_blocks(handle, inode, NULL, start, num, flags);
  1966. } else if (from == le32_to_cpu(ex->ee_block)
  1967. && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
  1968. /* head removal */
  1969. ext4_lblk_t num;
  1970. ext4_fsblk_t start;
  1971. num = to - from;
  1972. start = ext4_ext_pblock(ex);
  1973. ext_debug("free first %u blocks starting %llu\n", num, start);
  1974. ext4_free_blocks(handle, inode, 0, start, num, flags);
  1975. } else {
  1976. printk(KERN_INFO "strange request: removal(2) "
  1977. "%u-%u from %u:%u\n",
  1978. from, to, le32_to_cpu(ex->ee_block), ee_len);
  1979. }
  1980. return 0;
  1981. }
  1982. /*
  1983. * ext4_ext_rm_leaf() Removes the extents associated with the
  1984. * blocks appearing between "start" and "end", and splits the extents
  1985. * if "start" and "end" appear in the same extent
  1986. *
  1987. * @handle: The journal handle
  1988. * @inode: The files inode
  1989. * @path: The path to the leaf
  1990. * @start: The first block to remove
  1991. * @end: The last block to remove
  1992. */
  1993. static int
  1994. ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
  1995. struct ext4_ext_path *path, ext4_lblk_t start,
  1996. ext4_lblk_t end)
  1997. {
  1998. int err = 0, correct_index = 0;
  1999. int depth = ext_depth(inode), credits;
  2000. struct ext4_extent_header *eh;
  2001. ext4_lblk_t a, b, block;
  2002. unsigned num;
  2003. ext4_lblk_t ex_ee_block;
  2004. unsigned short ex_ee_len;
  2005. unsigned uninitialized = 0;
  2006. struct ext4_extent *ex;
  2007. struct ext4_map_blocks map;
  2008. /* the header must be checked already in ext4_ext_remove_space() */
  2009. ext_debug("truncate since %u in leaf\n", start);
  2010. if (!path[depth].p_hdr)
  2011. path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
  2012. eh = path[depth].p_hdr;
  2013. if (unlikely(path[depth].p_hdr == NULL)) {
  2014. EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
  2015. return -EIO;
  2016. }
  2017. /* find where to start removing */
  2018. ex = EXT_LAST_EXTENT(eh);
  2019. ex_ee_block = le32_to_cpu(ex->ee_block);
  2020. ex_ee_len = ext4_ext_get_actual_len(ex);
  2021. while (ex >= EXT_FIRST_EXTENT(eh) &&
  2022. ex_ee_block + ex_ee_len > start) {
  2023. if (ext4_ext_is_uninitialized(ex))
  2024. uninitialized = 1;
  2025. else
  2026. uninitialized = 0;
  2027. ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
  2028. uninitialized, ex_ee_len);
  2029. path[depth].p_ext = ex;
  2030. a = ex_ee_block > start ? ex_ee_block : start;
  2031. b = ex_ee_block+ex_ee_len - 1 < end ?
  2032. ex_ee_block+ex_ee_len - 1 : end;
  2033. ext_debug(" border %u:%u\n", a, b);
  2034. /* If this extent is beyond the end of the hole, skip it */
  2035. if (end <= ex_ee_block) {
  2036. ex--;
  2037. ex_ee_block = le32_to_cpu(ex->ee_block);
  2038. ex_ee_len = ext4_ext_get_actual_len(ex);
  2039. continue;
  2040. } else if (a != ex_ee_block &&
  2041. b != ex_ee_block + ex_ee_len - 1) {
  2042. /*
  2043. * If this is a truncate, then this condition should
  2044. * never happen because at least one of the end points
  2045. * needs to be on the edge of the extent.
  2046. */
  2047. if (end == EXT_MAX_BLOCKS - 1) {
  2048. ext_debug(" bad truncate %u:%u\n",
  2049. start, end);
  2050. block = 0;
  2051. num = 0;
  2052. err = -EIO;
  2053. goto out;
  2054. }
  2055. /*
  2056. * else this is a hole punch, so the extent needs to
  2057. * be split since neither edge of the hole is on the
  2058. * extent edge
  2059. */
  2060. else{
  2061. map.m_pblk = ext4_ext_pblock(ex);
  2062. map.m_lblk = ex_ee_block;
  2063. map.m_len = b - ex_ee_block;
  2064. err = ext4_split_extent(handle,
  2065. inode, path, &map, 0,
  2066. EXT4_GET_BLOCKS_PUNCH_OUT_EXT |
  2067. EXT4_GET_BLOCKS_PRE_IO);
  2068. if (err < 0)
  2069. goto out;
  2070. ex_ee_len = ext4_ext_get_actual_len(ex);
  2071. b = ex_ee_block+ex_ee_len - 1 < end ?
  2072. ex_ee_block+ex_ee_len - 1 : end;
  2073. /* Then remove tail of this extent */
  2074. block = ex_ee_block;
  2075. num = a - block;
  2076. }
  2077. } else if (a != ex_ee_block) {
  2078. /* remove tail of the extent */
  2079. block = ex_ee_block;
  2080. num = a - block;
  2081. } else if (b != ex_ee_block + ex_ee_len - 1) {
  2082. /* remove head of the extent */
  2083. block = b;
  2084. num = ex_ee_block + ex_ee_len - b;
  2085. /*
  2086. * If this is a truncate, this condition
  2087. * should never happen
  2088. */
  2089. if (end == EXT_MAX_BLOCKS - 1) {
  2090. ext_debug(" bad truncate %u:%u\n",
  2091. start, end);
  2092. err = -EIO;
  2093. goto out;
  2094. }
  2095. } else {
  2096. /* remove whole extent: excellent! */
  2097. block = ex_ee_block;
  2098. num = 0;
  2099. if (a != ex_ee_block) {
  2100. ext_debug(" bad truncate %u:%u\n",
  2101. start, end);
  2102. err = -EIO;
  2103. goto out;
  2104. }
  2105. if (b != ex_ee_block + ex_ee_len - 1) {
  2106. ext_debug(" bad truncate %u:%u\n",
  2107. start, end);
  2108. err = -EIO;
  2109. goto out;
  2110. }
  2111. }
  2112. /*
  2113. * 3 for leaf, sb, and inode plus 2 (bmap and group
  2114. * descriptor) for each block group; assume two block
  2115. * groups plus ex_ee_len/blocks_per_block_group for
  2116. * the worst case
  2117. */
  2118. credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
  2119. if (ex == EXT_FIRST_EXTENT(eh)) {
  2120. correct_index = 1;
  2121. credits += (ext_depth(inode)) + 1;
  2122. }
  2123. credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
  2124. err = ext4_ext_truncate_extend_restart(handle, inode, credits);
  2125. if (err)
  2126. goto out;
  2127. err = ext4_ext_get_access(handle, inode, path + depth);
  2128. if (err)
  2129. goto out;
  2130. err = ext4_remove_blocks(handle, inode, ex, a, b);
  2131. if (err)
  2132. goto out;
  2133. if (num == 0) {
  2134. /* this extent is removed; mark slot entirely unused */
  2135. ext4_ext_store_pblock(ex, 0);
  2136. } else if (block != ex_ee_block) {
  2137. /*
  2138. * If this was a head removal, then we need to update
  2139. * the physical block since it is now at a different
  2140. * location
  2141. */
  2142. ext4_ext_store_pblock(ex, ext4_ext_pblock(ex) + (b-a));
  2143. }
  2144. ex->ee_block = cpu_to_le32(block);
  2145. ex->ee_len = cpu_to_le16(num);
  2146. /*
  2147. * Do not mark uninitialized if all the blocks in the
  2148. * extent have been removed.
  2149. */
  2150. if (uninitialized && num)
  2151. ext4_ext_mark_uninitialized(ex);
  2152. err = ext4_ext_dirty(handle, inode, path + depth);
  2153. if (err)
  2154. goto out;
  2155. /*
  2156. * If the extent was completely released,
  2157. * we need to remove it from the leaf
  2158. */
  2159. if (num == 0) {
  2160. if (end != EXT_MAX_BLOCKS - 1) {
  2161. /*
  2162. * For hole punching, we need to scoot all the
  2163. * extents up when an extent is removed so that
  2164. * we dont have blank extents in the middle
  2165. */
  2166. memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
  2167. sizeof(struct ext4_extent));
  2168. /* Now get rid of the one at the end */
  2169. memset(EXT_LAST_EXTENT(eh), 0,
  2170. sizeof(struct ext4_extent));
  2171. }
  2172. le16_add_cpu(&eh->eh_entries, -1);
  2173. }
  2174. ext_debug("new extent: %u:%u:%llu\n", block, num,
  2175. ext4_ext_pblock(ex));
  2176. ex--;
  2177. ex_ee_block = le32_to_cpu(ex->ee_block);
  2178. ex_ee_len = ext4_ext_get_actual_len(ex);
  2179. }
  2180. if (correct_index && eh->eh_entries)
  2181. err = ext4_ext_correct_indexes(handle, inode, path);
  2182. /* if this leaf is free, then we should
  2183. * remove it from index block above */
  2184. if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
  2185. err = ext4_ext_rm_idx(handle, inode, path + depth);
  2186. out:
  2187. return err;
  2188. }
  2189. /*
  2190. * ext4_ext_more_to_rm:
  2191. * returns 1 if current index has to be freed (even partial)
  2192. */
  2193. static int
  2194. ext4_ext_more_to_rm(struct ext4_ext_path *path)
  2195. {
  2196. BUG_ON(path->p_idx == NULL);
  2197. if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
  2198. return 0;
  2199. /*
  2200. * if truncate on deeper level happened, it wasn't partial,
  2201. * so we have to consider current index for truncation
  2202. */
  2203. if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
  2204. return 0;
  2205. return 1;
  2206. }
  2207. static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
  2208. {
  2209. struct super_block *sb = inode->i_sb;
  2210. int depth = ext_depth(inode);
  2211. struct ext4_ext_path *path;
  2212. handle_t *handle;
  2213. int i, err;
  2214. ext_debug("truncate since %u\n", start);
  2215. /* probably first extent we're gonna free will be last in block */
  2216. handle = ext4_journal_start(inode, depth + 1);
  2217. if (IS_ERR(handle))
  2218. return PTR_ERR(handle);
  2219. again:
  2220. ext4_ext_invalidate_cache(inode);
  2221. /*
  2222. * We start scanning from right side, freeing all the blocks
  2223. * after i_size and walking into the tree depth-wise.
  2224. */
  2225. depth = ext_depth(inode);
  2226. path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
  2227. if (path == NULL) {
  2228. ext4_journal_stop(handle);
  2229. return -ENOMEM;
  2230. }
  2231. path[0].p_depth = depth;
  2232. path[0].p_hdr = ext_inode_hdr(inode);
  2233. if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
  2234. err = -EIO;
  2235. goto out;
  2236. }
  2237. i = err = 0;
  2238. while (i >= 0 && err == 0) {
  2239. if (i == depth) {
  2240. /* this is leaf block */
  2241. err = ext4_ext_rm_leaf(handle, inode, path,
  2242. start, EXT_MAX_BLOCKS - 1);
  2243. /* root level has p_bh == NULL, brelse() eats this */
  2244. brelse(path[i].p_bh);
  2245. path[i].p_bh = NULL;
  2246. i--;
  2247. continue;
  2248. }
  2249. /* this is index block */
  2250. if (!path[i].p_hdr) {
  2251. ext_debug("initialize header\n");
  2252. path[i].p_hdr = ext_block_hdr(path[i].p_bh);
  2253. }
  2254. if (!path[i].p_idx) {
  2255. /* this level hasn't been touched yet */
  2256. path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
  2257. path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
  2258. ext_debug("init index ptr: hdr 0x%p, num %d\n",
  2259. path[i].p_hdr,
  2260. le16_to_cpu(path[i].p_hdr->eh_entries));
  2261. } else {
  2262. /* we were already here, see at next index */
  2263. path[i].p_idx--;
  2264. }
  2265. ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
  2266. i, EXT_FIRST_INDEX(path[i].p_hdr),
  2267. path[i].p_idx);
  2268. if (ext4_ext_more_to_rm(path + i)) {
  2269. struct buffer_head *bh;
  2270. /* go to the next level */
  2271. ext_debug("move to level %d (block %llu)\n",
  2272. i + 1, ext4_idx_pblock(path[i].p_idx));
  2273. memset(path + i + 1, 0, sizeof(*path));
  2274. bh = sb_bread(sb, ext4_idx_pblock(path[i].p_idx));
  2275. if (!bh) {
  2276. /* should we reset i_size? */
  2277. err = -EIO;
  2278. break;
  2279. }
  2280. if (WARN_ON(i + 1 > depth)) {
  2281. err = -EIO;
  2282. break;
  2283. }
  2284. if (ext4_ext_check(inode, ext_block_hdr(bh),
  2285. depth - i - 1)) {
  2286. err = -EIO;
  2287. break;
  2288. }
  2289. path[i + 1].p_bh = bh;
  2290. /* save actual number of indexes since this
  2291. * number is changed at the next iteration */
  2292. path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
  2293. i++;
  2294. } else {
  2295. /* we finished processing this index, go up */
  2296. if (path[i].p_hdr->eh_entries == 0 && i > 0) {
  2297. /* index is empty, remove it;
  2298. * handle must be already prepared by the
  2299. * truncatei_leaf() */
  2300. err = ext4_ext_rm_idx(handle, inode, path + i);
  2301. }
  2302. /* root level has p_bh == NULL, brelse() eats this */
  2303. brelse(path[i].p_bh);
  2304. path[i].p_bh = NULL;
  2305. i--;
  2306. ext_debug("return to level %d\n", i);
  2307. }
  2308. }
  2309. /* TODO: flexible tree reduction should be here */
  2310. if (path->p_hdr->eh_entries == 0) {
  2311. /*
  2312. * truncate to zero freed all the tree,
  2313. * so we need to correct eh_depth
  2314. */
  2315. err = ext4_ext_get_access(handle, inode, path);
  2316. if (err == 0) {
  2317. ext_inode_hdr(inode)->eh_depth = 0;
  2318. ext_inode_hdr(inode)->eh_max =
  2319. cpu_to_le16(ext4_ext_space_root(inode, 0));
  2320. err = ext4_ext_dirty(handle, inode, path);
  2321. }
  2322. }
  2323. out:
  2324. ext4_ext_drop_refs(path);
  2325. kfree(path);
  2326. if (err == -EAGAIN)
  2327. goto again;
  2328. ext4_journal_stop(handle);
  2329. return err;
  2330. }
  2331. /*
  2332. * called at mount time
  2333. */
  2334. void ext4_ext_init(struct super_block *sb)
  2335. {
  2336. /*
  2337. * possible initialization would be here
  2338. */
  2339. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
  2340. #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
  2341. printk(KERN_INFO "EXT4-fs: file extents enabled");
  2342. #ifdef AGGRESSIVE_TEST
  2343. printk(", aggressive tests");
  2344. #endif
  2345. #ifdef CHECK_BINSEARCH
  2346. printk(", check binsearch");
  2347. #endif
  2348. #ifdef EXTENTS_STATS
  2349. printk(", stats");
  2350. #endif
  2351. printk("\n");
  2352. #endif
  2353. #ifdef EXTENTS_STATS
  2354. spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
  2355. EXT4_SB(sb)->s_ext_min = 1 << 30;
  2356. EXT4_SB(sb)->s_ext_max = 0;
  2357. #endif
  2358. }
  2359. }
  2360. /*
  2361. * called at umount time
  2362. */
  2363. void ext4_ext_release(struct super_block *sb)
  2364. {
  2365. if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
  2366. return;
  2367. #ifdef EXTENTS_STATS
  2368. if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
  2369. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2370. printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
  2371. sbi->s_ext_blocks, sbi->s_ext_extents,
  2372. sbi->s_ext_blocks / sbi->s_ext_extents);
  2373. printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
  2374. sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
  2375. }
  2376. #endif
  2377. }
  2378. /* FIXME!! we need to try to merge to left or right after zero-out */
  2379. static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
  2380. {
  2381. ext4_fsblk_t ee_pblock;
  2382. unsigned int ee_len;
  2383. int ret;
  2384. ee_len = ext4_ext_get_actual_len(ex);
  2385. ee_pblock = ext4_ext_pblock(ex);
  2386. ret = sb_issue_zeroout(inode->i_sb, ee_pblock, ee_len, GFP_NOFS);
  2387. if (ret > 0)
  2388. ret = 0;
  2389. return ret;
  2390. }
  2391. /*
  2392. * used by extent splitting.
  2393. */
  2394. #define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \
  2395. due to ENOSPC */
  2396. #define EXT4_EXT_MARK_UNINIT1 0x2 /* mark first half uninitialized */
  2397. #define EXT4_EXT_MARK_UNINIT2 0x4 /* mark second half uninitialized */
  2398. /*
  2399. * ext4_split_extent_at() splits an extent at given block.
  2400. *
  2401. * @handle: the journal handle
  2402. * @inode: the file inode
  2403. * @path: the path to the extent
  2404. * @split: the logical block where the extent is splitted.
  2405. * @split_flags: indicates if the extent could be zeroout if split fails, and
  2406. * the states(init or uninit) of new extents.
  2407. * @flags: flags used to insert new extent to extent tree.
  2408. *
  2409. *
  2410. * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
  2411. * of which are deterimined by split_flag.
  2412. *
  2413. * There are two cases:
  2414. * a> the extent are splitted into two extent.
  2415. * b> split is not needed, and just mark the extent.
  2416. *
  2417. * return 0 on success.
  2418. */
  2419. static int ext4_split_extent_at(handle_t *handle,
  2420. struct inode *inode,
  2421. struct ext4_ext_path *path,
  2422. ext4_lblk_t split,
  2423. int split_flag,
  2424. int flags)
  2425. {
  2426. ext4_fsblk_t newblock;
  2427. ext4_lblk_t ee_block;
  2428. struct ext4_extent *ex, newex, orig_ex;
  2429. struct ext4_extent *ex2 = NULL;
  2430. unsigned int ee_len, depth;
  2431. int err = 0;
  2432. ext_debug("ext4_split_extents_at: inode %lu, logical"
  2433. "block %llu\n", inode->i_ino, (unsigned long long)split);
  2434. ext4_ext_show_leaf(inode, path);
  2435. depth = ext_depth(inode);
  2436. ex = path[depth].p_ext;
  2437. ee_block = le32_to_cpu(ex->ee_block);
  2438. ee_len = ext4_ext_get_actual_len(ex);
  2439. newblock = split - ee_block + ext4_ext_pblock(ex);
  2440. BUG_ON(split < ee_block || split >= (ee_block + ee_len));
  2441. err = ext4_ext_get_access(handle, inode, path + depth);
  2442. if (err)
  2443. goto out;
  2444. if (split == ee_block) {
  2445. /*
  2446. * case b: block @split is the block that the extent begins with
  2447. * then we just change the state of the extent, and splitting
  2448. * is not needed.
  2449. */
  2450. if (split_flag & EXT4_EXT_MARK_UNINIT2)
  2451. ext4_ext_mark_uninitialized(ex);
  2452. else
  2453. ext4_ext_mark_initialized(ex);
  2454. if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
  2455. ext4_ext_try_to_merge(inode, path, ex);
  2456. err = ext4_ext_dirty(handle, inode, path + depth);
  2457. goto out;
  2458. }
  2459. /* case a */
  2460. memcpy(&orig_ex, ex, sizeof(orig_ex));
  2461. ex->ee_len = cpu_to_le16(split - ee_block);
  2462. if (split_flag & EXT4_EXT_MARK_UNINIT1)
  2463. ext4_ext_mark_uninitialized(ex);
  2464. /*
  2465. * path may lead to new leaf, not to original leaf any more
  2466. * after ext4_ext_insert_extent() returns,
  2467. */
  2468. err = ext4_ext_dirty(handle, inode, path + depth);
  2469. if (err)
  2470. goto fix_extent_len;
  2471. ex2 = &newex;
  2472. ex2->ee_block = cpu_to_le32(split);
  2473. ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
  2474. ext4_ext_store_pblock(ex2, newblock);
  2475. if (split_flag & EXT4_EXT_MARK_UNINIT2)
  2476. ext4_ext_mark_uninitialized(ex2);
  2477. err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
  2478. if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
  2479. err = ext4_ext_zeroout(inode, &orig_ex);
  2480. if (err)
  2481. goto fix_extent_len;
  2482. /* update the extent length and mark as initialized */
  2483. ex->ee_len = cpu_to_le32(ee_len);
  2484. ext4_ext_try_to_merge(inode, path, ex);
  2485. err = ext4_ext_dirty(handle, inode, path + depth);
  2486. goto out;
  2487. } else if (err)
  2488. goto fix_extent_len;
  2489. out:
  2490. ext4_ext_show_leaf(inode, path);
  2491. return err;
  2492. fix_extent_len:
  2493. ex->ee_len = orig_ex.ee_len;
  2494. ext4_ext_dirty(handle, inode, path + depth);
  2495. return err;
  2496. }
  2497. /*
  2498. * ext4_split_extents() splits an extent and mark extent which is covered
  2499. * by @map as split_flags indicates
  2500. *
  2501. * It may result in splitting the extent into multiple extents (upto three)
  2502. * There are three possibilities:
  2503. * a> There is no split required
  2504. * b> Splits in two extents: Split is happening at either end of the extent
  2505. * c> Splits in three extents: Somone is splitting in middle of the extent
  2506. *
  2507. */
  2508. static int ext4_split_extent(handle_t *handle,
  2509. struct inode *inode,
  2510. struct ext4_ext_path *path,
  2511. struct ext4_map_blocks *map,
  2512. int split_flag,
  2513. int flags)
  2514. {
  2515. ext4_lblk_t ee_block;
  2516. struct ext4_extent *ex;
  2517. unsigned int ee_len, depth;
  2518. int err = 0;
  2519. int uninitialized;
  2520. int split_flag1, flags1;
  2521. depth = ext_depth(inode);
  2522. ex = path[depth].p_ext;
  2523. ee_block = le32_to_cpu(ex->ee_block);
  2524. ee_len = ext4_ext_get_actual_len(ex);
  2525. uninitialized = ext4_ext_is_uninitialized(ex);
  2526. if (map->m_lblk + map->m_len < ee_block + ee_len) {
  2527. split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ?
  2528. EXT4_EXT_MAY_ZEROOUT : 0;
  2529. flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
  2530. if (uninitialized)
  2531. split_flag1 |= EXT4_EXT_MARK_UNINIT1 |
  2532. EXT4_EXT_MARK_UNINIT2;
  2533. err = ext4_split_extent_at(handle, inode, path,
  2534. map->m_lblk + map->m_len, split_flag1, flags1);
  2535. if (err)
  2536. goto out;
  2537. }
  2538. ext4_ext_drop_refs(path);
  2539. path = ext4_ext_find_extent(inode, map->m_lblk, path);
  2540. if (IS_ERR(path))
  2541. return PTR_ERR(path);
  2542. if (map->m_lblk >= ee_block) {
  2543. split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ?
  2544. EXT4_EXT_MAY_ZEROOUT : 0;
  2545. if (uninitialized)
  2546. split_flag1 |= EXT4_EXT_MARK_UNINIT1;
  2547. if (split_flag & EXT4_EXT_MARK_UNINIT2)
  2548. split_flag1 |= EXT4_EXT_MARK_UNINIT2;
  2549. err = ext4_split_extent_at(handle, inode, path,
  2550. map->m_lblk, split_flag1, flags);
  2551. if (err)
  2552. goto out;
  2553. }
  2554. ext4_ext_show_leaf(inode, path);
  2555. out:
  2556. return err ? err : map->m_len;
  2557. }
  2558. #define EXT4_EXT_ZERO_LEN 7
  2559. /*
  2560. * This function is called by ext4_ext_map_blocks() if someone tries to write
  2561. * to an uninitialized extent. It may result in splitting the uninitialized
  2562. * extent into multiple extents (up to three - one initialized and two
  2563. * uninitialized).
  2564. * There are three possibilities:
  2565. * a> There is no split required: Entire extent should be initialized
  2566. * b> Splits in two extents: Write is happening at either end of the extent
  2567. * c> Splits in three extents: Somone is writing in middle of the extent
  2568. */
  2569. static int ext4_ext_convert_to_initialized(handle_t *handle,
  2570. struct inode *inode,
  2571. struct ext4_map_blocks *map,
  2572. struct ext4_ext_path *path)
  2573. {
  2574. struct ext4_map_blocks split_map;
  2575. struct ext4_extent zero_ex;
  2576. struct ext4_extent *ex;
  2577. ext4_lblk_t ee_block, eof_block;
  2578. unsigned int allocated, ee_len, depth;
  2579. int err = 0;
  2580. int split_flag = 0;
  2581. ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
  2582. "block %llu, max_blocks %u\n", inode->i_ino,
  2583. (unsigned long long)map->m_lblk, map->m_len);
  2584. eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
  2585. inode->i_sb->s_blocksize_bits;
  2586. if (eof_block < map->m_lblk + map->m_len)
  2587. eof_block = map->m_lblk + map->m_len;
  2588. depth = ext_depth(inode);
  2589. ex = path[depth].p_ext;
  2590. ee_block = le32_to_cpu(ex->ee_block);
  2591. ee_len = ext4_ext_get_actual_len(ex);
  2592. allocated = ee_len - (map->m_lblk - ee_block);
  2593. WARN_ON(map->m_lblk < ee_block);
  2594. /*
  2595. * It is safe to convert extent to initialized via explicit
  2596. * zeroout only if extent is fully insde i_size or new_size.
  2597. */
  2598. split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
  2599. /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
  2600. if (ee_len <= 2*EXT4_EXT_ZERO_LEN &&
  2601. (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
  2602. err = ext4_ext_zeroout(inode, ex);
  2603. if (err)
  2604. goto out;
  2605. err = ext4_ext_get_access(handle, inode, path + depth);
  2606. if (err)
  2607. goto out;
  2608. ext4_ext_mark_initialized(ex);
  2609. ext4_ext_try_to_merge(inode, path, ex);
  2610. err = ext4_ext_dirty(handle, inode, path + depth);
  2611. goto out;
  2612. }
  2613. /*
  2614. * four cases:
  2615. * 1. split the extent into three extents.
  2616. * 2. split the extent into two extents, zeroout the first half.
  2617. * 3. split the extent into two extents, zeroout the second half.
  2618. * 4. split the extent into two extents with out zeroout.
  2619. */
  2620. split_map.m_lblk = map->m_lblk;
  2621. split_map.m_len = map->m_len;
  2622. if (allocated > map->m_len) {
  2623. if (allocated <= EXT4_EXT_ZERO_LEN &&
  2624. (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
  2625. /* case 3 */
  2626. zero_ex.ee_block =
  2627. cpu_to_le32(map->m_lblk);
  2628. zero_ex.ee_len = cpu_to_le16(allocated);
  2629. ext4_ext_store_pblock(&zero_ex,
  2630. ext4_ext_pblock(ex) + map->m_lblk - ee_block);
  2631. err = ext4_ext_zeroout(inode, &zero_ex);
  2632. if (err)
  2633. goto out;
  2634. split_map.m_lblk = map->m_lblk;
  2635. split_map.m_len = allocated;
  2636. } else if ((map->m_lblk - ee_block + map->m_len <
  2637. EXT4_EXT_ZERO_LEN) &&
  2638. (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
  2639. /* case 2 */
  2640. if (map->m_lblk != ee_block) {
  2641. zero_ex.ee_block = ex->ee_block;
  2642. zero_ex.ee_len = cpu_to_le16(map->m_lblk -
  2643. ee_block);
  2644. ext4_ext_store_pblock(&zero_ex,
  2645. ext4_ext_pblock(ex));
  2646. err = ext4_ext_zeroout(inode, &zero_ex);
  2647. if (err)
  2648. goto out;
  2649. }
  2650. split_map.m_lblk = ee_block;
  2651. split_map.m_len = map->m_lblk - ee_block + map->m_len;
  2652. allocated = map->m_len;
  2653. }
  2654. }
  2655. allocated = ext4_split_extent(handle, inode, path,
  2656. &split_map, split_flag, 0);
  2657. if (allocated < 0)
  2658. err = allocated;
  2659. out:
  2660. return err ? err : allocated;
  2661. }
  2662. /*
  2663. * This function is called by ext4_ext_map_blocks() from
  2664. * ext4_get_blocks_dio_write() when DIO to write
  2665. * to an uninitialized extent.
  2666. *
  2667. * Writing to an uninitialized extent may result in splitting the uninitialized
  2668. * extent into multiple /initialized uninitialized extents (up to three)
  2669. * There are three possibilities:
  2670. * a> There is no split required: Entire extent should be uninitialized
  2671. * b> Splits in two extents: Write is happening at either end of the extent
  2672. * c> Splits in three extents: Somone is writing in middle of the extent
  2673. *
  2674. * One of more index blocks maybe needed if the extent tree grow after
  2675. * the uninitialized extent split. To prevent ENOSPC occur at the IO
  2676. * complete, we need to split the uninitialized extent before DIO submit
  2677. * the IO. The uninitialized extent called at this time will be split
  2678. * into three uninitialized extent(at most). After IO complete, the part
  2679. * being filled will be convert to initialized by the end_io callback function
  2680. * via ext4_convert_unwritten_extents().
  2681. *
  2682. * Returns the size of uninitialized extent to be written on success.
  2683. */
  2684. static int ext4_split_unwritten_extents(handle_t *handle,
  2685. struct inode *inode,
  2686. struct ext4_map_blocks *map,
  2687. struct ext4_ext_path *path,
  2688. int flags)
  2689. {
  2690. ext4_lblk_t eof_block;
  2691. ext4_lblk_t ee_block;
  2692. struct ext4_extent *ex;
  2693. unsigned int ee_len;
  2694. int split_flag = 0, depth;
  2695. ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
  2696. "block %llu, max_blocks %u\n", inode->i_ino,
  2697. (unsigned long long)map->m_lblk, map->m_len);
  2698. eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
  2699. inode->i_sb->s_blocksize_bits;
  2700. if (eof_block < map->m_lblk + map->m_len)
  2701. eof_block = map->m_lblk + map->m_len;
  2702. /*
  2703. * It is safe to convert extent to initialized via explicit
  2704. * zeroout only if extent is fully insde i_size or new_size.
  2705. */
  2706. depth = ext_depth(inode);
  2707. ex = path[depth].p_ext;
  2708. ee_block = le32_to_cpu(ex->ee_block);
  2709. ee_len = ext4_ext_get_actual_len(ex);
  2710. split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
  2711. split_flag |= EXT4_EXT_MARK_UNINIT2;
  2712. flags |= EXT4_GET_BLOCKS_PRE_IO;
  2713. return ext4_split_extent(handle, inode, path, map, split_flag, flags);
  2714. }
  2715. static int ext4_convert_unwritten_extents_endio(handle_t *handle,
  2716. struct inode *inode,
  2717. struct ext4_ext_path *path)
  2718. {
  2719. struct ext4_extent *ex;
  2720. int depth;
  2721. int err = 0;
  2722. depth = ext_depth(inode);
  2723. ex = path[depth].p_ext;
  2724. ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
  2725. "block %llu, max_blocks %u\n", inode->i_ino,
  2726. (unsigned long long)le32_to_cpu(ex->ee_block),
  2727. ext4_ext_get_actual_len(ex));
  2728. err = ext4_ext_get_access(handle, inode, path + depth);
  2729. if (err)
  2730. goto out;
  2731. /* first mark the extent as initialized */
  2732. ext4_ext_mark_initialized(ex);
  2733. /* note: ext4_ext_correct_indexes() isn't needed here because
  2734. * borders are not changed
  2735. */
  2736. ext4_ext_try_to_merge(inode, path, ex);
  2737. /* Mark modified extent as dirty */
  2738. err = ext4_ext_dirty(handle, inode, path + depth);
  2739. out:
  2740. ext4_ext_show_leaf(inode, path);
  2741. return err;
  2742. }
  2743. static void unmap_underlying_metadata_blocks(struct block_device *bdev,
  2744. sector_t block, int count)
  2745. {
  2746. int i;
  2747. for (i = 0; i < count; i++)
  2748. unmap_underlying_metadata(bdev, block + i);
  2749. }
  2750. /*
  2751. * Handle EOFBLOCKS_FL flag, clearing it if necessary
  2752. */
  2753. static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
  2754. ext4_lblk_t lblk,
  2755. struct ext4_ext_path *path,
  2756. unsigned int len)
  2757. {
  2758. int i, depth;
  2759. struct ext4_extent_header *eh;
  2760. struct ext4_extent *last_ex;
  2761. if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
  2762. return 0;
  2763. depth = ext_depth(inode);
  2764. eh = path[depth].p_hdr;
  2765. if (unlikely(!eh->eh_entries)) {
  2766. EXT4_ERROR_INODE(inode, "eh->eh_entries == 0 and "
  2767. "EOFBLOCKS_FL set");
  2768. return -EIO;
  2769. }
  2770. last_ex = EXT_LAST_EXTENT(eh);
  2771. /*
  2772. * We should clear the EOFBLOCKS_FL flag if we are writing the
  2773. * last block in the last extent in the file. We test this by
  2774. * first checking to see if the caller to
  2775. * ext4_ext_get_blocks() was interested in the last block (or
  2776. * a block beyond the last block) in the current extent. If
  2777. * this turns out to be false, we can bail out from this
  2778. * function immediately.
  2779. */
  2780. if (lblk + len < le32_to_cpu(last_ex->ee_block) +
  2781. ext4_ext_get_actual_len(last_ex))
  2782. return 0;
  2783. /*
  2784. * If the caller does appear to be planning to write at or
  2785. * beyond the end of the current extent, we then test to see
  2786. * if the current extent is the last extent in the file, by
  2787. * checking to make sure it was reached via the rightmost node
  2788. * at each level of the tree.
  2789. */
  2790. for (i = depth-1; i >= 0; i--)
  2791. if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
  2792. return 0;
  2793. ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
  2794. return ext4_mark_inode_dirty(handle, inode);
  2795. }
  2796. static int
  2797. ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode,
  2798. struct ext4_map_blocks *map,
  2799. struct ext4_ext_path *path, int flags,
  2800. unsigned int allocated, ext4_fsblk_t newblock)
  2801. {
  2802. int ret = 0;
  2803. int err = 0;
  2804. ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
  2805. ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical"
  2806. "block %llu, max_blocks %u, flags %d, allocated %u",
  2807. inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
  2808. flags, allocated);
  2809. ext4_ext_show_leaf(inode, path);
  2810. /* get_block() before submit the IO, split the extent */
  2811. if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
  2812. ret = ext4_split_unwritten_extents(handle, inode, map,
  2813. path, flags);
  2814. /*
  2815. * Flag the inode(non aio case) or end_io struct (aio case)
  2816. * that this IO needs to conversion to written when IO is
  2817. * completed
  2818. */
  2819. if (io && !(io->flag & EXT4_IO_END_UNWRITTEN)) {
  2820. io->flag = EXT4_IO_END_UNWRITTEN;
  2821. atomic_inc(&EXT4_I(inode)->i_aiodio_unwritten);
  2822. } else
  2823. ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
  2824. if (ext4_should_dioread_nolock(inode))
  2825. map->m_flags |= EXT4_MAP_UNINIT;
  2826. goto out;
  2827. }
  2828. /* IO end_io complete, convert the filled extent to written */
  2829. if ((flags & EXT4_GET_BLOCKS_CONVERT)) {
  2830. ret = ext4_convert_unwritten_extents_endio(handle, inode,
  2831. path);
  2832. if (ret >= 0) {
  2833. ext4_update_inode_fsync_trans(handle, inode, 1);
  2834. err = check_eofblocks_fl(handle, inode, map->m_lblk,
  2835. path, map->m_len);
  2836. } else
  2837. err = ret;
  2838. goto out2;
  2839. }
  2840. /* buffered IO case */
  2841. /*
  2842. * repeat fallocate creation request
  2843. * we already have an unwritten extent
  2844. */
  2845. if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)
  2846. goto map_out;
  2847. /* buffered READ or buffered write_begin() lookup */
  2848. if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
  2849. /*
  2850. * We have blocks reserved already. We
  2851. * return allocated blocks so that delalloc
  2852. * won't do block reservation for us. But
  2853. * the buffer head will be unmapped so that
  2854. * a read from the block returns 0s.
  2855. */
  2856. map->m_flags |= EXT4_MAP_UNWRITTEN;
  2857. goto out1;
  2858. }
  2859. /* buffered write, writepage time, convert*/
  2860. ret = ext4_ext_convert_to_initialized(handle, inode, map, path);
  2861. if (ret >= 0) {
  2862. ext4_update_inode_fsync_trans(handle, inode, 1);
  2863. err = check_eofblocks_fl(handle, inode, map->m_lblk, path,
  2864. map->m_len);
  2865. if (err < 0)
  2866. goto out2;
  2867. }
  2868. out:
  2869. if (ret <= 0) {
  2870. err = ret;
  2871. goto out2;
  2872. } else
  2873. allocated = ret;
  2874. map->m_flags |= EXT4_MAP_NEW;
  2875. /*
  2876. * if we allocated more blocks than requested
  2877. * we need to make sure we unmap the extra block
  2878. * allocated. The actual needed block will get
  2879. * unmapped later when we find the buffer_head marked
  2880. * new.
  2881. */
  2882. if (allocated > map->m_len) {
  2883. unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
  2884. newblock + map->m_len,
  2885. allocated - map->m_len);
  2886. allocated = map->m_len;
  2887. }
  2888. /*
  2889. * If we have done fallocate with the offset that is already
  2890. * delayed allocated, we would have block reservation
  2891. * and quota reservation done in the delayed write path.
  2892. * But fallocate would have already updated quota and block
  2893. * count for this offset. So cancel these reservation
  2894. */
  2895. if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
  2896. ext4_da_update_reserve_space(inode, allocated, 0);
  2897. map_out:
  2898. map->m_flags |= EXT4_MAP_MAPPED;
  2899. out1:
  2900. if (allocated > map->m_len)
  2901. allocated = map->m_len;
  2902. ext4_ext_show_leaf(inode, path);
  2903. map->m_pblk = newblock;
  2904. map->m_len = allocated;
  2905. out2:
  2906. if (path) {
  2907. ext4_ext_drop_refs(path);
  2908. kfree(path);
  2909. }
  2910. return err ? err : allocated;
  2911. }
  2912. /*
  2913. * Block allocation/map/preallocation routine for extents based files
  2914. *
  2915. *
  2916. * Need to be called with
  2917. * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
  2918. * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
  2919. *
  2920. * return > 0, number of of blocks already mapped/allocated
  2921. * if create == 0 and these are pre-allocated blocks
  2922. * buffer head is unmapped
  2923. * otherwise blocks are mapped
  2924. *
  2925. * return = 0, if plain look up failed (blocks have not been allocated)
  2926. * buffer head is unmapped
  2927. *
  2928. * return < 0, error case.
  2929. */
  2930. int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
  2931. struct ext4_map_blocks *map, int flags)
  2932. {
  2933. struct ext4_ext_path *path = NULL;
  2934. struct ext4_extent newex, *ex;
  2935. ext4_fsblk_t newblock = 0;
  2936. int err = 0, depth, ret;
  2937. unsigned int allocated = 0;
  2938. unsigned int punched_out = 0;
  2939. unsigned int result = 0;
  2940. struct ext4_allocation_request ar;
  2941. ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
  2942. struct ext4_map_blocks punch_map;
  2943. ext_debug("blocks %u/%u requested for inode %lu\n",
  2944. map->m_lblk, map->m_len, inode->i_ino);
  2945. trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
  2946. /* check in cache */
  2947. if (!(flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) &&
  2948. ext4_ext_in_cache(inode, map->m_lblk, &newex)) {
  2949. if (!newex.ee_start_lo && !newex.ee_start_hi) {
  2950. if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
  2951. /*
  2952. * block isn't allocated yet and
  2953. * user doesn't want to allocate it
  2954. */
  2955. goto out2;
  2956. }
  2957. /* we should allocate requested block */
  2958. } else {
  2959. /* block is already allocated */
  2960. newblock = map->m_lblk
  2961. - le32_to_cpu(newex.ee_block)
  2962. + ext4_ext_pblock(&newex);
  2963. /* number of remaining blocks in the extent */
  2964. allocated = ext4_ext_get_actual_len(&newex) -
  2965. (map->m_lblk - le32_to_cpu(newex.ee_block));
  2966. goto out;
  2967. }
  2968. }
  2969. /* find extent for this block */
  2970. path = ext4_ext_find_extent(inode, map->m_lblk, NULL);
  2971. if (IS_ERR(path)) {
  2972. err = PTR_ERR(path);
  2973. path = NULL;
  2974. goto out2;
  2975. }
  2976. depth = ext_depth(inode);
  2977. /*
  2978. * consistent leaf must not be empty;
  2979. * this situation is possible, though, _during_ tree modification;
  2980. * this is why assert can't be put in ext4_ext_find_extent()
  2981. */
  2982. if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
  2983. EXT4_ERROR_INODE(inode, "bad extent address "
  2984. "lblock: %lu, depth: %d pblock %lld",
  2985. (unsigned long) map->m_lblk, depth,
  2986. path[depth].p_block);
  2987. err = -EIO;
  2988. goto out2;
  2989. }
  2990. ex = path[depth].p_ext;
  2991. if (ex) {
  2992. ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
  2993. ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
  2994. unsigned short ee_len;
  2995. /*
  2996. * Uninitialized extents are treated as holes, except that
  2997. * we split out initialized portions during a write.
  2998. */
  2999. ee_len = ext4_ext_get_actual_len(ex);
  3000. /* if found extent covers block, simply return it */
  3001. if (in_range(map->m_lblk, ee_block, ee_len)) {
  3002. newblock = map->m_lblk - ee_block + ee_start;
  3003. /* number of remaining blocks in the extent */
  3004. allocated = ee_len - (map->m_lblk - ee_block);
  3005. ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
  3006. ee_block, ee_len, newblock);
  3007. if ((flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) == 0) {
  3008. /*
  3009. * Do not put uninitialized extent
  3010. * in the cache
  3011. */
  3012. if (!ext4_ext_is_uninitialized(ex)) {
  3013. ext4_ext_put_in_cache(inode, ee_block,
  3014. ee_len, ee_start);
  3015. goto out;
  3016. }
  3017. ret = ext4_ext_handle_uninitialized_extents(
  3018. handle, inode, map, path, flags,
  3019. allocated, newblock);
  3020. return ret;
  3021. }
  3022. /*
  3023. * Punch out the map length, but only to the
  3024. * end of the extent
  3025. */
  3026. punched_out = allocated < map->m_len ?
  3027. allocated : map->m_len;
  3028. /*
  3029. * Sense extents need to be converted to
  3030. * uninitialized, they must fit in an
  3031. * uninitialized extent
  3032. */
  3033. if (punched_out > EXT_UNINIT_MAX_LEN)
  3034. punched_out = EXT_UNINIT_MAX_LEN;
  3035. punch_map.m_lblk = map->m_lblk;
  3036. punch_map.m_pblk = newblock;
  3037. punch_map.m_len = punched_out;
  3038. punch_map.m_flags = 0;
  3039. /* Check to see if the extent needs to be split */
  3040. if (punch_map.m_len != ee_len ||
  3041. punch_map.m_lblk != ee_block) {
  3042. ret = ext4_split_extent(handle, inode,
  3043. path, &punch_map, 0,
  3044. EXT4_GET_BLOCKS_PUNCH_OUT_EXT |
  3045. EXT4_GET_BLOCKS_PRE_IO);
  3046. if (ret < 0) {
  3047. err = ret;
  3048. goto out2;
  3049. }
  3050. /*
  3051. * find extent for the block at
  3052. * the start of the hole
  3053. */
  3054. ext4_ext_drop_refs(path);
  3055. kfree(path);
  3056. path = ext4_ext_find_extent(inode,
  3057. map->m_lblk, NULL);
  3058. if (IS_ERR(path)) {
  3059. err = PTR_ERR(path);
  3060. path = NULL;
  3061. goto out2;
  3062. }
  3063. depth = ext_depth(inode);
  3064. ex = path[depth].p_ext;
  3065. ee_len = ext4_ext_get_actual_len(ex);
  3066. ee_block = le32_to_cpu(ex->ee_block);
  3067. ee_start = ext4_ext_pblock(ex);
  3068. }
  3069. ext4_ext_mark_uninitialized(ex);
  3070. ext4_ext_invalidate_cache(inode);
  3071. err = ext4_ext_rm_leaf(handle, inode, path,
  3072. map->m_lblk, map->m_lblk + punched_out);
  3073. if (!err && path->p_hdr->eh_entries == 0) {
  3074. /*
  3075. * Punch hole freed all of this sub tree,
  3076. * so we need to correct eh_depth
  3077. */
  3078. err = ext4_ext_get_access(handle, inode, path);
  3079. if (err == 0) {
  3080. ext_inode_hdr(inode)->eh_depth = 0;
  3081. ext_inode_hdr(inode)->eh_max =
  3082. cpu_to_le16(ext4_ext_space_root(
  3083. inode, 0));
  3084. err = ext4_ext_dirty(
  3085. handle, inode, path);
  3086. }
  3087. }
  3088. goto out2;
  3089. }
  3090. }
  3091. /*
  3092. * requested block isn't allocated yet;
  3093. * we couldn't try to create block if create flag is zero
  3094. */
  3095. if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
  3096. /*
  3097. * put just found gap into cache to speed up
  3098. * subsequent requests
  3099. */
  3100. ext4_ext_put_gap_in_cache(inode, path, map->m_lblk);
  3101. goto out2;
  3102. }
  3103. /*
  3104. * Okay, we need to do block allocation.
  3105. */
  3106. /* find neighbour allocated blocks */
  3107. ar.lleft = map->m_lblk;
  3108. err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
  3109. if (err)
  3110. goto out2;
  3111. ar.lright = map->m_lblk;
  3112. err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
  3113. if (err)
  3114. goto out2;
  3115. /*
  3116. * See if request is beyond maximum number of blocks we can have in
  3117. * a single extent. For an initialized extent this limit is
  3118. * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
  3119. * EXT_UNINIT_MAX_LEN.
  3120. */
  3121. if (map->m_len > EXT_INIT_MAX_LEN &&
  3122. !(flags & EXT4_GET_BLOCKS_UNINIT_EXT))
  3123. map->m_len = EXT_INIT_MAX_LEN;
  3124. else if (map->m_len > EXT_UNINIT_MAX_LEN &&
  3125. (flags & EXT4_GET_BLOCKS_UNINIT_EXT))
  3126. map->m_len = EXT_UNINIT_MAX_LEN;
  3127. /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
  3128. newex.ee_block = cpu_to_le32(map->m_lblk);
  3129. newex.ee_len = cpu_to_le16(map->m_len);
  3130. err = ext4_ext_check_overlap(inode, &newex, path);
  3131. if (err)
  3132. allocated = ext4_ext_get_actual_len(&newex);
  3133. else
  3134. allocated = map->m_len;
  3135. /* allocate new block */
  3136. ar.inode = inode;
  3137. ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
  3138. ar.logical = map->m_lblk;
  3139. ar.len = allocated;
  3140. if (S_ISREG(inode->i_mode))
  3141. ar.flags = EXT4_MB_HINT_DATA;
  3142. else
  3143. /* disable in-core preallocation for non-regular files */
  3144. ar.flags = 0;
  3145. if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
  3146. ar.flags |= EXT4_MB_HINT_NOPREALLOC;
  3147. newblock = ext4_mb_new_blocks(handle, &ar, &err);
  3148. if (!newblock)
  3149. goto out2;
  3150. ext_debug("allocate new block: goal %llu, found %llu/%u\n",
  3151. ar.goal, newblock, allocated);
  3152. /* try to insert new extent into found leaf and return */
  3153. ext4_ext_store_pblock(&newex, newblock);
  3154. newex.ee_len = cpu_to_le16(ar.len);
  3155. /* Mark uninitialized */
  3156. if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){
  3157. ext4_ext_mark_uninitialized(&newex);
  3158. /*
  3159. * io_end structure was created for every IO write to an
  3160. * uninitialized extent. To avoid unnecessary conversion,
  3161. * here we flag the IO that really needs the conversion.
  3162. * For non asycn direct IO case, flag the inode state
  3163. * that we need to perform conversion when IO is done.
  3164. */
  3165. if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
  3166. if (io && !(io->flag & EXT4_IO_END_UNWRITTEN)) {
  3167. io->flag = EXT4_IO_END_UNWRITTEN;
  3168. atomic_inc(&EXT4_I(inode)->i_aiodio_unwritten);
  3169. } else
  3170. ext4_set_inode_state(inode,
  3171. EXT4_STATE_DIO_UNWRITTEN);
  3172. }
  3173. if (ext4_should_dioread_nolock(inode))
  3174. map->m_flags |= EXT4_MAP_UNINIT;
  3175. }
  3176. err = check_eofblocks_fl(handle, inode, map->m_lblk, path, ar.len);
  3177. if (!err)
  3178. err = ext4_ext_insert_extent(handle, inode, path,
  3179. &newex, flags);
  3180. if (err) {
  3181. int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ?
  3182. EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0;
  3183. /* free data blocks we just allocated */
  3184. /* not a good idea to call discard here directly,
  3185. * but otherwise we'd need to call it every free() */
  3186. ext4_discard_preallocations(inode);
  3187. ext4_free_blocks(handle, inode, NULL, ext4_ext_pblock(&newex),
  3188. ext4_ext_get_actual_len(&newex), fb_flags);
  3189. goto out2;
  3190. }
  3191. /* previous routine could use block we allocated */
  3192. newblock = ext4_ext_pblock(&newex);
  3193. allocated = ext4_ext_get_actual_len(&newex);
  3194. if (allocated > map->m_len)
  3195. allocated = map->m_len;
  3196. map->m_flags |= EXT4_MAP_NEW;
  3197. /*
  3198. * Update reserved blocks/metadata blocks after successful
  3199. * block allocation which had been deferred till now.
  3200. */
  3201. if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
  3202. ext4_da_update_reserve_space(inode, allocated, 1);
  3203. /*
  3204. * Cache the extent and update transaction to commit on fdatasync only
  3205. * when it is _not_ an uninitialized extent.
  3206. */
  3207. if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) {
  3208. ext4_ext_put_in_cache(inode, map->m_lblk, allocated, newblock);
  3209. ext4_update_inode_fsync_trans(handle, inode, 1);
  3210. } else
  3211. ext4_update_inode_fsync_trans(handle, inode, 0);
  3212. out:
  3213. if (allocated > map->m_len)
  3214. allocated = map->m_len;
  3215. ext4_ext_show_leaf(inode, path);
  3216. map->m_flags |= EXT4_MAP_MAPPED;
  3217. map->m_pblk = newblock;
  3218. map->m_len = allocated;
  3219. out2:
  3220. if (path) {
  3221. ext4_ext_drop_refs(path);
  3222. kfree(path);
  3223. }
  3224. trace_ext4_ext_map_blocks_exit(inode, map->m_lblk,
  3225. newblock, map->m_len, err ? err : allocated);
  3226. result = (flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) ?
  3227. punched_out : allocated;
  3228. return err ? err : result;
  3229. }
  3230. void ext4_ext_truncate(struct inode *inode)
  3231. {
  3232. struct address_space *mapping = inode->i_mapping;
  3233. struct super_block *sb = inode->i_sb;
  3234. ext4_lblk_t last_block;
  3235. handle_t *handle;
  3236. int err = 0;
  3237. /*
  3238. * finish any pending end_io work so we won't run the risk of
  3239. * converting any truncated blocks to initialized later
  3240. */
  3241. ext4_flush_completed_IO(inode);
  3242. /*
  3243. * probably first extent we're gonna free will be last in block
  3244. */
  3245. err = ext4_writepage_trans_blocks(inode);
  3246. handle = ext4_journal_start(inode, err);
  3247. if (IS_ERR(handle))
  3248. return;
  3249. if (inode->i_size & (sb->s_blocksize - 1))
  3250. ext4_block_truncate_page(handle, mapping, inode->i_size);
  3251. if (ext4_orphan_add(handle, inode))
  3252. goto out_stop;
  3253. down_write(&EXT4_I(inode)->i_data_sem);
  3254. ext4_ext_invalidate_cache(inode);
  3255. ext4_discard_preallocations(inode);
  3256. /*
  3257. * TODO: optimization is possible here.
  3258. * Probably we need not scan at all,
  3259. * because page truncation is enough.
  3260. */
  3261. /* we have to know where to truncate from in crash case */
  3262. EXT4_I(inode)->i_disksize = inode->i_size;
  3263. ext4_mark_inode_dirty(handle, inode);
  3264. last_block = (inode->i_size + sb->s_blocksize - 1)
  3265. >> EXT4_BLOCK_SIZE_BITS(sb);
  3266. err = ext4_ext_remove_space(inode, last_block);
  3267. /* In a multi-transaction truncate, we only make the final
  3268. * transaction synchronous.
  3269. */
  3270. if (IS_SYNC(inode))
  3271. ext4_handle_sync(handle);
  3272. up_write(&EXT4_I(inode)->i_data_sem);
  3273. out_stop:
  3274. /*
  3275. * If this was a simple ftruncate() and the file will remain alive,
  3276. * then we need to clear up the orphan record which we created above.
  3277. * However, if this was a real unlink then we were called by
  3278. * ext4_delete_inode(), and we allow that function to clean up the
  3279. * orphan info for us.
  3280. */
  3281. if (inode->i_nlink)
  3282. ext4_orphan_del(handle, inode);
  3283. inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
  3284. ext4_mark_inode_dirty(handle, inode);
  3285. ext4_journal_stop(handle);
  3286. }
  3287. static void ext4_falloc_update_inode(struct inode *inode,
  3288. int mode, loff_t new_size, int update_ctime)
  3289. {
  3290. struct timespec now;
  3291. if (update_ctime) {
  3292. now = current_fs_time(inode->i_sb);
  3293. if (!timespec_equal(&inode->i_ctime, &now))
  3294. inode->i_ctime = now;
  3295. }
  3296. /*
  3297. * Update only when preallocation was requested beyond
  3298. * the file size.
  3299. */
  3300. if (!(mode & FALLOC_FL_KEEP_SIZE)) {
  3301. if (new_size > i_size_read(inode))
  3302. i_size_write(inode, new_size);
  3303. if (new_size > EXT4_I(inode)->i_disksize)
  3304. ext4_update_i_disksize(inode, new_size);
  3305. } else {
  3306. /*
  3307. * Mark that we allocate beyond EOF so the subsequent truncate
  3308. * can proceed even if the new size is the same as i_size.
  3309. */
  3310. if (new_size > i_size_read(inode))
  3311. ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
  3312. }
  3313. }
  3314. /*
  3315. * preallocate space for a file. This implements ext4's fallocate file
  3316. * operation, which gets called from sys_fallocate system call.
  3317. * For block-mapped files, posix_fallocate should fall back to the method
  3318. * of writing zeroes to the required new blocks (the same behavior which is
  3319. * expected for file systems which do not support fallocate() system call).
  3320. */
  3321. long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  3322. {
  3323. struct inode *inode = file->f_path.dentry->d_inode;
  3324. handle_t *handle;
  3325. loff_t new_size;
  3326. unsigned int max_blocks;
  3327. int ret = 0;
  3328. int ret2 = 0;
  3329. int retries = 0;
  3330. struct ext4_map_blocks map;
  3331. unsigned int credits, blkbits = inode->i_blkbits;
  3332. /*
  3333. * currently supporting (pre)allocate mode for extent-based
  3334. * files _only_
  3335. */
  3336. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  3337. return -EOPNOTSUPP;
  3338. /* Return error if mode is not supported */
  3339. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  3340. return -EOPNOTSUPP;
  3341. if (mode & FALLOC_FL_PUNCH_HOLE)
  3342. return ext4_punch_hole(file, offset, len);
  3343. trace_ext4_fallocate_enter(inode, offset, len, mode);
  3344. map.m_lblk = offset >> blkbits;
  3345. /*
  3346. * We can't just convert len to max_blocks because
  3347. * If blocksize = 4096 offset = 3072 and len = 2048
  3348. */
  3349. max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
  3350. - map.m_lblk;
  3351. /*
  3352. * credits to insert 1 extent into extent tree
  3353. */
  3354. credits = ext4_chunk_trans_blocks(inode, max_blocks);
  3355. mutex_lock(&inode->i_mutex);
  3356. ret = inode_newsize_ok(inode, (len + offset));
  3357. if (ret) {
  3358. mutex_unlock(&inode->i_mutex);
  3359. trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
  3360. return ret;
  3361. }
  3362. retry:
  3363. while (ret >= 0 && ret < max_blocks) {
  3364. map.m_lblk = map.m_lblk + ret;
  3365. map.m_len = max_blocks = max_blocks - ret;
  3366. handle = ext4_journal_start(inode, credits);
  3367. if (IS_ERR(handle)) {
  3368. ret = PTR_ERR(handle);
  3369. break;
  3370. }
  3371. ret = ext4_map_blocks(handle, inode, &map,
  3372. EXT4_GET_BLOCKS_CREATE_UNINIT_EXT |
  3373. EXT4_GET_BLOCKS_NO_NORMALIZE);
  3374. if (ret <= 0) {
  3375. #ifdef EXT4FS_DEBUG
  3376. WARN_ON(ret <= 0);
  3377. printk(KERN_ERR "%s: ext4_ext_map_blocks "
  3378. "returned error inode#%lu, block=%u, "
  3379. "max_blocks=%u", __func__,
  3380. inode->i_ino, map.m_lblk, max_blocks);
  3381. #endif
  3382. ext4_mark_inode_dirty(handle, inode);
  3383. ret2 = ext4_journal_stop(handle);
  3384. break;
  3385. }
  3386. if ((map.m_lblk + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
  3387. blkbits) >> blkbits))
  3388. new_size = offset + len;
  3389. else
  3390. new_size = (map.m_lblk + ret) << blkbits;
  3391. ext4_falloc_update_inode(inode, mode, new_size,
  3392. (map.m_flags & EXT4_MAP_NEW));
  3393. ext4_mark_inode_dirty(handle, inode);
  3394. ret2 = ext4_journal_stop(handle);
  3395. if (ret2)
  3396. break;
  3397. }
  3398. if (ret == -ENOSPC &&
  3399. ext4_should_retry_alloc(inode->i_sb, &retries)) {
  3400. ret = 0;
  3401. goto retry;
  3402. }
  3403. mutex_unlock(&inode->i_mutex);
  3404. trace_ext4_fallocate_exit(inode, offset, max_blocks,
  3405. ret > 0 ? ret2 : ret);
  3406. return ret > 0 ? ret2 : ret;
  3407. }
  3408. /*
  3409. * This function convert a range of blocks to written extents
  3410. * The caller of this function will pass the start offset and the size.
  3411. * all unwritten extents within this range will be converted to
  3412. * written extents.
  3413. *
  3414. * This function is called from the direct IO end io call back
  3415. * function, to convert the fallocated extents after IO is completed.
  3416. * Returns 0 on success.
  3417. */
  3418. int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
  3419. ssize_t len)
  3420. {
  3421. handle_t *handle;
  3422. unsigned int max_blocks;
  3423. int ret = 0;
  3424. int ret2 = 0;
  3425. struct ext4_map_blocks map;
  3426. unsigned int credits, blkbits = inode->i_blkbits;
  3427. map.m_lblk = offset >> blkbits;
  3428. /*
  3429. * We can't just convert len to max_blocks because
  3430. * If blocksize = 4096 offset = 3072 and len = 2048
  3431. */
  3432. max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) -
  3433. map.m_lblk);
  3434. /*
  3435. * credits to insert 1 extent into extent tree
  3436. */
  3437. credits = ext4_chunk_trans_blocks(inode, max_blocks);
  3438. while (ret >= 0 && ret < max_blocks) {
  3439. map.m_lblk += ret;
  3440. map.m_len = (max_blocks -= ret);
  3441. handle = ext4_journal_start(inode, credits);
  3442. if (IS_ERR(handle)) {
  3443. ret = PTR_ERR(handle);
  3444. break;
  3445. }
  3446. ret = ext4_map_blocks(handle, inode, &map,
  3447. EXT4_GET_BLOCKS_IO_CONVERT_EXT);
  3448. if (ret <= 0) {
  3449. WARN_ON(ret <= 0);
  3450. printk(KERN_ERR "%s: ext4_ext_map_blocks "
  3451. "returned error inode#%lu, block=%u, "
  3452. "max_blocks=%u", __func__,
  3453. inode->i_ino, map.m_lblk, map.m_len);
  3454. }
  3455. ext4_mark_inode_dirty(handle, inode);
  3456. ret2 = ext4_journal_stop(handle);
  3457. if (ret <= 0 || ret2 )
  3458. break;
  3459. }
  3460. return ret > 0 ? ret2 : ret;
  3461. }
  3462. /*
  3463. * Callback function called for each extent to gather FIEMAP information.
  3464. */
  3465. static int ext4_ext_fiemap_cb(struct inode *inode, ext4_lblk_t next,
  3466. struct ext4_ext_cache *newex, struct ext4_extent *ex,
  3467. void *data)
  3468. {
  3469. __u64 logical;
  3470. __u64 physical;
  3471. __u64 length;
  3472. __u32 flags = 0;
  3473. int ret = 0;
  3474. struct fiemap_extent_info *fieinfo = data;
  3475. unsigned char blksize_bits;
  3476. blksize_bits = inode->i_sb->s_blocksize_bits;
  3477. logical = (__u64)newex->ec_block << blksize_bits;
  3478. if (newex->ec_start == 0) {
  3479. /*
  3480. * No extent in extent-tree contains block @newex->ec_start,
  3481. * then the block may stay in 1)a hole or 2)delayed-extent.
  3482. *
  3483. * Holes or delayed-extents are processed as follows.
  3484. * 1. lookup dirty pages with specified range in pagecache.
  3485. * If no page is got, then there is no delayed-extent and
  3486. * return with EXT_CONTINUE.
  3487. * 2. find the 1st mapped buffer,
  3488. * 3. check if the mapped buffer is both in the request range
  3489. * and a delayed buffer. If not, there is no delayed-extent,
  3490. * then return.
  3491. * 4. a delayed-extent is found, the extent will be collected.
  3492. */
  3493. ext4_lblk_t end = 0;
  3494. pgoff_t last_offset;
  3495. pgoff_t offset;
  3496. pgoff_t index;
  3497. pgoff_t start_index = 0;
  3498. struct page **pages = NULL;
  3499. struct buffer_head *bh = NULL;
  3500. struct buffer_head *head = NULL;
  3501. unsigned int nr_pages = PAGE_SIZE / sizeof(struct page *);
  3502. pages = kmalloc(PAGE_SIZE, GFP_KERNEL);
  3503. if (pages == NULL)
  3504. return -ENOMEM;
  3505. offset = logical >> PAGE_SHIFT;
  3506. repeat:
  3507. last_offset = offset;
  3508. head = NULL;
  3509. ret = find_get_pages_tag(inode->i_mapping, &offset,
  3510. PAGECACHE_TAG_DIRTY, nr_pages, pages);
  3511. if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
  3512. /* First time, try to find a mapped buffer. */
  3513. if (ret == 0) {
  3514. out:
  3515. for (index = 0; index < ret; index++)
  3516. page_cache_release(pages[index]);
  3517. /* just a hole. */
  3518. kfree(pages);
  3519. return EXT_CONTINUE;
  3520. }
  3521. index = 0;
  3522. next_page:
  3523. /* Try to find the 1st mapped buffer. */
  3524. end = ((__u64)pages[index]->index << PAGE_SHIFT) >>
  3525. blksize_bits;
  3526. if (!page_has_buffers(pages[index]))
  3527. goto out;
  3528. head = page_buffers(pages[index]);
  3529. if (!head)
  3530. goto out;
  3531. index++;
  3532. bh = head;
  3533. do {
  3534. if (end >= newex->ec_block +
  3535. newex->ec_len)
  3536. /* The buffer is out of
  3537. * the request range.
  3538. */
  3539. goto out;
  3540. if (buffer_mapped(bh) &&
  3541. end >= newex->ec_block) {
  3542. start_index = index - 1;
  3543. /* get the 1st mapped buffer. */
  3544. goto found_mapped_buffer;
  3545. }
  3546. bh = bh->b_this_page;
  3547. end++;
  3548. } while (bh != head);
  3549. /* No mapped buffer in the range found in this page,
  3550. * We need to look up next page.
  3551. */
  3552. if (index >= ret) {
  3553. /* There is no page left, but we need to limit
  3554. * newex->ec_len.
  3555. */
  3556. newex->ec_len = end - newex->ec_block;
  3557. goto out;
  3558. }
  3559. goto next_page;
  3560. } else {
  3561. /*Find contiguous delayed buffers. */
  3562. if (ret > 0 && pages[0]->index == last_offset)
  3563. head = page_buffers(pages[0]);
  3564. bh = head;
  3565. index = 1;
  3566. start_index = 0;
  3567. }
  3568. found_mapped_buffer:
  3569. if (bh != NULL && buffer_delay(bh)) {
  3570. /* 1st or contiguous delayed buffer found. */
  3571. if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
  3572. /*
  3573. * 1st delayed buffer found, record
  3574. * the start of extent.
  3575. */
  3576. flags |= FIEMAP_EXTENT_DELALLOC;
  3577. newex->ec_block = end;
  3578. logical = (__u64)end << blksize_bits;
  3579. }
  3580. /* Find contiguous delayed buffers. */
  3581. do {
  3582. if (!buffer_delay(bh))
  3583. goto found_delayed_extent;
  3584. bh = bh->b_this_page;
  3585. end++;
  3586. } while (bh != head);
  3587. for (; index < ret; index++) {
  3588. if (!page_has_buffers(pages[index])) {
  3589. bh = NULL;
  3590. break;
  3591. }
  3592. head = page_buffers(pages[index]);
  3593. if (!head) {
  3594. bh = NULL;
  3595. break;
  3596. }
  3597. if (pages[index]->index !=
  3598. pages[start_index]->index + index
  3599. - start_index) {
  3600. /* Blocks are not contiguous. */
  3601. bh = NULL;
  3602. break;
  3603. }
  3604. bh = head;
  3605. do {
  3606. if (!buffer_delay(bh))
  3607. /* Delayed-extent ends. */
  3608. goto found_delayed_extent;
  3609. bh = bh->b_this_page;
  3610. end++;
  3611. } while (bh != head);
  3612. }
  3613. } else if (!(flags & FIEMAP_EXTENT_DELALLOC))
  3614. /* a hole found. */
  3615. goto out;
  3616. found_delayed_extent:
  3617. newex->ec_len = min(end - newex->ec_block,
  3618. (ext4_lblk_t)EXT_INIT_MAX_LEN);
  3619. if (ret == nr_pages && bh != NULL &&
  3620. newex->ec_len < EXT_INIT_MAX_LEN &&
  3621. buffer_delay(bh)) {
  3622. /* Have not collected an extent and continue. */
  3623. for (index = 0; index < ret; index++)
  3624. page_cache_release(pages[index]);
  3625. goto repeat;
  3626. }
  3627. for (index = 0; index < ret; index++)
  3628. page_cache_release(pages[index]);
  3629. kfree(pages);
  3630. }
  3631. physical = (__u64)newex->ec_start << blksize_bits;
  3632. length = (__u64)newex->ec_len << blksize_bits;
  3633. if (ex && ext4_ext_is_uninitialized(ex))
  3634. flags |= FIEMAP_EXTENT_UNWRITTEN;
  3635. if (next == EXT_MAX_BLOCKS)
  3636. flags |= FIEMAP_EXTENT_LAST;
  3637. ret = fiemap_fill_next_extent(fieinfo, logical, physical,
  3638. length, flags);
  3639. if (ret < 0)
  3640. return ret;
  3641. if (ret == 1)
  3642. return EXT_BREAK;
  3643. return EXT_CONTINUE;
  3644. }
  3645. /* fiemap flags we can handle specified here */
  3646. #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
  3647. static int ext4_xattr_fiemap(struct inode *inode,
  3648. struct fiemap_extent_info *fieinfo)
  3649. {
  3650. __u64 physical = 0;
  3651. __u64 length;
  3652. __u32 flags = FIEMAP_EXTENT_LAST;
  3653. int blockbits = inode->i_sb->s_blocksize_bits;
  3654. int error = 0;
  3655. /* in-inode? */
  3656. if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
  3657. struct ext4_iloc iloc;
  3658. int offset; /* offset of xattr in inode */
  3659. error = ext4_get_inode_loc(inode, &iloc);
  3660. if (error)
  3661. return error;
  3662. physical = iloc.bh->b_blocknr << blockbits;
  3663. offset = EXT4_GOOD_OLD_INODE_SIZE +
  3664. EXT4_I(inode)->i_extra_isize;
  3665. physical += offset;
  3666. length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
  3667. flags |= FIEMAP_EXTENT_DATA_INLINE;
  3668. brelse(iloc.bh);
  3669. } else { /* external block */
  3670. physical = EXT4_I(inode)->i_file_acl << blockbits;
  3671. length = inode->i_sb->s_blocksize;
  3672. }
  3673. if (physical)
  3674. error = fiemap_fill_next_extent(fieinfo, 0, physical,
  3675. length, flags);
  3676. return (error < 0 ? error : 0);
  3677. }
  3678. /*
  3679. * ext4_ext_punch_hole
  3680. *
  3681. * Punches a hole of "length" bytes in a file starting
  3682. * at byte "offset"
  3683. *
  3684. * @inode: The inode of the file to punch a hole in
  3685. * @offset: The starting byte offset of the hole
  3686. * @length: The length of the hole
  3687. *
  3688. * Returns the number of blocks removed or negative on err
  3689. */
  3690. int ext4_ext_punch_hole(struct file *file, loff_t offset, loff_t length)
  3691. {
  3692. struct inode *inode = file->f_path.dentry->d_inode;
  3693. struct super_block *sb = inode->i_sb;
  3694. struct ext4_ext_cache cache_ex;
  3695. ext4_lblk_t first_block, last_block, num_blocks, iblock, max_blocks;
  3696. struct address_space *mapping = inode->i_mapping;
  3697. struct ext4_map_blocks map;
  3698. handle_t *handle;
  3699. loff_t first_block_offset, last_block_offset, block_len;
  3700. loff_t first_page, last_page, first_page_offset, last_page_offset;
  3701. int ret, credits, blocks_released, err = 0;
  3702. first_block = (offset + sb->s_blocksize - 1) >>
  3703. EXT4_BLOCK_SIZE_BITS(sb);
  3704. last_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
  3705. first_block_offset = first_block << EXT4_BLOCK_SIZE_BITS(sb);
  3706. last_block_offset = last_block << EXT4_BLOCK_SIZE_BITS(sb);
  3707. first_page = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  3708. last_page = (offset + length) >> PAGE_CACHE_SHIFT;
  3709. first_page_offset = first_page << PAGE_CACHE_SHIFT;
  3710. last_page_offset = last_page << PAGE_CACHE_SHIFT;
  3711. /*
  3712. * Write out all dirty pages to avoid race conditions
  3713. * Then release them.
  3714. */
  3715. if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
  3716. err = filemap_write_and_wait_range(mapping,
  3717. first_page_offset == 0 ? 0 : first_page_offset-1,
  3718. last_page_offset);
  3719. if (err)
  3720. return err;
  3721. }
  3722. /* Now release the pages */
  3723. if (last_page_offset > first_page_offset) {
  3724. truncate_inode_pages_range(mapping, first_page_offset,
  3725. last_page_offset-1);
  3726. }
  3727. /* finish any pending end_io work */
  3728. ext4_flush_completed_IO(inode);
  3729. credits = ext4_writepage_trans_blocks(inode);
  3730. handle = ext4_journal_start(inode, credits);
  3731. if (IS_ERR(handle))
  3732. return PTR_ERR(handle);
  3733. err = ext4_orphan_add(handle, inode);
  3734. if (err)
  3735. goto out;
  3736. /*
  3737. * Now we need to zero out the un block aligned data.
  3738. * If the file is smaller than a block, just
  3739. * zero out the middle
  3740. */
  3741. if (first_block > last_block)
  3742. ext4_block_zero_page_range(handle, mapping, offset, length);
  3743. else {
  3744. /* zero out the head of the hole before the first block */
  3745. block_len = first_block_offset - offset;
  3746. if (block_len > 0)
  3747. ext4_block_zero_page_range(handle, mapping,
  3748. offset, block_len);
  3749. /* zero out the tail of the hole after the last block */
  3750. block_len = offset + length - last_block_offset;
  3751. if (block_len > 0) {
  3752. ext4_block_zero_page_range(handle, mapping,
  3753. last_block_offset, block_len);
  3754. }
  3755. }
  3756. /* If there are no blocks to remove, return now */
  3757. if (first_block >= last_block)
  3758. goto out;
  3759. down_write(&EXT4_I(inode)->i_data_sem);
  3760. ext4_ext_invalidate_cache(inode);
  3761. ext4_discard_preallocations(inode);
  3762. /*
  3763. * Loop over all the blocks and identify blocks
  3764. * that need to be punched out
  3765. */
  3766. iblock = first_block;
  3767. blocks_released = 0;
  3768. while (iblock < last_block) {
  3769. max_blocks = last_block - iblock;
  3770. num_blocks = 1;
  3771. memset(&map, 0, sizeof(map));
  3772. map.m_lblk = iblock;
  3773. map.m_len = max_blocks;
  3774. ret = ext4_ext_map_blocks(handle, inode, &map,
  3775. EXT4_GET_BLOCKS_PUNCH_OUT_EXT);
  3776. if (ret > 0) {
  3777. blocks_released += ret;
  3778. num_blocks = ret;
  3779. } else if (ret == 0) {
  3780. /*
  3781. * If map blocks could not find the block,
  3782. * then it is in a hole. If the hole was
  3783. * not already cached, then map blocks should
  3784. * put it in the cache. So we can get the hole
  3785. * out of the cache
  3786. */
  3787. memset(&cache_ex, 0, sizeof(cache_ex));
  3788. if ((ext4_ext_check_cache(inode, iblock, &cache_ex)) &&
  3789. !cache_ex.ec_start) {
  3790. /* The hole is cached */
  3791. num_blocks = cache_ex.ec_block +
  3792. cache_ex.ec_len - iblock;
  3793. } else {
  3794. /* The block could not be identified */
  3795. err = -EIO;
  3796. break;
  3797. }
  3798. } else {
  3799. /* Map blocks error */
  3800. err = ret;
  3801. break;
  3802. }
  3803. if (num_blocks == 0) {
  3804. /* This condition should never happen */
  3805. ext_debug("Block lookup failed");
  3806. err = -EIO;
  3807. break;
  3808. }
  3809. iblock += num_blocks;
  3810. }
  3811. if (blocks_released > 0) {
  3812. ext4_ext_invalidate_cache(inode);
  3813. ext4_discard_preallocations(inode);
  3814. }
  3815. if (IS_SYNC(inode))
  3816. ext4_handle_sync(handle);
  3817. up_write(&EXT4_I(inode)->i_data_sem);
  3818. out:
  3819. ext4_orphan_del(handle, inode);
  3820. inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
  3821. ext4_mark_inode_dirty(handle, inode);
  3822. ext4_journal_stop(handle);
  3823. return err;
  3824. }
  3825. int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  3826. __u64 start, __u64 len)
  3827. {
  3828. ext4_lblk_t start_blk;
  3829. int error = 0;
  3830. /* fallback to generic here if not in extents fmt */
  3831. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  3832. return generic_block_fiemap(inode, fieinfo, start, len,
  3833. ext4_get_block);
  3834. if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
  3835. return -EBADR;
  3836. if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
  3837. error = ext4_xattr_fiemap(inode, fieinfo);
  3838. } else {
  3839. ext4_lblk_t len_blks;
  3840. __u64 last_blk;
  3841. start_blk = start >> inode->i_sb->s_blocksize_bits;
  3842. last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
  3843. if (last_blk >= EXT_MAX_BLOCKS)
  3844. last_blk = EXT_MAX_BLOCKS-1;
  3845. len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
  3846. /*
  3847. * Walk the extent tree gathering extent information.
  3848. * ext4_ext_fiemap_cb will push extents back to user.
  3849. */
  3850. error = ext4_ext_walk_space(inode, start_blk, len_blks,
  3851. ext4_ext_fiemap_cb, fieinfo);
  3852. }
  3853. return error;
  3854. }