extents.c 85 KB

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