extents.c 70 KB

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