extents.c 107 KB

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