extents.c 104 KB

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