extents.c 75 KB

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