extents.c 114 KB

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