alloc.c 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * alloc.c
  5. *
  6. * Extent allocs and frees
  7. *
  8. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. * Boston, MA 021110-1307, USA.
  24. */
  25. #include <linux/fs.h>
  26. #include <linux/types.h>
  27. #include <linux/slab.h>
  28. #include <linux/highmem.h>
  29. #include <linux/swap.h>
  30. #define MLOG_MASK_PREFIX ML_DISK_ALLOC
  31. #include <cluster/masklog.h>
  32. #include "ocfs2.h"
  33. #include "alloc.h"
  34. #include "aops.h"
  35. #include "dlmglue.h"
  36. #include "extent_map.h"
  37. #include "inode.h"
  38. #include "journal.h"
  39. #include "localalloc.h"
  40. #include "suballoc.h"
  41. #include "sysfile.h"
  42. #include "file.h"
  43. #include "super.h"
  44. #include "uptodate.h"
  45. #include "buffer_head_io.h"
  46. static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
  47. /*
  48. * Structures which describe a path through a btree, and functions to
  49. * manipulate them.
  50. *
  51. * The idea here is to be as generic as possible with the tree
  52. * manipulation code.
  53. */
  54. struct ocfs2_path_item {
  55. struct buffer_head *bh;
  56. struct ocfs2_extent_list *el;
  57. };
  58. #define OCFS2_MAX_PATH_DEPTH 5
  59. struct ocfs2_path {
  60. int p_tree_depth;
  61. struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
  62. };
  63. #define path_root_bh(_path) ((_path)->p_node[0].bh)
  64. #define path_root_el(_path) ((_path)->p_node[0].el)
  65. #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
  66. #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
  67. #define path_num_items(_path) ((_path)->p_tree_depth + 1)
  68. /*
  69. * Reset the actual path elements so that we can re-use the structure
  70. * to build another path. Generally, this involves freeing the buffer
  71. * heads.
  72. */
  73. static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
  74. {
  75. int i, start = 0, depth = 0;
  76. struct ocfs2_path_item *node;
  77. if (keep_root)
  78. start = 1;
  79. for(i = start; i < path_num_items(path); i++) {
  80. node = &path->p_node[i];
  81. brelse(node->bh);
  82. node->bh = NULL;
  83. node->el = NULL;
  84. }
  85. /*
  86. * Tree depth may change during truncate, or insert. If we're
  87. * keeping the root extent list, then make sure that our path
  88. * structure reflects the proper depth.
  89. */
  90. if (keep_root)
  91. depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
  92. path->p_tree_depth = depth;
  93. }
  94. static void ocfs2_free_path(struct ocfs2_path *path)
  95. {
  96. if (path) {
  97. ocfs2_reinit_path(path, 0);
  98. kfree(path);
  99. }
  100. }
  101. /*
  102. * Make the *dest path the same as src and re-initialize src path to
  103. * have a root only.
  104. */
  105. static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
  106. {
  107. int i;
  108. BUG_ON(path_root_bh(dest) != path_root_bh(src));
  109. for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
  110. brelse(dest->p_node[i].bh);
  111. dest->p_node[i].bh = src->p_node[i].bh;
  112. dest->p_node[i].el = src->p_node[i].el;
  113. src->p_node[i].bh = NULL;
  114. src->p_node[i].el = NULL;
  115. }
  116. }
  117. /*
  118. * Insert an extent block at given index.
  119. *
  120. * This will not take an additional reference on eb_bh.
  121. */
  122. static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
  123. struct buffer_head *eb_bh)
  124. {
  125. struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
  126. /*
  127. * Right now, no root bh is an extent block, so this helps
  128. * catch code errors with dinode trees. The assertion can be
  129. * safely removed if we ever need to insert extent block
  130. * structures at the root.
  131. */
  132. BUG_ON(index == 0);
  133. path->p_node[index].bh = eb_bh;
  134. path->p_node[index].el = &eb->h_list;
  135. }
  136. static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
  137. struct ocfs2_extent_list *root_el)
  138. {
  139. struct ocfs2_path *path;
  140. BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
  141. path = kzalloc(sizeof(*path), GFP_NOFS);
  142. if (path) {
  143. path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
  144. get_bh(root_bh);
  145. path_root_bh(path) = root_bh;
  146. path_root_el(path) = root_el;
  147. }
  148. return path;
  149. }
  150. /*
  151. * Allocate and initialize a new path based on a disk inode tree.
  152. */
  153. static struct ocfs2_path *ocfs2_new_inode_path(struct buffer_head *di_bh)
  154. {
  155. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  156. struct ocfs2_extent_list *el = &di->id2.i_list;
  157. return ocfs2_new_path(di_bh, el);
  158. }
  159. /*
  160. * Convenience function to journal all components in a path.
  161. */
  162. static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle,
  163. struct ocfs2_path *path)
  164. {
  165. int i, ret = 0;
  166. if (!path)
  167. goto out;
  168. for(i = 0; i < path_num_items(path); i++) {
  169. ret = ocfs2_journal_access(handle, inode, path->p_node[i].bh,
  170. OCFS2_JOURNAL_ACCESS_WRITE);
  171. if (ret < 0) {
  172. mlog_errno(ret);
  173. goto out;
  174. }
  175. }
  176. out:
  177. return ret;
  178. }
  179. enum ocfs2_contig_type {
  180. CONTIG_NONE = 0,
  181. CONTIG_LEFT,
  182. CONTIG_RIGHT
  183. };
  184. /*
  185. * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
  186. * ocfs2_extent_contig only work properly against leaf nodes!
  187. */
  188. static int ocfs2_block_extent_contig(struct super_block *sb,
  189. struct ocfs2_extent_rec *ext,
  190. u64 blkno)
  191. {
  192. u64 blk_end = le64_to_cpu(ext->e_blkno);
  193. blk_end += ocfs2_clusters_to_blocks(sb,
  194. le16_to_cpu(ext->e_leaf_clusters));
  195. return blkno == blk_end;
  196. }
  197. static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
  198. struct ocfs2_extent_rec *right)
  199. {
  200. u32 left_range;
  201. left_range = le32_to_cpu(left->e_cpos) +
  202. le16_to_cpu(left->e_leaf_clusters);
  203. return (left_range == le32_to_cpu(right->e_cpos));
  204. }
  205. static enum ocfs2_contig_type
  206. ocfs2_extent_contig(struct inode *inode,
  207. struct ocfs2_extent_rec *ext,
  208. struct ocfs2_extent_rec *insert_rec)
  209. {
  210. u64 blkno = le64_to_cpu(insert_rec->e_blkno);
  211. if (ocfs2_extents_adjacent(ext, insert_rec) &&
  212. ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
  213. return CONTIG_RIGHT;
  214. blkno = le64_to_cpu(ext->e_blkno);
  215. if (ocfs2_extents_adjacent(insert_rec, ext) &&
  216. ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
  217. return CONTIG_LEFT;
  218. return CONTIG_NONE;
  219. }
  220. /*
  221. * NOTE: We can have pretty much any combination of contiguousness and
  222. * appending.
  223. *
  224. * The usefulness of APPEND_TAIL is more in that it lets us know that
  225. * we'll have to update the path to that leaf.
  226. */
  227. enum ocfs2_append_type {
  228. APPEND_NONE = 0,
  229. APPEND_TAIL,
  230. };
  231. struct ocfs2_insert_type {
  232. enum ocfs2_append_type ins_appending;
  233. enum ocfs2_contig_type ins_contig;
  234. int ins_contig_index;
  235. int ins_free_records;
  236. int ins_tree_depth;
  237. };
  238. /*
  239. * How many free extents have we got before we need more meta data?
  240. */
  241. int ocfs2_num_free_extents(struct ocfs2_super *osb,
  242. struct inode *inode,
  243. struct ocfs2_dinode *fe)
  244. {
  245. int retval;
  246. struct ocfs2_extent_list *el;
  247. struct ocfs2_extent_block *eb;
  248. struct buffer_head *eb_bh = NULL;
  249. mlog_entry_void();
  250. if (!OCFS2_IS_VALID_DINODE(fe)) {
  251. OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
  252. retval = -EIO;
  253. goto bail;
  254. }
  255. if (fe->i_last_eb_blk) {
  256. retval = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
  257. &eb_bh, OCFS2_BH_CACHED, inode);
  258. if (retval < 0) {
  259. mlog_errno(retval);
  260. goto bail;
  261. }
  262. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  263. el = &eb->h_list;
  264. } else
  265. el = &fe->id2.i_list;
  266. BUG_ON(el->l_tree_depth != 0);
  267. retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
  268. bail:
  269. if (eb_bh)
  270. brelse(eb_bh);
  271. mlog_exit(retval);
  272. return retval;
  273. }
  274. /* expects array to already be allocated
  275. *
  276. * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
  277. * l_count for you
  278. */
  279. static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
  280. handle_t *handle,
  281. struct inode *inode,
  282. int wanted,
  283. struct ocfs2_alloc_context *meta_ac,
  284. struct buffer_head *bhs[])
  285. {
  286. int count, status, i;
  287. u16 suballoc_bit_start;
  288. u32 num_got;
  289. u64 first_blkno;
  290. struct ocfs2_extent_block *eb;
  291. mlog_entry_void();
  292. count = 0;
  293. while (count < wanted) {
  294. status = ocfs2_claim_metadata(osb,
  295. handle,
  296. meta_ac,
  297. wanted - count,
  298. &suballoc_bit_start,
  299. &num_got,
  300. &first_blkno);
  301. if (status < 0) {
  302. mlog_errno(status);
  303. goto bail;
  304. }
  305. for(i = count; i < (num_got + count); i++) {
  306. bhs[i] = sb_getblk(osb->sb, first_blkno);
  307. if (bhs[i] == NULL) {
  308. status = -EIO;
  309. mlog_errno(status);
  310. goto bail;
  311. }
  312. ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
  313. status = ocfs2_journal_access(handle, inode, bhs[i],
  314. OCFS2_JOURNAL_ACCESS_CREATE);
  315. if (status < 0) {
  316. mlog_errno(status);
  317. goto bail;
  318. }
  319. memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
  320. eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
  321. /* Ok, setup the minimal stuff here. */
  322. strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
  323. eb->h_blkno = cpu_to_le64(first_blkno);
  324. eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
  325. #ifndef OCFS2_USE_ALL_METADATA_SUBALLOCATORS
  326. /* we always use slot zero's suballocator */
  327. eb->h_suballoc_slot = 0;
  328. #else
  329. eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
  330. #endif
  331. eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
  332. eb->h_list.l_count =
  333. cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
  334. suballoc_bit_start++;
  335. first_blkno++;
  336. /* We'll also be dirtied by the caller, so
  337. * this isn't absolutely necessary. */
  338. status = ocfs2_journal_dirty(handle, bhs[i]);
  339. if (status < 0) {
  340. mlog_errno(status);
  341. goto bail;
  342. }
  343. }
  344. count += num_got;
  345. }
  346. status = 0;
  347. bail:
  348. if (status < 0) {
  349. for(i = 0; i < wanted; i++) {
  350. if (bhs[i])
  351. brelse(bhs[i]);
  352. bhs[i] = NULL;
  353. }
  354. }
  355. mlog_exit(status);
  356. return status;
  357. }
  358. /*
  359. * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
  360. *
  361. * Returns the sum of the rightmost extent rec logical offset and
  362. * cluster count.
  363. *
  364. * ocfs2_add_branch() uses this to determine what logical cluster
  365. * value should be populated into the leftmost new branch records.
  366. *
  367. * ocfs2_shift_tree_depth() uses this to determine the # clusters
  368. * value for the new topmost tree record.
  369. */
  370. static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
  371. {
  372. int i;
  373. i = le16_to_cpu(el->l_next_free_rec) - 1;
  374. return le32_to_cpu(el->l_recs[i].e_cpos) +
  375. ocfs2_rec_clusters(el, &el->l_recs[i]);
  376. }
  377. /*
  378. * Add an entire tree branch to our inode. eb_bh is the extent block
  379. * to start at, if we don't want to start the branch at the dinode
  380. * structure.
  381. *
  382. * last_eb_bh is required as we have to update it's next_leaf pointer
  383. * for the new last extent block.
  384. *
  385. * the new branch will be 'empty' in the sense that every block will
  386. * contain a single record with cluster count == 0.
  387. */
  388. static int ocfs2_add_branch(struct ocfs2_super *osb,
  389. handle_t *handle,
  390. struct inode *inode,
  391. struct buffer_head *fe_bh,
  392. struct buffer_head *eb_bh,
  393. struct buffer_head *last_eb_bh,
  394. struct ocfs2_alloc_context *meta_ac)
  395. {
  396. int status, new_blocks, i;
  397. u64 next_blkno, new_last_eb_blk;
  398. struct buffer_head *bh;
  399. struct buffer_head **new_eb_bhs = NULL;
  400. struct ocfs2_dinode *fe;
  401. struct ocfs2_extent_block *eb;
  402. struct ocfs2_extent_list *eb_el;
  403. struct ocfs2_extent_list *el;
  404. u32 new_cpos;
  405. mlog_entry_void();
  406. BUG_ON(!last_eb_bh);
  407. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  408. if (eb_bh) {
  409. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  410. el = &eb->h_list;
  411. } else
  412. el = &fe->id2.i_list;
  413. /* we never add a branch to a leaf. */
  414. BUG_ON(!el->l_tree_depth);
  415. new_blocks = le16_to_cpu(el->l_tree_depth);
  416. /* allocate the number of new eb blocks we need */
  417. new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
  418. GFP_KERNEL);
  419. if (!new_eb_bhs) {
  420. status = -ENOMEM;
  421. mlog_errno(status);
  422. goto bail;
  423. }
  424. status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
  425. meta_ac, new_eb_bhs);
  426. if (status < 0) {
  427. mlog_errno(status);
  428. goto bail;
  429. }
  430. eb = (struct ocfs2_extent_block *)last_eb_bh->b_data;
  431. new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
  432. /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
  433. * linked with the rest of the tree.
  434. * conversly, new_eb_bhs[0] is the new bottommost leaf.
  435. *
  436. * when we leave the loop, new_last_eb_blk will point to the
  437. * newest leaf, and next_blkno will point to the topmost extent
  438. * block. */
  439. next_blkno = new_last_eb_blk = 0;
  440. for(i = 0; i < new_blocks; i++) {
  441. bh = new_eb_bhs[i];
  442. eb = (struct ocfs2_extent_block *) bh->b_data;
  443. if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
  444. OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
  445. status = -EIO;
  446. goto bail;
  447. }
  448. eb_el = &eb->h_list;
  449. status = ocfs2_journal_access(handle, inode, bh,
  450. OCFS2_JOURNAL_ACCESS_CREATE);
  451. if (status < 0) {
  452. mlog_errno(status);
  453. goto bail;
  454. }
  455. eb->h_next_leaf_blk = 0;
  456. eb_el->l_tree_depth = cpu_to_le16(i);
  457. eb_el->l_next_free_rec = cpu_to_le16(1);
  458. /*
  459. * This actually counts as an empty extent as
  460. * c_clusters == 0
  461. */
  462. eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
  463. eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
  464. /*
  465. * eb_el isn't always an interior node, but even leaf
  466. * nodes want a zero'd flags and reserved field so
  467. * this gets the whole 32 bits regardless of use.
  468. */
  469. eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
  470. if (!eb_el->l_tree_depth)
  471. new_last_eb_blk = le64_to_cpu(eb->h_blkno);
  472. status = ocfs2_journal_dirty(handle, bh);
  473. if (status < 0) {
  474. mlog_errno(status);
  475. goto bail;
  476. }
  477. next_blkno = le64_to_cpu(eb->h_blkno);
  478. }
  479. /* This is a bit hairy. We want to update up to three blocks
  480. * here without leaving any of them in an inconsistent state
  481. * in case of error. We don't have to worry about
  482. * journal_dirty erroring as it won't unless we've aborted the
  483. * handle (in which case we would never be here) so reserving
  484. * the write with journal_access is all we need to do. */
  485. status = ocfs2_journal_access(handle, inode, last_eb_bh,
  486. OCFS2_JOURNAL_ACCESS_WRITE);
  487. if (status < 0) {
  488. mlog_errno(status);
  489. goto bail;
  490. }
  491. status = ocfs2_journal_access(handle, inode, fe_bh,
  492. OCFS2_JOURNAL_ACCESS_WRITE);
  493. if (status < 0) {
  494. mlog_errno(status);
  495. goto bail;
  496. }
  497. if (eb_bh) {
  498. status = ocfs2_journal_access(handle, inode, eb_bh,
  499. OCFS2_JOURNAL_ACCESS_WRITE);
  500. if (status < 0) {
  501. mlog_errno(status);
  502. goto bail;
  503. }
  504. }
  505. /* Link the new branch into the rest of the tree (el will
  506. * either be on the fe, or the extent block passed in. */
  507. i = le16_to_cpu(el->l_next_free_rec);
  508. el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
  509. el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
  510. el->l_recs[i].e_int_clusters = 0;
  511. le16_add_cpu(&el->l_next_free_rec, 1);
  512. /* fe needs a new last extent block pointer, as does the
  513. * next_leaf on the previously last-extent-block. */
  514. fe->i_last_eb_blk = cpu_to_le64(new_last_eb_blk);
  515. eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
  516. eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
  517. status = ocfs2_journal_dirty(handle, last_eb_bh);
  518. if (status < 0)
  519. mlog_errno(status);
  520. status = ocfs2_journal_dirty(handle, fe_bh);
  521. if (status < 0)
  522. mlog_errno(status);
  523. if (eb_bh) {
  524. status = ocfs2_journal_dirty(handle, eb_bh);
  525. if (status < 0)
  526. mlog_errno(status);
  527. }
  528. status = 0;
  529. bail:
  530. if (new_eb_bhs) {
  531. for (i = 0; i < new_blocks; i++)
  532. if (new_eb_bhs[i])
  533. brelse(new_eb_bhs[i]);
  534. kfree(new_eb_bhs);
  535. }
  536. mlog_exit(status);
  537. return status;
  538. }
  539. /*
  540. * adds another level to the allocation tree.
  541. * returns back the new extent block so you can add a branch to it
  542. * after this call.
  543. */
  544. static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
  545. handle_t *handle,
  546. struct inode *inode,
  547. struct buffer_head *fe_bh,
  548. struct ocfs2_alloc_context *meta_ac,
  549. struct buffer_head **ret_new_eb_bh)
  550. {
  551. int status, i;
  552. u32 new_clusters;
  553. struct buffer_head *new_eb_bh = NULL;
  554. struct ocfs2_dinode *fe;
  555. struct ocfs2_extent_block *eb;
  556. struct ocfs2_extent_list *fe_el;
  557. struct ocfs2_extent_list *eb_el;
  558. mlog_entry_void();
  559. status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
  560. &new_eb_bh);
  561. if (status < 0) {
  562. mlog_errno(status);
  563. goto bail;
  564. }
  565. eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
  566. if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
  567. OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
  568. status = -EIO;
  569. goto bail;
  570. }
  571. eb_el = &eb->h_list;
  572. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  573. fe_el = &fe->id2.i_list;
  574. status = ocfs2_journal_access(handle, inode, new_eb_bh,
  575. OCFS2_JOURNAL_ACCESS_CREATE);
  576. if (status < 0) {
  577. mlog_errno(status);
  578. goto bail;
  579. }
  580. /* copy the fe data into the new extent block */
  581. eb_el->l_tree_depth = fe_el->l_tree_depth;
  582. eb_el->l_next_free_rec = fe_el->l_next_free_rec;
  583. for(i = 0; i < le16_to_cpu(fe_el->l_next_free_rec); i++)
  584. eb_el->l_recs[i] = fe_el->l_recs[i];
  585. status = ocfs2_journal_dirty(handle, new_eb_bh);
  586. if (status < 0) {
  587. mlog_errno(status);
  588. goto bail;
  589. }
  590. status = ocfs2_journal_access(handle, inode, fe_bh,
  591. OCFS2_JOURNAL_ACCESS_WRITE);
  592. if (status < 0) {
  593. mlog_errno(status);
  594. goto bail;
  595. }
  596. new_clusters = ocfs2_sum_rightmost_rec(eb_el);
  597. /* update fe now */
  598. le16_add_cpu(&fe_el->l_tree_depth, 1);
  599. fe_el->l_recs[0].e_cpos = 0;
  600. fe_el->l_recs[0].e_blkno = eb->h_blkno;
  601. fe_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
  602. for(i = 1; i < le16_to_cpu(fe_el->l_next_free_rec); i++)
  603. memset(&fe_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
  604. fe_el->l_next_free_rec = cpu_to_le16(1);
  605. /* If this is our 1st tree depth shift, then last_eb_blk
  606. * becomes the allocated extent block */
  607. if (fe_el->l_tree_depth == cpu_to_le16(1))
  608. fe->i_last_eb_blk = eb->h_blkno;
  609. status = ocfs2_journal_dirty(handle, fe_bh);
  610. if (status < 0) {
  611. mlog_errno(status);
  612. goto bail;
  613. }
  614. *ret_new_eb_bh = new_eb_bh;
  615. new_eb_bh = NULL;
  616. status = 0;
  617. bail:
  618. if (new_eb_bh)
  619. brelse(new_eb_bh);
  620. mlog_exit(status);
  621. return status;
  622. }
  623. /*
  624. * Should only be called when there is no space left in any of the
  625. * leaf nodes. What we want to do is find the lowest tree depth
  626. * non-leaf extent block with room for new records. There are three
  627. * valid results of this search:
  628. *
  629. * 1) a lowest extent block is found, then we pass it back in
  630. * *lowest_eb_bh and return '0'
  631. *
  632. * 2) the search fails to find anything, but the dinode has room. We
  633. * pass NULL back in *lowest_eb_bh, but still return '0'
  634. *
  635. * 3) the search fails to find anything AND the dinode is full, in
  636. * which case we return > 0
  637. *
  638. * return status < 0 indicates an error.
  639. */
  640. static int ocfs2_find_branch_target(struct ocfs2_super *osb,
  641. struct inode *inode,
  642. struct buffer_head *fe_bh,
  643. struct buffer_head **target_bh)
  644. {
  645. int status = 0, i;
  646. u64 blkno;
  647. struct ocfs2_dinode *fe;
  648. struct ocfs2_extent_block *eb;
  649. struct ocfs2_extent_list *el;
  650. struct buffer_head *bh = NULL;
  651. struct buffer_head *lowest_bh = NULL;
  652. mlog_entry_void();
  653. *target_bh = NULL;
  654. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  655. el = &fe->id2.i_list;
  656. while(le16_to_cpu(el->l_tree_depth) > 1) {
  657. if (le16_to_cpu(el->l_next_free_rec) == 0) {
  658. ocfs2_error(inode->i_sb, "Dinode %llu has empty "
  659. "extent list (next_free_rec == 0)",
  660. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  661. status = -EIO;
  662. goto bail;
  663. }
  664. i = le16_to_cpu(el->l_next_free_rec) - 1;
  665. blkno = le64_to_cpu(el->l_recs[i].e_blkno);
  666. if (!blkno) {
  667. ocfs2_error(inode->i_sb, "Dinode %llu has extent "
  668. "list where extent # %d has no physical "
  669. "block start",
  670. (unsigned long long)OCFS2_I(inode)->ip_blkno, i);
  671. status = -EIO;
  672. goto bail;
  673. }
  674. if (bh) {
  675. brelse(bh);
  676. bh = NULL;
  677. }
  678. status = ocfs2_read_block(osb, blkno, &bh, OCFS2_BH_CACHED,
  679. inode);
  680. if (status < 0) {
  681. mlog_errno(status);
  682. goto bail;
  683. }
  684. eb = (struct ocfs2_extent_block *) bh->b_data;
  685. if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
  686. OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
  687. status = -EIO;
  688. goto bail;
  689. }
  690. el = &eb->h_list;
  691. if (le16_to_cpu(el->l_next_free_rec) <
  692. le16_to_cpu(el->l_count)) {
  693. if (lowest_bh)
  694. brelse(lowest_bh);
  695. lowest_bh = bh;
  696. get_bh(lowest_bh);
  697. }
  698. }
  699. /* If we didn't find one and the fe doesn't have any room,
  700. * then return '1' */
  701. if (!lowest_bh
  702. && (fe->id2.i_list.l_next_free_rec == fe->id2.i_list.l_count))
  703. status = 1;
  704. *target_bh = lowest_bh;
  705. bail:
  706. if (bh)
  707. brelse(bh);
  708. mlog_exit(status);
  709. return status;
  710. }
  711. /*
  712. * This is only valid for leaf nodes, which are the only ones that can
  713. * have empty extents anyway.
  714. */
  715. static inline int ocfs2_is_empty_extent(struct ocfs2_extent_rec *rec)
  716. {
  717. return !rec->e_leaf_clusters;
  718. }
  719. /*
  720. * This function will discard the rightmost extent record.
  721. */
  722. static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
  723. {
  724. int next_free = le16_to_cpu(el->l_next_free_rec);
  725. int count = le16_to_cpu(el->l_count);
  726. unsigned int num_bytes;
  727. BUG_ON(!next_free);
  728. /* This will cause us to go off the end of our extent list. */
  729. BUG_ON(next_free >= count);
  730. num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
  731. memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
  732. }
  733. static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
  734. struct ocfs2_extent_rec *insert_rec)
  735. {
  736. int i, insert_index, next_free, has_empty, num_bytes;
  737. u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
  738. struct ocfs2_extent_rec *rec;
  739. next_free = le16_to_cpu(el->l_next_free_rec);
  740. has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
  741. BUG_ON(!next_free);
  742. /* The tree code before us didn't allow enough room in the leaf. */
  743. if (el->l_next_free_rec == el->l_count && !has_empty)
  744. BUG();
  745. /*
  746. * The easiest way to approach this is to just remove the
  747. * empty extent and temporarily decrement next_free.
  748. */
  749. if (has_empty) {
  750. /*
  751. * If next_free was 1 (only an empty extent), this
  752. * loop won't execute, which is fine. We still want
  753. * the decrement above to happen.
  754. */
  755. for(i = 0; i < (next_free - 1); i++)
  756. el->l_recs[i] = el->l_recs[i+1];
  757. next_free--;
  758. }
  759. /*
  760. * Figure out what the new record index should be.
  761. */
  762. for(i = 0; i < next_free; i++) {
  763. rec = &el->l_recs[i];
  764. if (insert_cpos < le32_to_cpu(rec->e_cpos))
  765. break;
  766. }
  767. insert_index = i;
  768. mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
  769. insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
  770. BUG_ON(insert_index < 0);
  771. BUG_ON(insert_index >= le16_to_cpu(el->l_count));
  772. BUG_ON(insert_index > next_free);
  773. /*
  774. * No need to memmove if we're just adding to the tail.
  775. */
  776. if (insert_index != next_free) {
  777. BUG_ON(next_free >= le16_to_cpu(el->l_count));
  778. num_bytes = next_free - insert_index;
  779. num_bytes *= sizeof(struct ocfs2_extent_rec);
  780. memmove(&el->l_recs[insert_index + 1],
  781. &el->l_recs[insert_index],
  782. num_bytes);
  783. }
  784. /*
  785. * Either we had an empty extent, and need to re-increment or
  786. * there was no empty extent on a non full rightmost leaf node,
  787. * in which case we still need to increment.
  788. */
  789. next_free++;
  790. el->l_next_free_rec = cpu_to_le16(next_free);
  791. /*
  792. * Make sure none of the math above just messed up our tree.
  793. */
  794. BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
  795. el->l_recs[insert_index] = *insert_rec;
  796. }
  797. /*
  798. * Create an empty extent record .
  799. *
  800. * l_next_free_rec may be updated.
  801. *
  802. * If an empty extent already exists do nothing.
  803. */
  804. static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
  805. {
  806. int next_free = le16_to_cpu(el->l_next_free_rec);
  807. BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
  808. if (next_free == 0)
  809. goto set_and_inc;
  810. if (ocfs2_is_empty_extent(&el->l_recs[0]))
  811. return;
  812. mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
  813. "Asked to create an empty extent in a full list:\n"
  814. "count = %u, tree depth = %u",
  815. le16_to_cpu(el->l_count),
  816. le16_to_cpu(el->l_tree_depth));
  817. ocfs2_shift_records_right(el);
  818. set_and_inc:
  819. le16_add_cpu(&el->l_next_free_rec, 1);
  820. memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
  821. }
  822. /*
  823. * For a rotation which involves two leaf nodes, the "root node" is
  824. * the lowest level tree node which contains a path to both leafs. This
  825. * resulting set of information can be used to form a complete "subtree"
  826. *
  827. * This function is passed two full paths from the dinode down to a
  828. * pair of adjacent leaves. It's task is to figure out which path
  829. * index contains the subtree root - this can be the root index itself
  830. * in a worst-case rotation.
  831. *
  832. * The array index of the subtree root is passed back.
  833. */
  834. static int ocfs2_find_subtree_root(struct inode *inode,
  835. struct ocfs2_path *left,
  836. struct ocfs2_path *right)
  837. {
  838. int i = 0;
  839. /*
  840. * Check that the caller passed in two paths from the same tree.
  841. */
  842. BUG_ON(path_root_bh(left) != path_root_bh(right));
  843. do {
  844. i++;
  845. /*
  846. * The caller didn't pass two adjacent paths.
  847. */
  848. mlog_bug_on_msg(i > left->p_tree_depth,
  849. "Inode %lu, left depth %u, right depth %u\n"
  850. "left leaf blk %llu, right leaf blk %llu\n",
  851. inode->i_ino, left->p_tree_depth,
  852. right->p_tree_depth,
  853. (unsigned long long)path_leaf_bh(left)->b_blocknr,
  854. (unsigned long long)path_leaf_bh(right)->b_blocknr);
  855. } while (left->p_node[i].bh->b_blocknr ==
  856. right->p_node[i].bh->b_blocknr);
  857. return i - 1;
  858. }
  859. typedef void (path_insert_t)(void *, struct buffer_head *);
  860. /*
  861. * Traverse a btree path in search of cpos, starting at root_el.
  862. *
  863. * This code can be called with a cpos larger than the tree, in which
  864. * case it will return the rightmost path.
  865. */
  866. static int __ocfs2_find_path(struct inode *inode,
  867. struct ocfs2_extent_list *root_el, u32 cpos,
  868. path_insert_t *func, void *data)
  869. {
  870. int i, ret = 0;
  871. u32 range;
  872. u64 blkno;
  873. struct buffer_head *bh = NULL;
  874. struct ocfs2_extent_block *eb;
  875. struct ocfs2_extent_list *el;
  876. struct ocfs2_extent_rec *rec;
  877. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  878. el = root_el;
  879. while (el->l_tree_depth) {
  880. if (le16_to_cpu(el->l_next_free_rec) == 0) {
  881. ocfs2_error(inode->i_sb,
  882. "Inode %llu has empty extent list at "
  883. "depth %u\n",
  884. (unsigned long long)oi->ip_blkno,
  885. le16_to_cpu(el->l_tree_depth));
  886. ret = -EROFS;
  887. goto out;
  888. }
  889. for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
  890. rec = &el->l_recs[i];
  891. /*
  892. * In the case that cpos is off the allocation
  893. * tree, this should just wind up returning the
  894. * rightmost record.
  895. */
  896. range = le32_to_cpu(rec->e_cpos) +
  897. ocfs2_rec_clusters(el, rec);
  898. if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
  899. break;
  900. }
  901. blkno = le64_to_cpu(el->l_recs[i].e_blkno);
  902. if (blkno == 0) {
  903. ocfs2_error(inode->i_sb,
  904. "Inode %llu has bad blkno in extent list "
  905. "at depth %u (index %d)\n",
  906. (unsigned long long)oi->ip_blkno,
  907. le16_to_cpu(el->l_tree_depth), i);
  908. ret = -EROFS;
  909. goto out;
  910. }
  911. brelse(bh);
  912. bh = NULL;
  913. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
  914. &bh, OCFS2_BH_CACHED, inode);
  915. if (ret) {
  916. mlog_errno(ret);
  917. goto out;
  918. }
  919. eb = (struct ocfs2_extent_block *) bh->b_data;
  920. el = &eb->h_list;
  921. if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
  922. OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
  923. ret = -EIO;
  924. goto out;
  925. }
  926. if (le16_to_cpu(el->l_next_free_rec) >
  927. le16_to_cpu(el->l_count)) {
  928. ocfs2_error(inode->i_sb,
  929. "Inode %llu has bad count in extent list "
  930. "at block %llu (next free=%u, count=%u)\n",
  931. (unsigned long long)oi->ip_blkno,
  932. (unsigned long long)bh->b_blocknr,
  933. le16_to_cpu(el->l_next_free_rec),
  934. le16_to_cpu(el->l_count));
  935. ret = -EROFS;
  936. goto out;
  937. }
  938. if (func)
  939. func(data, bh);
  940. }
  941. out:
  942. /*
  943. * Catch any trailing bh that the loop didn't handle.
  944. */
  945. brelse(bh);
  946. return ret;
  947. }
  948. /*
  949. * Given an initialized path (that is, it has a valid root extent
  950. * list), this function will traverse the btree in search of the path
  951. * which would contain cpos.
  952. *
  953. * The path traveled is recorded in the path structure.
  954. *
  955. * Note that this will not do any comparisons on leaf node extent
  956. * records, so it will work fine in the case that we just added a tree
  957. * branch.
  958. */
  959. struct find_path_data {
  960. int index;
  961. struct ocfs2_path *path;
  962. };
  963. static void find_path_ins(void *data, struct buffer_head *bh)
  964. {
  965. struct find_path_data *fp = data;
  966. get_bh(bh);
  967. ocfs2_path_insert_eb(fp->path, fp->index, bh);
  968. fp->index++;
  969. }
  970. static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
  971. u32 cpos)
  972. {
  973. struct find_path_data data;
  974. data.index = 1;
  975. data.path = path;
  976. return __ocfs2_find_path(inode, path_root_el(path), cpos,
  977. find_path_ins, &data);
  978. }
  979. static void find_leaf_ins(void *data, struct buffer_head *bh)
  980. {
  981. struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
  982. struct ocfs2_extent_list *el = &eb->h_list;
  983. struct buffer_head **ret = data;
  984. /* We want to retain only the leaf block. */
  985. if (le16_to_cpu(el->l_tree_depth) == 0) {
  986. get_bh(bh);
  987. *ret = bh;
  988. }
  989. }
  990. /*
  991. * Find the leaf block in the tree which would contain cpos. No
  992. * checking of the actual leaf is done.
  993. *
  994. * Some paths want to call this instead of allocating a path structure
  995. * and calling ocfs2_find_path().
  996. *
  997. * This function doesn't handle non btree extent lists.
  998. */
  999. int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
  1000. u32 cpos, struct buffer_head **leaf_bh)
  1001. {
  1002. int ret;
  1003. struct buffer_head *bh = NULL;
  1004. ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
  1005. if (ret) {
  1006. mlog_errno(ret);
  1007. goto out;
  1008. }
  1009. *leaf_bh = bh;
  1010. out:
  1011. return ret;
  1012. }
  1013. /*
  1014. * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
  1015. *
  1016. * Basically, we've moved stuff around at the bottom of the tree and
  1017. * we need to fix up the extent records above the changes to reflect
  1018. * the new changes.
  1019. *
  1020. * left_rec: the record on the left.
  1021. * left_child_el: is the child list pointed to by left_rec
  1022. * right_rec: the record to the right of left_rec
  1023. * right_child_el: is the child list pointed to by right_rec
  1024. *
  1025. * By definition, this only works on interior nodes.
  1026. */
  1027. static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
  1028. struct ocfs2_extent_list *left_child_el,
  1029. struct ocfs2_extent_rec *right_rec,
  1030. struct ocfs2_extent_list *right_child_el)
  1031. {
  1032. u32 left_clusters, right_end;
  1033. /*
  1034. * Interior nodes never have holes. Their cpos is the cpos of
  1035. * the leftmost record in their child list. Their cluster
  1036. * count covers the full theoretical range of their child list
  1037. * - the range between their cpos and the cpos of the record
  1038. * immediately to their right.
  1039. */
  1040. left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
  1041. left_clusters -= le32_to_cpu(left_rec->e_cpos);
  1042. left_rec->e_int_clusters = cpu_to_le32(left_clusters);
  1043. /*
  1044. * Calculate the rightmost cluster count boundary before
  1045. * moving cpos - we will need to adjust clusters after
  1046. * updating e_cpos to keep the same highest cluster count.
  1047. */
  1048. right_end = le32_to_cpu(right_rec->e_cpos);
  1049. right_end += le32_to_cpu(right_rec->e_int_clusters);
  1050. right_rec->e_cpos = left_rec->e_cpos;
  1051. le32_add_cpu(&right_rec->e_cpos, left_clusters);
  1052. right_end -= le32_to_cpu(right_rec->e_cpos);
  1053. right_rec->e_int_clusters = cpu_to_le32(right_end);
  1054. }
  1055. /*
  1056. * Adjust the adjacent root node records involved in a
  1057. * rotation. left_el_blkno is passed in as a key so that we can easily
  1058. * find it's index in the root list.
  1059. */
  1060. static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
  1061. struct ocfs2_extent_list *left_el,
  1062. struct ocfs2_extent_list *right_el,
  1063. u64 left_el_blkno)
  1064. {
  1065. int i;
  1066. BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
  1067. le16_to_cpu(left_el->l_tree_depth));
  1068. for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
  1069. if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
  1070. break;
  1071. }
  1072. /*
  1073. * The path walking code should have never returned a root and
  1074. * two paths which are not adjacent.
  1075. */
  1076. BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
  1077. ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
  1078. &root_el->l_recs[i + 1], right_el);
  1079. }
  1080. /*
  1081. * We've changed a leaf block (in right_path) and need to reflect that
  1082. * change back up the subtree.
  1083. *
  1084. * This happens in multiple places:
  1085. * - When we've moved an extent record from the left path leaf to the right
  1086. * path leaf to make room for an empty extent in the left path leaf.
  1087. * - When our insert into the right path leaf is at the leftmost edge
  1088. * and requires an update of the path immediately to it's left. This
  1089. * can occur at the end of some types of rotation and appending inserts.
  1090. */
  1091. static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
  1092. struct ocfs2_path *left_path,
  1093. struct ocfs2_path *right_path,
  1094. int subtree_index)
  1095. {
  1096. int ret, i, idx;
  1097. struct ocfs2_extent_list *el, *left_el, *right_el;
  1098. struct ocfs2_extent_rec *left_rec, *right_rec;
  1099. struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
  1100. /*
  1101. * Update the counts and position values within all the
  1102. * interior nodes to reflect the leaf rotation we just did.
  1103. *
  1104. * The root node is handled below the loop.
  1105. *
  1106. * We begin the loop with right_el and left_el pointing to the
  1107. * leaf lists and work our way up.
  1108. *
  1109. * NOTE: within this loop, left_el and right_el always refer
  1110. * to the *child* lists.
  1111. */
  1112. left_el = path_leaf_el(left_path);
  1113. right_el = path_leaf_el(right_path);
  1114. for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
  1115. mlog(0, "Adjust records at index %u\n", i);
  1116. /*
  1117. * One nice property of knowing that all of these
  1118. * nodes are below the root is that we only deal with
  1119. * the leftmost right node record and the rightmost
  1120. * left node record.
  1121. */
  1122. el = left_path->p_node[i].el;
  1123. idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
  1124. left_rec = &el->l_recs[idx];
  1125. el = right_path->p_node[i].el;
  1126. right_rec = &el->l_recs[0];
  1127. ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
  1128. right_el);
  1129. ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
  1130. if (ret)
  1131. mlog_errno(ret);
  1132. ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
  1133. if (ret)
  1134. mlog_errno(ret);
  1135. /*
  1136. * Setup our list pointers now so that the current
  1137. * parents become children in the next iteration.
  1138. */
  1139. left_el = left_path->p_node[i].el;
  1140. right_el = right_path->p_node[i].el;
  1141. }
  1142. /*
  1143. * At the root node, adjust the two adjacent records which
  1144. * begin our path to the leaves.
  1145. */
  1146. el = left_path->p_node[subtree_index].el;
  1147. left_el = left_path->p_node[subtree_index + 1].el;
  1148. right_el = right_path->p_node[subtree_index + 1].el;
  1149. ocfs2_adjust_root_records(el, left_el, right_el,
  1150. left_path->p_node[subtree_index + 1].bh->b_blocknr);
  1151. root_bh = left_path->p_node[subtree_index].bh;
  1152. ret = ocfs2_journal_dirty(handle, root_bh);
  1153. if (ret)
  1154. mlog_errno(ret);
  1155. }
  1156. static int ocfs2_rotate_subtree_right(struct inode *inode,
  1157. handle_t *handle,
  1158. struct ocfs2_path *left_path,
  1159. struct ocfs2_path *right_path,
  1160. int subtree_index)
  1161. {
  1162. int ret, i;
  1163. struct buffer_head *right_leaf_bh;
  1164. struct buffer_head *left_leaf_bh = NULL;
  1165. struct buffer_head *root_bh;
  1166. struct ocfs2_extent_list *right_el, *left_el;
  1167. struct ocfs2_extent_rec move_rec;
  1168. left_leaf_bh = path_leaf_bh(left_path);
  1169. left_el = path_leaf_el(left_path);
  1170. if (left_el->l_next_free_rec != left_el->l_count) {
  1171. ocfs2_error(inode->i_sb,
  1172. "Inode %llu has non-full interior leaf node %llu"
  1173. "(next free = %u)",
  1174. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1175. (unsigned long long)left_leaf_bh->b_blocknr,
  1176. le16_to_cpu(left_el->l_next_free_rec));
  1177. return -EROFS;
  1178. }
  1179. /*
  1180. * This extent block may already have an empty record, so we
  1181. * return early if so.
  1182. */
  1183. if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
  1184. return 0;
  1185. root_bh = left_path->p_node[subtree_index].bh;
  1186. BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
  1187. ret = ocfs2_journal_access(handle, inode, root_bh,
  1188. OCFS2_JOURNAL_ACCESS_WRITE);
  1189. if (ret) {
  1190. mlog_errno(ret);
  1191. goto out;
  1192. }
  1193. for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
  1194. ret = ocfs2_journal_access(handle, inode,
  1195. right_path->p_node[i].bh,
  1196. OCFS2_JOURNAL_ACCESS_WRITE);
  1197. if (ret) {
  1198. mlog_errno(ret);
  1199. goto out;
  1200. }
  1201. ret = ocfs2_journal_access(handle, inode,
  1202. left_path->p_node[i].bh,
  1203. OCFS2_JOURNAL_ACCESS_WRITE);
  1204. if (ret) {
  1205. mlog_errno(ret);
  1206. goto out;
  1207. }
  1208. }
  1209. right_leaf_bh = path_leaf_bh(right_path);
  1210. right_el = path_leaf_el(right_path);
  1211. /* This is a code error, not a disk corruption. */
  1212. mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
  1213. "because rightmost leaf block %llu is empty\n",
  1214. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1215. (unsigned long long)right_leaf_bh->b_blocknr);
  1216. ocfs2_create_empty_extent(right_el);
  1217. ret = ocfs2_journal_dirty(handle, right_leaf_bh);
  1218. if (ret) {
  1219. mlog_errno(ret);
  1220. goto out;
  1221. }
  1222. /* Do the copy now. */
  1223. i = le16_to_cpu(left_el->l_next_free_rec) - 1;
  1224. move_rec = left_el->l_recs[i];
  1225. right_el->l_recs[0] = move_rec;
  1226. /*
  1227. * Clear out the record we just copied and shift everything
  1228. * over, leaving an empty extent in the left leaf.
  1229. *
  1230. * We temporarily subtract from next_free_rec so that the
  1231. * shift will lose the tail record (which is now defunct).
  1232. */
  1233. le16_add_cpu(&left_el->l_next_free_rec, -1);
  1234. ocfs2_shift_records_right(left_el);
  1235. memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
  1236. le16_add_cpu(&left_el->l_next_free_rec, 1);
  1237. ret = ocfs2_journal_dirty(handle, left_leaf_bh);
  1238. if (ret) {
  1239. mlog_errno(ret);
  1240. goto out;
  1241. }
  1242. ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
  1243. subtree_index);
  1244. out:
  1245. return ret;
  1246. }
  1247. /*
  1248. * Given a full path, determine what cpos value would return us a path
  1249. * containing the leaf immediately to the left of the current one.
  1250. *
  1251. * Will return zero if the path passed in is already the leftmost path.
  1252. */
  1253. static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
  1254. struct ocfs2_path *path, u32 *cpos)
  1255. {
  1256. int i, j, ret = 0;
  1257. u64 blkno;
  1258. struct ocfs2_extent_list *el;
  1259. BUG_ON(path->p_tree_depth == 0);
  1260. *cpos = 0;
  1261. blkno = path_leaf_bh(path)->b_blocknr;
  1262. /* Start at the tree node just above the leaf and work our way up. */
  1263. i = path->p_tree_depth - 1;
  1264. while (i >= 0) {
  1265. el = path->p_node[i].el;
  1266. /*
  1267. * Find the extent record just before the one in our
  1268. * path.
  1269. */
  1270. for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
  1271. if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
  1272. if (j == 0) {
  1273. if (i == 0) {
  1274. /*
  1275. * We've determined that the
  1276. * path specified is already
  1277. * the leftmost one - return a
  1278. * cpos of zero.
  1279. */
  1280. goto out;
  1281. }
  1282. /*
  1283. * The leftmost record points to our
  1284. * leaf - we need to travel up the
  1285. * tree one level.
  1286. */
  1287. goto next_node;
  1288. }
  1289. *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
  1290. *cpos = *cpos + ocfs2_rec_clusters(el,
  1291. &el->l_recs[j - 1]);
  1292. *cpos = *cpos - 1;
  1293. goto out;
  1294. }
  1295. }
  1296. /*
  1297. * If we got here, we never found a valid node where
  1298. * the tree indicated one should be.
  1299. */
  1300. ocfs2_error(sb,
  1301. "Invalid extent tree at extent block %llu\n",
  1302. (unsigned long long)blkno);
  1303. ret = -EROFS;
  1304. goto out;
  1305. next_node:
  1306. blkno = path->p_node[i].bh->b_blocknr;
  1307. i--;
  1308. }
  1309. out:
  1310. return ret;
  1311. }
  1312. static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
  1313. struct ocfs2_path *path)
  1314. {
  1315. int credits = (path->p_tree_depth - subtree_depth) * 2 + 1;
  1316. if (handle->h_buffer_credits < credits)
  1317. return ocfs2_extend_trans(handle, credits);
  1318. return 0;
  1319. }
  1320. /*
  1321. * Trap the case where we're inserting into the theoretical range past
  1322. * the _actual_ left leaf range. Otherwise, we'll rotate a record
  1323. * whose cpos is less than ours into the right leaf.
  1324. *
  1325. * It's only necessary to look at the rightmost record of the left
  1326. * leaf because the logic that calls us should ensure that the
  1327. * theoretical ranges in the path components above the leaves are
  1328. * correct.
  1329. */
  1330. static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
  1331. u32 insert_cpos)
  1332. {
  1333. struct ocfs2_extent_list *left_el;
  1334. struct ocfs2_extent_rec *rec;
  1335. int next_free;
  1336. left_el = path_leaf_el(left_path);
  1337. next_free = le16_to_cpu(left_el->l_next_free_rec);
  1338. rec = &left_el->l_recs[next_free - 1];
  1339. if (insert_cpos > le32_to_cpu(rec->e_cpos))
  1340. return 1;
  1341. return 0;
  1342. }
  1343. /*
  1344. * Rotate all the records in a btree right one record, starting at insert_cpos.
  1345. *
  1346. * The path to the rightmost leaf should be passed in.
  1347. *
  1348. * The array is assumed to be large enough to hold an entire path (tree depth).
  1349. *
  1350. * Upon succesful return from this function:
  1351. *
  1352. * - The 'right_path' array will contain a path to the leaf block
  1353. * whose range contains e_cpos.
  1354. * - That leaf block will have a single empty extent in list index 0.
  1355. * - In the case that the rotation requires a post-insert update,
  1356. * *ret_left_path will contain a valid path which can be passed to
  1357. * ocfs2_insert_path().
  1358. */
  1359. static int ocfs2_rotate_tree_right(struct inode *inode,
  1360. handle_t *handle,
  1361. u32 insert_cpos,
  1362. struct ocfs2_path *right_path,
  1363. struct ocfs2_path **ret_left_path)
  1364. {
  1365. int ret, start;
  1366. u32 cpos;
  1367. struct ocfs2_path *left_path = NULL;
  1368. *ret_left_path = NULL;
  1369. left_path = ocfs2_new_path(path_root_bh(right_path),
  1370. path_root_el(right_path));
  1371. if (!left_path) {
  1372. ret = -ENOMEM;
  1373. mlog_errno(ret);
  1374. goto out;
  1375. }
  1376. ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
  1377. if (ret) {
  1378. mlog_errno(ret);
  1379. goto out;
  1380. }
  1381. mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
  1382. /*
  1383. * What we want to do here is:
  1384. *
  1385. * 1) Start with the rightmost path.
  1386. *
  1387. * 2) Determine a path to the leaf block directly to the left
  1388. * of that leaf.
  1389. *
  1390. * 3) Determine the 'subtree root' - the lowest level tree node
  1391. * which contains a path to both leaves.
  1392. *
  1393. * 4) Rotate the subtree.
  1394. *
  1395. * 5) Find the next subtree by considering the left path to be
  1396. * the new right path.
  1397. *
  1398. * The check at the top of this while loop also accepts
  1399. * insert_cpos == cpos because cpos is only a _theoretical_
  1400. * value to get us the left path - insert_cpos might very well
  1401. * be filling that hole.
  1402. *
  1403. * Stop at a cpos of '0' because we either started at the
  1404. * leftmost branch (i.e., a tree with one branch and a
  1405. * rotation inside of it), or we've gone as far as we can in
  1406. * rotating subtrees.
  1407. */
  1408. while (cpos && insert_cpos <= cpos) {
  1409. mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
  1410. insert_cpos, cpos);
  1411. ret = ocfs2_find_path(inode, left_path, cpos);
  1412. if (ret) {
  1413. mlog_errno(ret);
  1414. goto out;
  1415. }
  1416. mlog_bug_on_msg(path_leaf_bh(left_path) ==
  1417. path_leaf_bh(right_path),
  1418. "Inode %lu: error during insert of %u "
  1419. "(left path cpos %u) results in two identical "
  1420. "paths ending at %llu\n",
  1421. inode->i_ino, insert_cpos, cpos,
  1422. (unsigned long long)
  1423. path_leaf_bh(left_path)->b_blocknr);
  1424. if (ocfs2_rotate_requires_path_adjustment(left_path,
  1425. insert_cpos)) {
  1426. mlog(0, "Path adjustment required\n");
  1427. /*
  1428. * We've rotated the tree as much as we
  1429. * should. The rest is up to
  1430. * ocfs2_insert_path() to complete, after the
  1431. * record insertion. We indicate this
  1432. * situation by returning the left path.
  1433. *
  1434. * The reason we don't adjust the records here
  1435. * before the record insert is that an error
  1436. * later might break the rule where a parent
  1437. * record e_cpos will reflect the actual
  1438. * e_cpos of the 1st nonempty record of the
  1439. * child list.
  1440. */
  1441. *ret_left_path = left_path;
  1442. goto out_ret_path;
  1443. }
  1444. start = ocfs2_find_subtree_root(inode, left_path, right_path);
  1445. mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
  1446. start,
  1447. (unsigned long long) right_path->p_node[start].bh->b_blocknr,
  1448. right_path->p_tree_depth);
  1449. ret = ocfs2_extend_rotate_transaction(handle, start,
  1450. right_path);
  1451. if (ret) {
  1452. mlog_errno(ret);
  1453. goto out;
  1454. }
  1455. ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
  1456. right_path, start);
  1457. if (ret) {
  1458. mlog_errno(ret);
  1459. goto out;
  1460. }
  1461. /*
  1462. * There is no need to re-read the next right path
  1463. * as we know that it'll be our current left
  1464. * path. Optimize by copying values instead.
  1465. */
  1466. ocfs2_mv_path(right_path, left_path);
  1467. ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
  1468. &cpos);
  1469. if (ret) {
  1470. mlog_errno(ret);
  1471. goto out;
  1472. }
  1473. }
  1474. out:
  1475. ocfs2_free_path(left_path);
  1476. out_ret_path:
  1477. return ret;
  1478. }
  1479. /*
  1480. * Do the final bits of extent record insertion at the target leaf
  1481. * list. If this leaf is part of an allocation tree, it is assumed
  1482. * that the tree above has been prepared.
  1483. */
  1484. static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
  1485. struct ocfs2_extent_list *el,
  1486. struct ocfs2_insert_type *insert,
  1487. struct inode *inode)
  1488. {
  1489. int i = insert->ins_contig_index;
  1490. unsigned int range;
  1491. struct ocfs2_extent_rec *rec;
  1492. BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
  1493. /*
  1494. * Contiguous insert - either left or right.
  1495. */
  1496. if (insert->ins_contig != CONTIG_NONE) {
  1497. rec = &el->l_recs[i];
  1498. if (insert->ins_contig == CONTIG_LEFT) {
  1499. rec->e_blkno = insert_rec->e_blkno;
  1500. rec->e_cpos = insert_rec->e_cpos;
  1501. }
  1502. le16_add_cpu(&rec->e_leaf_clusters,
  1503. le16_to_cpu(insert_rec->e_leaf_clusters));
  1504. return;
  1505. }
  1506. /*
  1507. * Handle insert into an empty leaf.
  1508. */
  1509. if (le16_to_cpu(el->l_next_free_rec) == 0 ||
  1510. ((le16_to_cpu(el->l_next_free_rec) == 1) &&
  1511. ocfs2_is_empty_extent(&el->l_recs[0]))) {
  1512. el->l_recs[0] = *insert_rec;
  1513. el->l_next_free_rec = cpu_to_le16(1);
  1514. return;
  1515. }
  1516. /*
  1517. * Appending insert.
  1518. */
  1519. if (insert->ins_appending == APPEND_TAIL) {
  1520. i = le16_to_cpu(el->l_next_free_rec) - 1;
  1521. rec = &el->l_recs[i];
  1522. range = le32_to_cpu(rec->e_cpos)
  1523. + le16_to_cpu(rec->e_leaf_clusters);
  1524. BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
  1525. mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
  1526. le16_to_cpu(el->l_count),
  1527. "inode %lu, depth %u, count %u, next free %u, "
  1528. "rec.cpos %u, rec.clusters %u, "
  1529. "insert.cpos %u, insert.clusters %u\n",
  1530. inode->i_ino,
  1531. le16_to_cpu(el->l_tree_depth),
  1532. le16_to_cpu(el->l_count),
  1533. le16_to_cpu(el->l_next_free_rec),
  1534. le32_to_cpu(el->l_recs[i].e_cpos),
  1535. le16_to_cpu(el->l_recs[i].e_leaf_clusters),
  1536. le32_to_cpu(insert_rec->e_cpos),
  1537. le16_to_cpu(insert_rec->e_leaf_clusters));
  1538. i++;
  1539. el->l_recs[i] = *insert_rec;
  1540. le16_add_cpu(&el->l_next_free_rec, 1);
  1541. return;
  1542. }
  1543. /*
  1544. * Ok, we have to rotate.
  1545. *
  1546. * At this point, it is safe to assume that inserting into an
  1547. * empty leaf and appending to a leaf have both been handled
  1548. * above.
  1549. *
  1550. * This leaf needs to have space, either by the empty 1st
  1551. * extent record, or by virtue of an l_next_rec < l_count.
  1552. */
  1553. ocfs2_rotate_leaf(el, insert_rec);
  1554. }
  1555. static inline void ocfs2_update_dinode_clusters(struct inode *inode,
  1556. struct ocfs2_dinode *di,
  1557. u32 clusters)
  1558. {
  1559. le32_add_cpu(&di->i_clusters, clusters);
  1560. spin_lock(&OCFS2_I(inode)->ip_lock);
  1561. OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
  1562. spin_unlock(&OCFS2_I(inode)->ip_lock);
  1563. }
  1564. static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
  1565. struct ocfs2_extent_rec *insert_rec,
  1566. struct ocfs2_path *right_path,
  1567. struct ocfs2_path **ret_left_path)
  1568. {
  1569. int ret, i, next_free;
  1570. struct buffer_head *bh;
  1571. struct ocfs2_extent_list *el;
  1572. struct ocfs2_path *left_path = NULL;
  1573. *ret_left_path = NULL;
  1574. /*
  1575. * This shouldn't happen for non-trees. The extent rec cluster
  1576. * count manipulation below only works for interior nodes.
  1577. */
  1578. BUG_ON(right_path->p_tree_depth == 0);
  1579. /*
  1580. * If our appending insert is at the leftmost edge of a leaf,
  1581. * then we might need to update the rightmost records of the
  1582. * neighboring path.
  1583. */
  1584. el = path_leaf_el(right_path);
  1585. next_free = le16_to_cpu(el->l_next_free_rec);
  1586. if (next_free == 0 ||
  1587. (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
  1588. u32 left_cpos;
  1589. ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
  1590. &left_cpos);
  1591. if (ret) {
  1592. mlog_errno(ret);
  1593. goto out;
  1594. }
  1595. mlog(0, "Append may need a left path update. cpos: %u, "
  1596. "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
  1597. left_cpos);
  1598. /*
  1599. * No need to worry if the append is already in the
  1600. * leftmost leaf.
  1601. */
  1602. if (left_cpos) {
  1603. left_path = ocfs2_new_path(path_root_bh(right_path),
  1604. path_root_el(right_path));
  1605. if (!left_path) {
  1606. ret = -ENOMEM;
  1607. mlog_errno(ret);
  1608. goto out;
  1609. }
  1610. ret = ocfs2_find_path(inode, left_path, left_cpos);
  1611. if (ret) {
  1612. mlog_errno(ret);
  1613. goto out;
  1614. }
  1615. /*
  1616. * ocfs2_insert_path() will pass the left_path to the
  1617. * journal for us.
  1618. */
  1619. }
  1620. }
  1621. ret = ocfs2_journal_access_path(inode, handle, right_path);
  1622. if (ret) {
  1623. mlog_errno(ret);
  1624. goto out;
  1625. }
  1626. el = path_root_el(right_path);
  1627. bh = path_root_bh(right_path);
  1628. i = 0;
  1629. while (1) {
  1630. struct ocfs2_extent_rec *rec;
  1631. next_free = le16_to_cpu(el->l_next_free_rec);
  1632. if (next_free == 0) {
  1633. ocfs2_error(inode->i_sb,
  1634. "Dinode %llu has a bad extent list",
  1635. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1636. ret = -EIO;
  1637. goto out;
  1638. }
  1639. rec = &el->l_recs[next_free - 1];
  1640. rec->e_int_clusters = insert_rec->e_cpos;
  1641. le32_add_cpu(&rec->e_int_clusters,
  1642. le16_to_cpu(insert_rec->e_leaf_clusters));
  1643. le32_add_cpu(&rec->e_int_clusters,
  1644. -le32_to_cpu(rec->e_cpos));
  1645. ret = ocfs2_journal_dirty(handle, bh);
  1646. if (ret)
  1647. mlog_errno(ret);
  1648. /* Don't touch the leaf node */
  1649. if (++i >= right_path->p_tree_depth)
  1650. break;
  1651. bh = right_path->p_node[i].bh;
  1652. el = right_path->p_node[i].el;
  1653. }
  1654. *ret_left_path = left_path;
  1655. ret = 0;
  1656. out:
  1657. if (ret != 0)
  1658. ocfs2_free_path(left_path);
  1659. return ret;
  1660. }
  1661. /*
  1662. * This function only does inserts on an allocation b-tree. For dinode
  1663. * lists, ocfs2_insert_at_leaf() is called directly.
  1664. *
  1665. * right_path is the path we want to do the actual insert
  1666. * in. left_path should only be passed in if we need to update that
  1667. * portion of the tree after an edge insert.
  1668. */
  1669. static int ocfs2_insert_path(struct inode *inode,
  1670. handle_t *handle,
  1671. struct ocfs2_path *left_path,
  1672. struct ocfs2_path *right_path,
  1673. struct ocfs2_extent_rec *insert_rec,
  1674. struct ocfs2_insert_type *insert)
  1675. {
  1676. int ret, subtree_index;
  1677. struct buffer_head *leaf_bh = path_leaf_bh(right_path);
  1678. struct ocfs2_extent_list *el;
  1679. /*
  1680. * Pass both paths to the journal. The majority of inserts
  1681. * will be touching all components anyway.
  1682. */
  1683. ret = ocfs2_journal_access_path(inode, handle, right_path);
  1684. if (ret < 0) {
  1685. mlog_errno(ret);
  1686. goto out;
  1687. }
  1688. if (left_path) {
  1689. int credits = handle->h_buffer_credits;
  1690. /*
  1691. * There's a chance that left_path got passed back to
  1692. * us without being accounted for in the
  1693. * journal. Extend our transaction here to be sure we
  1694. * can change those blocks.
  1695. */
  1696. credits += left_path->p_tree_depth;
  1697. ret = ocfs2_extend_trans(handle, credits);
  1698. if (ret < 0) {
  1699. mlog_errno(ret);
  1700. goto out;
  1701. }
  1702. ret = ocfs2_journal_access_path(inode, handle, left_path);
  1703. if (ret < 0) {
  1704. mlog_errno(ret);
  1705. goto out;
  1706. }
  1707. }
  1708. el = path_leaf_el(right_path);
  1709. ocfs2_insert_at_leaf(insert_rec, el, insert, inode);
  1710. ret = ocfs2_journal_dirty(handle, leaf_bh);
  1711. if (ret)
  1712. mlog_errno(ret);
  1713. if (left_path) {
  1714. /*
  1715. * The rotate code has indicated that we need to fix
  1716. * up portions of the tree after the insert.
  1717. *
  1718. * XXX: Should we extend the transaction here?
  1719. */
  1720. subtree_index = ocfs2_find_subtree_root(inode, left_path,
  1721. right_path);
  1722. ocfs2_complete_edge_insert(inode, handle, left_path,
  1723. right_path, subtree_index);
  1724. }
  1725. ret = 0;
  1726. out:
  1727. return ret;
  1728. }
  1729. static int ocfs2_do_insert_extent(struct inode *inode,
  1730. handle_t *handle,
  1731. struct buffer_head *di_bh,
  1732. struct ocfs2_extent_rec *insert_rec,
  1733. struct ocfs2_insert_type *type)
  1734. {
  1735. int ret, rotate = 0;
  1736. u32 cpos;
  1737. struct ocfs2_path *right_path = NULL;
  1738. struct ocfs2_path *left_path = NULL;
  1739. struct ocfs2_dinode *di;
  1740. struct ocfs2_extent_list *el;
  1741. di = (struct ocfs2_dinode *) di_bh->b_data;
  1742. el = &di->id2.i_list;
  1743. ret = ocfs2_journal_access(handle, inode, di_bh,
  1744. OCFS2_JOURNAL_ACCESS_WRITE);
  1745. if (ret) {
  1746. mlog_errno(ret);
  1747. goto out;
  1748. }
  1749. if (le16_to_cpu(el->l_tree_depth) == 0) {
  1750. ocfs2_insert_at_leaf(insert_rec, el, type, inode);
  1751. goto out_update_clusters;
  1752. }
  1753. right_path = ocfs2_new_inode_path(di_bh);
  1754. if (!right_path) {
  1755. ret = -ENOMEM;
  1756. mlog_errno(ret);
  1757. goto out;
  1758. }
  1759. /*
  1760. * Determine the path to start with. Rotations need the
  1761. * rightmost path, everything else can go directly to the
  1762. * target leaf.
  1763. */
  1764. cpos = le32_to_cpu(insert_rec->e_cpos);
  1765. if (type->ins_appending == APPEND_NONE &&
  1766. type->ins_contig == CONTIG_NONE) {
  1767. rotate = 1;
  1768. cpos = UINT_MAX;
  1769. }
  1770. ret = ocfs2_find_path(inode, right_path, cpos);
  1771. if (ret) {
  1772. mlog_errno(ret);
  1773. goto out;
  1774. }
  1775. /*
  1776. * Rotations and appends need special treatment - they modify
  1777. * parts of the tree's above them.
  1778. *
  1779. * Both might pass back a path immediate to the left of the
  1780. * one being inserted to. This will be cause
  1781. * ocfs2_insert_path() to modify the rightmost records of
  1782. * left_path to account for an edge insert.
  1783. *
  1784. * XXX: When modifying this code, keep in mind that an insert
  1785. * can wind up skipping both of these two special cases...
  1786. */
  1787. if (rotate) {
  1788. ret = ocfs2_rotate_tree_right(inode, handle,
  1789. le32_to_cpu(insert_rec->e_cpos),
  1790. right_path, &left_path);
  1791. if (ret) {
  1792. mlog_errno(ret);
  1793. goto out;
  1794. }
  1795. } else if (type->ins_appending == APPEND_TAIL
  1796. && type->ins_contig != CONTIG_LEFT) {
  1797. ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
  1798. right_path, &left_path);
  1799. if (ret) {
  1800. mlog_errno(ret);
  1801. goto out;
  1802. }
  1803. }
  1804. ret = ocfs2_insert_path(inode, handle, left_path, right_path,
  1805. insert_rec, type);
  1806. if (ret) {
  1807. mlog_errno(ret);
  1808. goto out;
  1809. }
  1810. out_update_clusters:
  1811. ocfs2_update_dinode_clusters(inode, di,
  1812. le16_to_cpu(insert_rec->e_leaf_clusters));
  1813. ret = ocfs2_journal_dirty(handle, di_bh);
  1814. if (ret)
  1815. mlog_errno(ret);
  1816. out:
  1817. ocfs2_free_path(left_path);
  1818. ocfs2_free_path(right_path);
  1819. return ret;
  1820. }
  1821. static void ocfs2_figure_contig_type(struct inode *inode,
  1822. struct ocfs2_insert_type *insert,
  1823. struct ocfs2_extent_list *el,
  1824. struct ocfs2_extent_rec *insert_rec)
  1825. {
  1826. int i;
  1827. enum ocfs2_contig_type contig_type = CONTIG_NONE;
  1828. BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
  1829. for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
  1830. contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
  1831. insert_rec);
  1832. if (contig_type != CONTIG_NONE) {
  1833. insert->ins_contig_index = i;
  1834. break;
  1835. }
  1836. }
  1837. insert->ins_contig = contig_type;
  1838. }
  1839. /*
  1840. * This should only be called against the righmost leaf extent list.
  1841. *
  1842. * ocfs2_figure_appending_type() will figure out whether we'll have to
  1843. * insert at the tail of the rightmost leaf.
  1844. *
  1845. * This should also work against the dinode list for tree's with 0
  1846. * depth. If we consider the dinode list to be the rightmost leaf node
  1847. * then the logic here makes sense.
  1848. */
  1849. static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
  1850. struct ocfs2_extent_list *el,
  1851. struct ocfs2_extent_rec *insert_rec)
  1852. {
  1853. int i;
  1854. u32 cpos = le32_to_cpu(insert_rec->e_cpos);
  1855. struct ocfs2_extent_rec *rec;
  1856. insert->ins_appending = APPEND_NONE;
  1857. BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
  1858. if (!el->l_next_free_rec)
  1859. goto set_tail_append;
  1860. if (ocfs2_is_empty_extent(&el->l_recs[0])) {
  1861. /* Were all records empty? */
  1862. if (le16_to_cpu(el->l_next_free_rec) == 1)
  1863. goto set_tail_append;
  1864. }
  1865. i = le16_to_cpu(el->l_next_free_rec) - 1;
  1866. rec = &el->l_recs[i];
  1867. if (cpos >=
  1868. (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
  1869. goto set_tail_append;
  1870. return;
  1871. set_tail_append:
  1872. insert->ins_appending = APPEND_TAIL;
  1873. }
  1874. /*
  1875. * Helper function called at the begining of an insert.
  1876. *
  1877. * This computes a few things that are commonly used in the process of
  1878. * inserting into the btree:
  1879. * - Whether the new extent is contiguous with an existing one.
  1880. * - The current tree depth.
  1881. * - Whether the insert is an appending one.
  1882. * - The total # of free records in the tree.
  1883. *
  1884. * All of the information is stored on the ocfs2_insert_type
  1885. * structure.
  1886. */
  1887. static int ocfs2_figure_insert_type(struct inode *inode,
  1888. struct buffer_head *di_bh,
  1889. struct buffer_head **last_eb_bh,
  1890. struct ocfs2_extent_rec *insert_rec,
  1891. struct ocfs2_insert_type *insert)
  1892. {
  1893. int ret;
  1894. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1895. struct ocfs2_extent_block *eb;
  1896. struct ocfs2_extent_list *el;
  1897. struct ocfs2_path *path = NULL;
  1898. struct buffer_head *bh = NULL;
  1899. el = &di->id2.i_list;
  1900. insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
  1901. if (el->l_tree_depth) {
  1902. /*
  1903. * If we have tree depth, we read in the
  1904. * rightmost extent block ahead of time as
  1905. * ocfs2_figure_insert_type() and ocfs2_add_branch()
  1906. * may want it later.
  1907. */
  1908. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  1909. le64_to_cpu(di->i_last_eb_blk), &bh,
  1910. OCFS2_BH_CACHED, inode);
  1911. if (ret) {
  1912. mlog_exit(ret);
  1913. goto out;
  1914. }
  1915. eb = (struct ocfs2_extent_block *) bh->b_data;
  1916. el = &eb->h_list;
  1917. }
  1918. /*
  1919. * Unless we have a contiguous insert, we'll need to know if
  1920. * there is room left in our allocation tree for another
  1921. * extent record.
  1922. *
  1923. * XXX: This test is simplistic, we can search for empty
  1924. * extent records too.
  1925. */
  1926. insert->ins_free_records = le16_to_cpu(el->l_count) -
  1927. le16_to_cpu(el->l_next_free_rec);
  1928. if (!insert->ins_tree_depth) {
  1929. ocfs2_figure_contig_type(inode, insert, el, insert_rec);
  1930. ocfs2_figure_appending_type(insert, el, insert_rec);
  1931. return 0;
  1932. }
  1933. path = ocfs2_new_inode_path(di_bh);
  1934. if (!path) {
  1935. ret = -ENOMEM;
  1936. mlog_errno(ret);
  1937. goto out;
  1938. }
  1939. /*
  1940. * In the case that we're inserting past what the tree
  1941. * currently accounts for, ocfs2_find_path() will return for
  1942. * us the rightmost tree path. This is accounted for below in
  1943. * the appending code.
  1944. */
  1945. ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
  1946. if (ret) {
  1947. mlog_errno(ret);
  1948. goto out;
  1949. }
  1950. el = path_leaf_el(path);
  1951. /*
  1952. * Now that we have the path, there's two things we want to determine:
  1953. * 1) Contiguousness (also set contig_index if this is so)
  1954. *
  1955. * 2) Are we doing an append? We can trivially break this up
  1956. * into two types of appends: simple record append, or a
  1957. * rotate inside the tail leaf.
  1958. */
  1959. ocfs2_figure_contig_type(inode, insert, el, insert_rec);
  1960. /*
  1961. * The insert code isn't quite ready to deal with all cases of
  1962. * left contiguousness. Specifically, if it's an insert into
  1963. * the 1st record in a leaf, it will require the adjustment of
  1964. * cluster count on the last record of the path directly to it's
  1965. * left. For now, just catch that case and fool the layers
  1966. * above us. This works just fine for tree_depth == 0, which
  1967. * is why we allow that above.
  1968. */
  1969. if (insert->ins_contig == CONTIG_LEFT &&
  1970. insert->ins_contig_index == 0)
  1971. insert->ins_contig = CONTIG_NONE;
  1972. /*
  1973. * Ok, so we can simply compare against last_eb to figure out
  1974. * whether the path doesn't exist. This will only happen in
  1975. * the case that we're doing a tail append, so maybe we can
  1976. * take advantage of that information somehow.
  1977. */
  1978. if (le64_to_cpu(di->i_last_eb_blk) == path_leaf_bh(path)->b_blocknr) {
  1979. /*
  1980. * Ok, ocfs2_find_path() returned us the rightmost
  1981. * tree path. This might be an appending insert. There are
  1982. * two cases:
  1983. * 1) We're doing a true append at the tail:
  1984. * -This might even be off the end of the leaf
  1985. * 2) We're "appending" by rotating in the tail
  1986. */
  1987. ocfs2_figure_appending_type(insert, el, insert_rec);
  1988. }
  1989. out:
  1990. ocfs2_free_path(path);
  1991. if (ret == 0)
  1992. *last_eb_bh = bh;
  1993. else
  1994. brelse(bh);
  1995. return ret;
  1996. }
  1997. /*
  1998. * Insert an extent into an inode btree.
  1999. *
  2000. * The caller needs to update fe->i_clusters
  2001. */
  2002. int ocfs2_insert_extent(struct ocfs2_super *osb,
  2003. handle_t *handle,
  2004. struct inode *inode,
  2005. struct buffer_head *fe_bh,
  2006. u32 cpos,
  2007. u64 start_blk,
  2008. u32 new_clusters,
  2009. struct ocfs2_alloc_context *meta_ac)
  2010. {
  2011. int status, shift;
  2012. struct buffer_head *last_eb_bh = NULL;
  2013. struct buffer_head *bh = NULL;
  2014. struct ocfs2_insert_type insert = {0, };
  2015. struct ocfs2_extent_rec rec;
  2016. mlog(0, "add %u clusters at position %u to inode %llu\n",
  2017. new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
  2018. mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
  2019. (OCFS2_I(inode)->ip_clusters != cpos),
  2020. "Device %s, asking for sparse allocation: inode %llu, "
  2021. "cpos %u, clusters %u\n",
  2022. osb->dev_str,
  2023. (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos,
  2024. OCFS2_I(inode)->ip_clusters);
  2025. memset(&rec, 0, sizeof(rec));
  2026. rec.e_cpos = cpu_to_le32(cpos);
  2027. rec.e_blkno = cpu_to_le64(start_blk);
  2028. rec.e_leaf_clusters = cpu_to_le16(new_clusters);
  2029. status = ocfs2_figure_insert_type(inode, fe_bh, &last_eb_bh, &rec,
  2030. &insert);
  2031. if (status < 0) {
  2032. mlog_errno(status);
  2033. goto bail;
  2034. }
  2035. mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
  2036. "Insert.contig_index: %d, Insert.free_records: %d, "
  2037. "Insert.tree_depth: %d\n",
  2038. insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
  2039. insert.ins_free_records, insert.ins_tree_depth);
  2040. /*
  2041. * Avoid growing the tree unless we're out of records and the
  2042. * insert type requres one.
  2043. */
  2044. if (insert.ins_contig != CONTIG_NONE || insert.ins_free_records)
  2045. goto out_add;
  2046. shift = ocfs2_find_branch_target(osb, inode, fe_bh, &bh);
  2047. if (shift < 0) {
  2048. status = shift;
  2049. mlog_errno(status);
  2050. goto bail;
  2051. }
  2052. /* We traveled all the way to the bottom of the allocation tree
  2053. * and didn't find room for any more extents - we need to add
  2054. * another tree level */
  2055. if (shift) {
  2056. BUG_ON(bh);
  2057. mlog(0, "need to shift tree depth "
  2058. "(current = %d)\n", insert.ins_tree_depth);
  2059. /* ocfs2_shift_tree_depth will return us a buffer with
  2060. * the new extent block (so we can pass that to
  2061. * ocfs2_add_branch). */
  2062. status = ocfs2_shift_tree_depth(osb, handle, inode, fe_bh,
  2063. meta_ac, &bh);
  2064. if (status < 0) {
  2065. mlog_errno(status);
  2066. goto bail;
  2067. }
  2068. insert.ins_tree_depth++;
  2069. /* Special case: we have room now if we shifted from
  2070. * tree_depth 0 */
  2071. if (insert.ins_tree_depth == 1)
  2072. goto out_add;
  2073. }
  2074. /* call ocfs2_add_branch to add the final part of the tree with
  2075. * the new data. */
  2076. mlog(0, "add branch. bh = %p\n", bh);
  2077. status = ocfs2_add_branch(osb, handle, inode, fe_bh, bh, last_eb_bh,
  2078. meta_ac);
  2079. if (status < 0) {
  2080. mlog_errno(status);
  2081. goto bail;
  2082. }
  2083. out_add:
  2084. /* Finally, we can add clusters. This might rotate the tree for us. */
  2085. status = ocfs2_do_insert_extent(inode, handle, fe_bh, &rec, &insert);
  2086. if (status < 0)
  2087. mlog_errno(status);
  2088. else
  2089. ocfs2_extent_map_insert_rec(inode, &rec);
  2090. bail:
  2091. if (bh)
  2092. brelse(bh);
  2093. if (last_eb_bh)
  2094. brelse(last_eb_bh);
  2095. mlog_exit(status);
  2096. return status;
  2097. }
  2098. static inline int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
  2099. {
  2100. struct buffer_head *tl_bh = osb->osb_tl_bh;
  2101. struct ocfs2_dinode *di;
  2102. struct ocfs2_truncate_log *tl;
  2103. di = (struct ocfs2_dinode *) tl_bh->b_data;
  2104. tl = &di->id2.i_dealloc;
  2105. mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
  2106. "slot %d, invalid truncate log parameters: used = "
  2107. "%u, count = %u\n", osb->slot_num,
  2108. le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
  2109. return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
  2110. }
  2111. static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
  2112. unsigned int new_start)
  2113. {
  2114. unsigned int tail_index;
  2115. unsigned int current_tail;
  2116. /* No records, nothing to coalesce */
  2117. if (!le16_to_cpu(tl->tl_used))
  2118. return 0;
  2119. tail_index = le16_to_cpu(tl->tl_used) - 1;
  2120. current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
  2121. current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
  2122. return current_tail == new_start;
  2123. }
  2124. static int ocfs2_truncate_log_append(struct ocfs2_super *osb,
  2125. handle_t *handle,
  2126. u64 start_blk,
  2127. unsigned int num_clusters)
  2128. {
  2129. int status, index;
  2130. unsigned int start_cluster, tl_count;
  2131. struct inode *tl_inode = osb->osb_tl_inode;
  2132. struct buffer_head *tl_bh = osb->osb_tl_bh;
  2133. struct ocfs2_dinode *di;
  2134. struct ocfs2_truncate_log *tl;
  2135. mlog_entry("start_blk = %llu, num_clusters = %u\n",
  2136. (unsigned long long)start_blk, num_clusters);
  2137. BUG_ON(mutex_trylock(&tl_inode->i_mutex));
  2138. start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
  2139. di = (struct ocfs2_dinode *) tl_bh->b_data;
  2140. tl = &di->id2.i_dealloc;
  2141. if (!OCFS2_IS_VALID_DINODE(di)) {
  2142. OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
  2143. status = -EIO;
  2144. goto bail;
  2145. }
  2146. tl_count = le16_to_cpu(tl->tl_count);
  2147. mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
  2148. tl_count == 0,
  2149. "Truncate record count on #%llu invalid "
  2150. "wanted %u, actual %u\n",
  2151. (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
  2152. ocfs2_truncate_recs_per_inode(osb->sb),
  2153. le16_to_cpu(tl->tl_count));
  2154. /* Caller should have known to flush before calling us. */
  2155. index = le16_to_cpu(tl->tl_used);
  2156. if (index >= tl_count) {
  2157. status = -ENOSPC;
  2158. mlog_errno(status);
  2159. goto bail;
  2160. }
  2161. status = ocfs2_journal_access(handle, tl_inode, tl_bh,
  2162. OCFS2_JOURNAL_ACCESS_WRITE);
  2163. if (status < 0) {
  2164. mlog_errno(status);
  2165. goto bail;
  2166. }
  2167. mlog(0, "Log truncate of %u clusters starting at cluster %u to "
  2168. "%llu (index = %d)\n", num_clusters, start_cluster,
  2169. (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
  2170. if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
  2171. /*
  2172. * Move index back to the record we are coalescing with.
  2173. * ocfs2_truncate_log_can_coalesce() guarantees nonzero
  2174. */
  2175. index--;
  2176. num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
  2177. mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
  2178. index, le32_to_cpu(tl->tl_recs[index].t_start),
  2179. num_clusters);
  2180. } else {
  2181. tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
  2182. tl->tl_used = cpu_to_le16(index + 1);
  2183. }
  2184. tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
  2185. status = ocfs2_journal_dirty(handle, tl_bh);
  2186. if (status < 0) {
  2187. mlog_errno(status);
  2188. goto bail;
  2189. }
  2190. bail:
  2191. mlog_exit(status);
  2192. return status;
  2193. }
  2194. static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
  2195. handle_t *handle,
  2196. struct inode *data_alloc_inode,
  2197. struct buffer_head *data_alloc_bh)
  2198. {
  2199. int status = 0;
  2200. int i;
  2201. unsigned int num_clusters;
  2202. u64 start_blk;
  2203. struct ocfs2_truncate_rec rec;
  2204. struct ocfs2_dinode *di;
  2205. struct ocfs2_truncate_log *tl;
  2206. struct inode *tl_inode = osb->osb_tl_inode;
  2207. struct buffer_head *tl_bh = osb->osb_tl_bh;
  2208. mlog_entry_void();
  2209. di = (struct ocfs2_dinode *) tl_bh->b_data;
  2210. tl = &di->id2.i_dealloc;
  2211. i = le16_to_cpu(tl->tl_used) - 1;
  2212. while (i >= 0) {
  2213. /* Caller has given us at least enough credits to
  2214. * update the truncate log dinode */
  2215. status = ocfs2_journal_access(handle, tl_inode, tl_bh,
  2216. OCFS2_JOURNAL_ACCESS_WRITE);
  2217. if (status < 0) {
  2218. mlog_errno(status);
  2219. goto bail;
  2220. }
  2221. tl->tl_used = cpu_to_le16(i);
  2222. status = ocfs2_journal_dirty(handle, tl_bh);
  2223. if (status < 0) {
  2224. mlog_errno(status);
  2225. goto bail;
  2226. }
  2227. /* TODO: Perhaps we can calculate the bulk of the
  2228. * credits up front rather than extending like
  2229. * this. */
  2230. status = ocfs2_extend_trans(handle,
  2231. OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
  2232. if (status < 0) {
  2233. mlog_errno(status);
  2234. goto bail;
  2235. }
  2236. rec = tl->tl_recs[i];
  2237. start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
  2238. le32_to_cpu(rec.t_start));
  2239. num_clusters = le32_to_cpu(rec.t_clusters);
  2240. /* if start_blk is not set, we ignore the record as
  2241. * invalid. */
  2242. if (start_blk) {
  2243. mlog(0, "free record %d, start = %u, clusters = %u\n",
  2244. i, le32_to_cpu(rec.t_start), num_clusters);
  2245. status = ocfs2_free_clusters(handle, data_alloc_inode,
  2246. data_alloc_bh, start_blk,
  2247. num_clusters);
  2248. if (status < 0) {
  2249. mlog_errno(status);
  2250. goto bail;
  2251. }
  2252. }
  2253. i--;
  2254. }
  2255. bail:
  2256. mlog_exit(status);
  2257. return status;
  2258. }
  2259. /* Expects you to already be holding tl_inode->i_mutex */
  2260. static int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
  2261. {
  2262. int status;
  2263. unsigned int num_to_flush;
  2264. handle_t *handle;
  2265. struct inode *tl_inode = osb->osb_tl_inode;
  2266. struct inode *data_alloc_inode = NULL;
  2267. struct buffer_head *tl_bh = osb->osb_tl_bh;
  2268. struct buffer_head *data_alloc_bh = NULL;
  2269. struct ocfs2_dinode *di;
  2270. struct ocfs2_truncate_log *tl;
  2271. mlog_entry_void();
  2272. BUG_ON(mutex_trylock(&tl_inode->i_mutex));
  2273. di = (struct ocfs2_dinode *) tl_bh->b_data;
  2274. tl = &di->id2.i_dealloc;
  2275. if (!OCFS2_IS_VALID_DINODE(di)) {
  2276. OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
  2277. status = -EIO;
  2278. goto out;
  2279. }
  2280. num_to_flush = le16_to_cpu(tl->tl_used);
  2281. mlog(0, "Flush %u records from truncate log #%llu\n",
  2282. num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
  2283. if (!num_to_flush) {
  2284. status = 0;
  2285. goto out;
  2286. }
  2287. data_alloc_inode = ocfs2_get_system_file_inode(osb,
  2288. GLOBAL_BITMAP_SYSTEM_INODE,
  2289. OCFS2_INVALID_SLOT);
  2290. if (!data_alloc_inode) {
  2291. status = -EINVAL;
  2292. mlog(ML_ERROR, "Could not get bitmap inode!\n");
  2293. goto out;
  2294. }
  2295. mutex_lock(&data_alloc_inode->i_mutex);
  2296. status = ocfs2_meta_lock(data_alloc_inode, &data_alloc_bh, 1);
  2297. if (status < 0) {
  2298. mlog_errno(status);
  2299. goto out_mutex;
  2300. }
  2301. handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
  2302. if (IS_ERR(handle)) {
  2303. status = PTR_ERR(handle);
  2304. mlog_errno(status);
  2305. goto out_unlock;
  2306. }
  2307. status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
  2308. data_alloc_bh);
  2309. if (status < 0)
  2310. mlog_errno(status);
  2311. ocfs2_commit_trans(osb, handle);
  2312. out_unlock:
  2313. brelse(data_alloc_bh);
  2314. ocfs2_meta_unlock(data_alloc_inode, 1);
  2315. out_mutex:
  2316. mutex_unlock(&data_alloc_inode->i_mutex);
  2317. iput(data_alloc_inode);
  2318. out:
  2319. mlog_exit(status);
  2320. return status;
  2321. }
  2322. int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
  2323. {
  2324. int status;
  2325. struct inode *tl_inode = osb->osb_tl_inode;
  2326. mutex_lock(&tl_inode->i_mutex);
  2327. status = __ocfs2_flush_truncate_log(osb);
  2328. mutex_unlock(&tl_inode->i_mutex);
  2329. return status;
  2330. }
  2331. static void ocfs2_truncate_log_worker(struct work_struct *work)
  2332. {
  2333. int status;
  2334. struct ocfs2_super *osb =
  2335. container_of(work, struct ocfs2_super,
  2336. osb_truncate_log_wq.work);
  2337. mlog_entry_void();
  2338. status = ocfs2_flush_truncate_log(osb);
  2339. if (status < 0)
  2340. mlog_errno(status);
  2341. mlog_exit(status);
  2342. }
  2343. #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
  2344. void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
  2345. int cancel)
  2346. {
  2347. if (osb->osb_tl_inode) {
  2348. /* We want to push off log flushes while truncates are
  2349. * still running. */
  2350. if (cancel)
  2351. cancel_delayed_work(&osb->osb_truncate_log_wq);
  2352. queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
  2353. OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
  2354. }
  2355. }
  2356. static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
  2357. int slot_num,
  2358. struct inode **tl_inode,
  2359. struct buffer_head **tl_bh)
  2360. {
  2361. int status;
  2362. struct inode *inode = NULL;
  2363. struct buffer_head *bh = NULL;
  2364. inode = ocfs2_get_system_file_inode(osb,
  2365. TRUNCATE_LOG_SYSTEM_INODE,
  2366. slot_num);
  2367. if (!inode) {
  2368. status = -EINVAL;
  2369. mlog(ML_ERROR, "Could not get load truncate log inode!\n");
  2370. goto bail;
  2371. }
  2372. status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
  2373. OCFS2_BH_CACHED, inode);
  2374. if (status < 0) {
  2375. iput(inode);
  2376. mlog_errno(status);
  2377. goto bail;
  2378. }
  2379. *tl_inode = inode;
  2380. *tl_bh = bh;
  2381. bail:
  2382. mlog_exit(status);
  2383. return status;
  2384. }
  2385. /* called during the 1st stage of node recovery. we stamp a clean
  2386. * truncate log and pass back a copy for processing later. if the
  2387. * truncate log does not require processing, a *tl_copy is set to
  2388. * NULL. */
  2389. int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
  2390. int slot_num,
  2391. struct ocfs2_dinode **tl_copy)
  2392. {
  2393. int status;
  2394. struct inode *tl_inode = NULL;
  2395. struct buffer_head *tl_bh = NULL;
  2396. struct ocfs2_dinode *di;
  2397. struct ocfs2_truncate_log *tl;
  2398. *tl_copy = NULL;
  2399. mlog(0, "recover truncate log from slot %d\n", slot_num);
  2400. status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
  2401. if (status < 0) {
  2402. mlog_errno(status);
  2403. goto bail;
  2404. }
  2405. di = (struct ocfs2_dinode *) tl_bh->b_data;
  2406. tl = &di->id2.i_dealloc;
  2407. if (!OCFS2_IS_VALID_DINODE(di)) {
  2408. OCFS2_RO_ON_INVALID_DINODE(tl_inode->i_sb, di);
  2409. status = -EIO;
  2410. goto bail;
  2411. }
  2412. if (le16_to_cpu(tl->tl_used)) {
  2413. mlog(0, "We'll have %u logs to recover\n",
  2414. le16_to_cpu(tl->tl_used));
  2415. *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
  2416. if (!(*tl_copy)) {
  2417. status = -ENOMEM;
  2418. mlog_errno(status);
  2419. goto bail;
  2420. }
  2421. /* Assuming the write-out below goes well, this copy
  2422. * will be passed back to recovery for processing. */
  2423. memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
  2424. /* All we need to do to clear the truncate log is set
  2425. * tl_used. */
  2426. tl->tl_used = 0;
  2427. status = ocfs2_write_block(osb, tl_bh, tl_inode);
  2428. if (status < 0) {
  2429. mlog_errno(status);
  2430. goto bail;
  2431. }
  2432. }
  2433. bail:
  2434. if (tl_inode)
  2435. iput(tl_inode);
  2436. if (tl_bh)
  2437. brelse(tl_bh);
  2438. if (status < 0 && (*tl_copy)) {
  2439. kfree(*tl_copy);
  2440. *tl_copy = NULL;
  2441. }
  2442. mlog_exit(status);
  2443. return status;
  2444. }
  2445. int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
  2446. struct ocfs2_dinode *tl_copy)
  2447. {
  2448. int status = 0;
  2449. int i;
  2450. unsigned int clusters, num_recs, start_cluster;
  2451. u64 start_blk;
  2452. handle_t *handle;
  2453. struct inode *tl_inode = osb->osb_tl_inode;
  2454. struct ocfs2_truncate_log *tl;
  2455. mlog_entry_void();
  2456. if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
  2457. mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
  2458. return -EINVAL;
  2459. }
  2460. tl = &tl_copy->id2.i_dealloc;
  2461. num_recs = le16_to_cpu(tl->tl_used);
  2462. mlog(0, "cleanup %u records from %llu\n", num_recs,
  2463. (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
  2464. mutex_lock(&tl_inode->i_mutex);
  2465. for(i = 0; i < num_recs; i++) {
  2466. if (ocfs2_truncate_log_needs_flush(osb)) {
  2467. status = __ocfs2_flush_truncate_log(osb);
  2468. if (status < 0) {
  2469. mlog_errno(status);
  2470. goto bail_up;
  2471. }
  2472. }
  2473. handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
  2474. if (IS_ERR(handle)) {
  2475. status = PTR_ERR(handle);
  2476. mlog_errno(status);
  2477. goto bail_up;
  2478. }
  2479. clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
  2480. start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
  2481. start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
  2482. status = ocfs2_truncate_log_append(osb, handle,
  2483. start_blk, clusters);
  2484. ocfs2_commit_trans(osb, handle);
  2485. if (status < 0) {
  2486. mlog_errno(status);
  2487. goto bail_up;
  2488. }
  2489. }
  2490. bail_up:
  2491. mutex_unlock(&tl_inode->i_mutex);
  2492. mlog_exit(status);
  2493. return status;
  2494. }
  2495. void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
  2496. {
  2497. int status;
  2498. struct inode *tl_inode = osb->osb_tl_inode;
  2499. mlog_entry_void();
  2500. if (tl_inode) {
  2501. cancel_delayed_work(&osb->osb_truncate_log_wq);
  2502. flush_workqueue(ocfs2_wq);
  2503. status = ocfs2_flush_truncate_log(osb);
  2504. if (status < 0)
  2505. mlog_errno(status);
  2506. brelse(osb->osb_tl_bh);
  2507. iput(osb->osb_tl_inode);
  2508. }
  2509. mlog_exit_void();
  2510. }
  2511. int ocfs2_truncate_log_init(struct ocfs2_super *osb)
  2512. {
  2513. int status;
  2514. struct inode *tl_inode = NULL;
  2515. struct buffer_head *tl_bh = NULL;
  2516. mlog_entry_void();
  2517. status = ocfs2_get_truncate_log_info(osb,
  2518. osb->slot_num,
  2519. &tl_inode,
  2520. &tl_bh);
  2521. if (status < 0)
  2522. mlog_errno(status);
  2523. /* ocfs2_truncate_log_shutdown keys on the existence of
  2524. * osb->osb_tl_inode so we don't set any of the osb variables
  2525. * until we're sure all is well. */
  2526. INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
  2527. ocfs2_truncate_log_worker);
  2528. osb->osb_tl_bh = tl_bh;
  2529. osb->osb_tl_inode = tl_inode;
  2530. mlog_exit(status);
  2531. return status;
  2532. }
  2533. /* This function will figure out whether the currently last extent
  2534. * block will be deleted, and if it will, what the new last extent
  2535. * block will be so we can update his h_next_leaf_blk field, as well
  2536. * as the dinodes i_last_eb_blk */
  2537. static int ocfs2_find_new_last_ext_blk(struct inode *inode,
  2538. unsigned int clusters_to_del,
  2539. struct ocfs2_path *path,
  2540. struct buffer_head **new_last_eb)
  2541. {
  2542. int next_free, ret = 0;
  2543. u32 cpos;
  2544. struct ocfs2_extent_rec *rec;
  2545. struct ocfs2_extent_block *eb;
  2546. struct ocfs2_extent_list *el;
  2547. struct buffer_head *bh = NULL;
  2548. *new_last_eb = NULL;
  2549. /* we have no tree, so of course, no last_eb. */
  2550. if (!path->p_tree_depth)
  2551. goto out;
  2552. /* trunc to zero special case - this makes tree_depth = 0
  2553. * regardless of what it is. */
  2554. if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
  2555. goto out;
  2556. el = path_leaf_el(path);
  2557. BUG_ON(!el->l_next_free_rec);
  2558. /*
  2559. * Make sure that this extent list will actually be empty
  2560. * after we clear away the data. We can shortcut out if
  2561. * there's more than one non-empty extent in the
  2562. * list. Otherwise, a check of the remaining extent is
  2563. * necessary.
  2564. */
  2565. next_free = le16_to_cpu(el->l_next_free_rec);
  2566. rec = NULL;
  2567. if (ocfs2_is_empty_extent(&el->l_recs[0])) {
  2568. if (next_free > 2)
  2569. goto out;
  2570. /* We may have a valid extent in index 1, check it. */
  2571. if (next_free == 2)
  2572. rec = &el->l_recs[1];
  2573. /*
  2574. * Fall through - no more nonempty extents, so we want
  2575. * to delete this leaf.
  2576. */
  2577. } else {
  2578. if (next_free > 1)
  2579. goto out;
  2580. rec = &el->l_recs[0];
  2581. }
  2582. if (rec) {
  2583. /*
  2584. * Check it we'll only be trimming off the end of this
  2585. * cluster.
  2586. */
  2587. if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
  2588. goto out;
  2589. }
  2590. ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
  2591. if (ret) {
  2592. mlog_errno(ret);
  2593. goto out;
  2594. }
  2595. ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
  2596. if (ret) {
  2597. mlog_errno(ret);
  2598. goto out;
  2599. }
  2600. eb = (struct ocfs2_extent_block *) bh->b_data;
  2601. el = &eb->h_list;
  2602. if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
  2603. OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
  2604. ret = -EROFS;
  2605. goto out;
  2606. }
  2607. *new_last_eb = bh;
  2608. get_bh(*new_last_eb);
  2609. mlog(0, "returning block %llu, (cpos: %u)\n",
  2610. (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
  2611. out:
  2612. brelse(bh);
  2613. return ret;
  2614. }
  2615. /*
  2616. * Trim some clusters off the rightmost edge of a tree. Only called
  2617. * during truncate.
  2618. *
  2619. * The caller needs to:
  2620. * - start journaling of each path component.
  2621. * - compute and fully set up any new last ext block
  2622. */
  2623. static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
  2624. handle_t *handle, struct ocfs2_truncate_context *tc,
  2625. u32 clusters_to_del, u64 *delete_start)
  2626. {
  2627. int ret, i, index = path->p_tree_depth;
  2628. u32 new_edge = 0;
  2629. u64 deleted_eb = 0;
  2630. struct buffer_head *bh;
  2631. struct ocfs2_extent_list *el;
  2632. struct ocfs2_extent_rec *rec;
  2633. *delete_start = 0;
  2634. while (index >= 0) {
  2635. bh = path->p_node[index].bh;
  2636. el = path->p_node[index].el;
  2637. mlog(0, "traveling tree (index = %d, block = %llu)\n",
  2638. index, (unsigned long long)bh->b_blocknr);
  2639. BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
  2640. if (index !=
  2641. (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
  2642. ocfs2_error(inode->i_sb,
  2643. "Inode %lu has invalid ext. block %llu",
  2644. inode->i_ino,
  2645. (unsigned long long)bh->b_blocknr);
  2646. ret = -EROFS;
  2647. goto out;
  2648. }
  2649. find_tail_record:
  2650. i = le16_to_cpu(el->l_next_free_rec) - 1;
  2651. rec = &el->l_recs[i];
  2652. mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
  2653. "next = %u\n", i, le32_to_cpu(rec->e_cpos),
  2654. ocfs2_rec_clusters(el, rec),
  2655. (unsigned long long)le64_to_cpu(rec->e_blkno),
  2656. le16_to_cpu(el->l_next_free_rec));
  2657. BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
  2658. if (le16_to_cpu(el->l_tree_depth) == 0) {
  2659. /*
  2660. * If the leaf block contains a single empty
  2661. * extent and no records, we can just remove
  2662. * the block.
  2663. */
  2664. if (i == 0 && ocfs2_is_empty_extent(rec)) {
  2665. memset(rec, 0,
  2666. sizeof(struct ocfs2_extent_rec));
  2667. el->l_next_free_rec = cpu_to_le16(0);
  2668. goto delete;
  2669. }
  2670. /*
  2671. * Remove any empty extents by shifting things
  2672. * left. That should make life much easier on
  2673. * the code below. This condition is rare
  2674. * enough that we shouldn't see a performance
  2675. * hit.
  2676. */
  2677. if (ocfs2_is_empty_extent(&el->l_recs[0])) {
  2678. le16_add_cpu(&el->l_next_free_rec, -1);
  2679. for(i = 0;
  2680. i < le16_to_cpu(el->l_next_free_rec); i++)
  2681. el->l_recs[i] = el->l_recs[i + 1];
  2682. memset(&el->l_recs[i], 0,
  2683. sizeof(struct ocfs2_extent_rec));
  2684. /*
  2685. * We've modified our extent list. The
  2686. * simplest way to handle this change
  2687. * is to being the search from the
  2688. * start again.
  2689. */
  2690. goto find_tail_record;
  2691. }
  2692. le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
  2693. /*
  2694. * We'll use "new_edge" on our way back up the
  2695. * tree to know what our rightmost cpos is.
  2696. */
  2697. new_edge = le16_to_cpu(rec->e_leaf_clusters);
  2698. new_edge += le32_to_cpu(rec->e_cpos);
  2699. /*
  2700. * The caller will use this to delete data blocks.
  2701. */
  2702. *delete_start = le64_to_cpu(rec->e_blkno)
  2703. + ocfs2_clusters_to_blocks(inode->i_sb,
  2704. le16_to_cpu(rec->e_leaf_clusters));
  2705. /*
  2706. * If it's now empty, remove this record.
  2707. */
  2708. if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
  2709. memset(rec, 0,
  2710. sizeof(struct ocfs2_extent_rec));
  2711. le16_add_cpu(&el->l_next_free_rec, -1);
  2712. }
  2713. } else {
  2714. if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
  2715. memset(rec, 0,
  2716. sizeof(struct ocfs2_extent_rec));
  2717. le16_add_cpu(&el->l_next_free_rec, -1);
  2718. goto delete;
  2719. }
  2720. /* Can this actually happen? */
  2721. if (le16_to_cpu(el->l_next_free_rec) == 0)
  2722. goto delete;
  2723. /*
  2724. * We never actually deleted any clusters
  2725. * because our leaf was empty. There's no
  2726. * reason to adjust the rightmost edge then.
  2727. */
  2728. if (new_edge == 0)
  2729. goto delete;
  2730. rec->e_int_clusters = cpu_to_le32(new_edge);
  2731. le32_add_cpu(&rec->e_int_clusters,
  2732. -le32_to_cpu(rec->e_cpos));
  2733. /*
  2734. * A deleted child record should have been
  2735. * caught above.
  2736. */
  2737. BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
  2738. }
  2739. delete:
  2740. ret = ocfs2_journal_dirty(handle, bh);
  2741. if (ret) {
  2742. mlog_errno(ret);
  2743. goto out;
  2744. }
  2745. mlog(0, "extent list container %llu, after: record %d: "
  2746. "(%u, %u, %llu), next = %u.\n",
  2747. (unsigned long long)bh->b_blocknr, i,
  2748. le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
  2749. (unsigned long long)le64_to_cpu(rec->e_blkno),
  2750. le16_to_cpu(el->l_next_free_rec));
  2751. /*
  2752. * We must be careful to only attempt delete of an
  2753. * extent block (and not the root inode block).
  2754. */
  2755. if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
  2756. struct ocfs2_extent_block *eb =
  2757. (struct ocfs2_extent_block *)bh->b_data;
  2758. /*
  2759. * Save this for use when processing the
  2760. * parent block.
  2761. */
  2762. deleted_eb = le64_to_cpu(eb->h_blkno);
  2763. mlog(0, "deleting this extent block.\n");
  2764. ocfs2_remove_from_cache(inode, bh);
  2765. BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
  2766. BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
  2767. BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
  2768. if (le16_to_cpu(eb->h_suballoc_slot) == 0) {
  2769. /*
  2770. * This code only understands how to
  2771. * lock the suballocator in slot 0,
  2772. * which is fine because allocation is
  2773. * only ever done out of that
  2774. * suballocator too. A future version
  2775. * might change that however, so avoid
  2776. * a free if we don't know how to
  2777. * handle it. This way an fs incompat
  2778. * bit will not be necessary.
  2779. */
  2780. ret = ocfs2_free_extent_block(handle,
  2781. tc->tc_ext_alloc_inode,
  2782. tc->tc_ext_alloc_bh,
  2783. eb);
  2784. /* An error here is not fatal. */
  2785. if (ret < 0)
  2786. mlog_errno(ret);
  2787. }
  2788. } else {
  2789. deleted_eb = 0;
  2790. }
  2791. index--;
  2792. }
  2793. ret = 0;
  2794. out:
  2795. return ret;
  2796. }
  2797. static int ocfs2_do_truncate(struct ocfs2_super *osb,
  2798. unsigned int clusters_to_del,
  2799. struct inode *inode,
  2800. struct buffer_head *fe_bh,
  2801. handle_t *handle,
  2802. struct ocfs2_truncate_context *tc,
  2803. struct ocfs2_path *path)
  2804. {
  2805. int status;
  2806. struct ocfs2_dinode *fe;
  2807. struct ocfs2_extent_block *last_eb = NULL;
  2808. struct ocfs2_extent_list *el;
  2809. struct buffer_head *last_eb_bh = NULL;
  2810. u64 delete_blk = 0;
  2811. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  2812. status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
  2813. path, &last_eb_bh);
  2814. if (status < 0) {
  2815. mlog_errno(status);
  2816. goto bail;
  2817. }
  2818. /*
  2819. * Each component will be touched, so we might as well journal
  2820. * here to avoid having to handle errors later.
  2821. */
  2822. status = ocfs2_journal_access_path(inode, handle, path);
  2823. if (status < 0) {
  2824. mlog_errno(status);
  2825. goto bail;
  2826. }
  2827. if (last_eb_bh) {
  2828. status = ocfs2_journal_access(handle, inode, last_eb_bh,
  2829. OCFS2_JOURNAL_ACCESS_WRITE);
  2830. if (status < 0) {
  2831. mlog_errno(status);
  2832. goto bail;
  2833. }
  2834. last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
  2835. }
  2836. el = &(fe->id2.i_list);
  2837. /*
  2838. * Lower levels depend on this never happening, but it's best
  2839. * to check it up here before changing the tree.
  2840. */
  2841. if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
  2842. ocfs2_error(inode->i_sb,
  2843. "Inode %lu has an empty extent record, depth %u\n",
  2844. inode->i_ino, le16_to_cpu(el->l_tree_depth));
  2845. status = -EROFS;
  2846. goto bail;
  2847. }
  2848. spin_lock(&OCFS2_I(inode)->ip_lock);
  2849. OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
  2850. clusters_to_del;
  2851. spin_unlock(&OCFS2_I(inode)->ip_lock);
  2852. le32_add_cpu(&fe->i_clusters, -clusters_to_del);
  2853. status = ocfs2_trim_tree(inode, path, handle, tc,
  2854. clusters_to_del, &delete_blk);
  2855. if (status) {
  2856. mlog_errno(status);
  2857. goto bail;
  2858. }
  2859. if (le32_to_cpu(fe->i_clusters) == 0) {
  2860. /* trunc to zero is a special case. */
  2861. el->l_tree_depth = 0;
  2862. fe->i_last_eb_blk = 0;
  2863. } else if (last_eb)
  2864. fe->i_last_eb_blk = last_eb->h_blkno;
  2865. status = ocfs2_journal_dirty(handle, fe_bh);
  2866. if (status < 0) {
  2867. mlog_errno(status);
  2868. goto bail;
  2869. }
  2870. if (last_eb) {
  2871. /* If there will be a new last extent block, then by
  2872. * definition, there cannot be any leaves to the right of
  2873. * him. */
  2874. last_eb->h_next_leaf_blk = 0;
  2875. status = ocfs2_journal_dirty(handle, last_eb_bh);
  2876. if (status < 0) {
  2877. mlog_errno(status);
  2878. goto bail;
  2879. }
  2880. }
  2881. if (delete_blk) {
  2882. status = ocfs2_truncate_log_append(osb, handle, delete_blk,
  2883. clusters_to_del);
  2884. if (status < 0) {
  2885. mlog_errno(status);
  2886. goto bail;
  2887. }
  2888. }
  2889. status = 0;
  2890. bail:
  2891. mlog_exit(status);
  2892. return status;
  2893. }
  2894. static int ocfs2_writeback_zero_func(handle_t *handle, struct buffer_head *bh)
  2895. {
  2896. set_buffer_uptodate(bh);
  2897. mark_buffer_dirty(bh);
  2898. return 0;
  2899. }
  2900. static int ocfs2_ordered_zero_func(handle_t *handle, struct buffer_head *bh)
  2901. {
  2902. set_buffer_uptodate(bh);
  2903. mark_buffer_dirty(bh);
  2904. return ocfs2_journal_dirty_data(handle, bh);
  2905. }
  2906. static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t isize,
  2907. struct page **pages, int numpages,
  2908. u64 phys, handle_t *handle)
  2909. {
  2910. int i, ret, partial = 0;
  2911. void *kaddr;
  2912. struct page *page;
  2913. unsigned int from, to = PAGE_CACHE_SIZE;
  2914. struct super_block *sb = inode->i_sb;
  2915. BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
  2916. if (numpages == 0)
  2917. goto out;
  2918. from = isize & (PAGE_CACHE_SIZE - 1); /* 1st page offset */
  2919. if (PAGE_CACHE_SHIFT > OCFS2_SB(sb)->s_clustersize_bits) {
  2920. /*
  2921. * Since 'from' has been capped to a value below page
  2922. * size, this calculation won't be able to overflow
  2923. * 'to'
  2924. */
  2925. to = ocfs2_align_bytes_to_clusters(sb, from);
  2926. /*
  2927. * The truncate tail in this case should never contain
  2928. * more than one page at maximum. The loop below also
  2929. * assumes this.
  2930. */
  2931. BUG_ON(numpages != 1);
  2932. }
  2933. for(i = 0; i < numpages; i++) {
  2934. page = pages[i];
  2935. BUG_ON(from > PAGE_CACHE_SIZE);
  2936. BUG_ON(to > PAGE_CACHE_SIZE);
  2937. ret = ocfs2_map_page_blocks(page, &phys, inode, from, to, 0);
  2938. if (ret)
  2939. mlog_errno(ret);
  2940. kaddr = kmap_atomic(page, KM_USER0);
  2941. memset(kaddr + from, 0, to - from);
  2942. kunmap_atomic(kaddr, KM_USER0);
  2943. /*
  2944. * Need to set the buffers we zero'd into uptodate
  2945. * here if they aren't - ocfs2_map_page_blocks()
  2946. * might've skipped some
  2947. */
  2948. if (ocfs2_should_order_data(inode)) {
  2949. ret = walk_page_buffers(handle,
  2950. page_buffers(page),
  2951. from, to, &partial,
  2952. ocfs2_ordered_zero_func);
  2953. if (ret < 0)
  2954. mlog_errno(ret);
  2955. } else {
  2956. ret = walk_page_buffers(handle, page_buffers(page),
  2957. from, to, &partial,
  2958. ocfs2_writeback_zero_func);
  2959. if (ret < 0)
  2960. mlog_errno(ret);
  2961. }
  2962. if (!partial)
  2963. SetPageUptodate(page);
  2964. flush_dcache_page(page);
  2965. /*
  2966. * Every page after the 1st one should be completely zero'd.
  2967. */
  2968. from = 0;
  2969. }
  2970. out:
  2971. if (pages) {
  2972. for (i = 0; i < numpages; i++) {
  2973. page = pages[i];
  2974. unlock_page(page);
  2975. mark_page_accessed(page);
  2976. page_cache_release(page);
  2977. }
  2978. }
  2979. }
  2980. static int ocfs2_grab_eof_pages(struct inode *inode, loff_t isize, struct page **pages,
  2981. int *num, u64 *phys)
  2982. {
  2983. int i, numpages = 0, ret = 0;
  2984. unsigned int csize = OCFS2_SB(inode->i_sb)->s_clustersize;
  2985. unsigned int ext_flags;
  2986. struct super_block *sb = inode->i_sb;
  2987. struct address_space *mapping = inode->i_mapping;
  2988. unsigned long index;
  2989. u64 next_cluster_bytes;
  2990. BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
  2991. /* Cluster boundary, so we don't need to grab any pages. */
  2992. if ((isize & (csize - 1)) == 0)
  2993. goto out;
  2994. ret = ocfs2_extent_map_get_blocks(inode, isize >> sb->s_blocksize_bits,
  2995. phys, NULL, &ext_flags);
  2996. if (ret) {
  2997. mlog_errno(ret);
  2998. goto out;
  2999. }
  3000. /* Tail is a hole. */
  3001. if (*phys == 0)
  3002. goto out;
  3003. /* Tail is marked as unwritten, we can count on write to zero
  3004. * in that case. */
  3005. if (ext_flags & OCFS2_EXT_UNWRITTEN)
  3006. goto out;
  3007. next_cluster_bytes = ocfs2_align_bytes_to_clusters(inode->i_sb, isize);
  3008. index = isize >> PAGE_CACHE_SHIFT;
  3009. do {
  3010. pages[numpages] = grab_cache_page(mapping, index);
  3011. if (!pages[numpages]) {
  3012. ret = -ENOMEM;
  3013. mlog_errno(ret);
  3014. goto out;
  3015. }
  3016. numpages++;
  3017. index++;
  3018. } while (index < (next_cluster_bytes >> PAGE_CACHE_SHIFT));
  3019. out:
  3020. if (ret != 0) {
  3021. if (pages) {
  3022. for (i = 0; i < numpages; i++) {
  3023. if (pages[i]) {
  3024. unlock_page(pages[i]);
  3025. page_cache_release(pages[i]);
  3026. }
  3027. }
  3028. }
  3029. numpages = 0;
  3030. }
  3031. *num = numpages;
  3032. return ret;
  3033. }
  3034. /*
  3035. * Zero the area past i_size but still within an allocated
  3036. * cluster. This avoids exposing nonzero data on subsequent file
  3037. * extends.
  3038. *
  3039. * We need to call this before i_size is updated on the inode because
  3040. * otherwise block_write_full_page() will skip writeout of pages past
  3041. * i_size. The new_i_size parameter is passed for this reason.
  3042. */
  3043. int ocfs2_zero_tail_for_truncate(struct inode *inode, handle_t *handle,
  3044. u64 new_i_size)
  3045. {
  3046. int ret, numpages;
  3047. loff_t endbyte;
  3048. struct page **pages = NULL;
  3049. u64 phys;
  3050. /*
  3051. * File systems which don't support sparse files zero on every
  3052. * extend.
  3053. */
  3054. if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
  3055. return 0;
  3056. pages = kcalloc(ocfs2_pages_per_cluster(inode->i_sb),
  3057. sizeof(struct page *), GFP_NOFS);
  3058. if (pages == NULL) {
  3059. ret = -ENOMEM;
  3060. mlog_errno(ret);
  3061. goto out;
  3062. }
  3063. ret = ocfs2_grab_eof_pages(inode, new_i_size, pages, &numpages, &phys);
  3064. if (ret) {
  3065. mlog_errno(ret);
  3066. goto out;
  3067. }
  3068. if (numpages == 0)
  3069. goto out;
  3070. ocfs2_zero_cluster_pages(inode, new_i_size, pages, numpages, phys,
  3071. handle);
  3072. /*
  3073. * Initiate writeout of the pages we zero'd here. We don't
  3074. * wait on them - the truncate_inode_pages() call later will
  3075. * do that for us.
  3076. */
  3077. endbyte = ocfs2_align_bytes_to_clusters(inode->i_sb, new_i_size);
  3078. ret = do_sync_mapping_range(inode->i_mapping, new_i_size,
  3079. endbyte - 1, SYNC_FILE_RANGE_WRITE);
  3080. if (ret)
  3081. mlog_errno(ret);
  3082. out:
  3083. if (pages)
  3084. kfree(pages);
  3085. return ret;
  3086. }
  3087. /*
  3088. * It is expected, that by the time you call this function,
  3089. * inode->i_size and fe->i_size have been adjusted.
  3090. *
  3091. * WARNING: This will kfree the truncate context
  3092. */
  3093. int ocfs2_commit_truncate(struct ocfs2_super *osb,
  3094. struct inode *inode,
  3095. struct buffer_head *fe_bh,
  3096. struct ocfs2_truncate_context *tc)
  3097. {
  3098. int status, i, credits, tl_sem = 0;
  3099. u32 clusters_to_del, new_highest_cpos, range;
  3100. struct ocfs2_extent_list *el;
  3101. handle_t *handle = NULL;
  3102. struct inode *tl_inode = osb->osb_tl_inode;
  3103. struct ocfs2_path *path = NULL;
  3104. mlog_entry_void();
  3105. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  3106. new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
  3107. i_size_read(inode));
  3108. path = ocfs2_new_inode_path(fe_bh);
  3109. if (!path) {
  3110. status = -ENOMEM;
  3111. mlog_errno(status);
  3112. goto bail;
  3113. }
  3114. ocfs2_extent_map_trunc(inode, new_highest_cpos);
  3115. start:
  3116. /*
  3117. * Check that we still have allocation to delete.
  3118. */
  3119. if (OCFS2_I(inode)->ip_clusters == 0) {
  3120. status = 0;
  3121. goto bail;
  3122. }
  3123. /*
  3124. * Truncate always works against the rightmost tree branch.
  3125. */
  3126. status = ocfs2_find_path(inode, path, UINT_MAX);
  3127. if (status) {
  3128. mlog_errno(status);
  3129. goto bail;
  3130. }
  3131. mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
  3132. OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
  3133. /*
  3134. * By now, el will point to the extent list on the bottom most
  3135. * portion of this tree. Only the tail record is considered in
  3136. * each pass.
  3137. *
  3138. * We handle the following cases, in order:
  3139. * - empty extent: delete the remaining branch
  3140. * - remove the entire record
  3141. * - remove a partial record
  3142. * - no record needs to be removed (truncate has completed)
  3143. */
  3144. el = path_leaf_el(path);
  3145. if (le16_to_cpu(el->l_next_free_rec) == 0) {
  3146. ocfs2_error(inode->i_sb,
  3147. "Inode %llu has empty extent block at %llu\n",
  3148. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  3149. (unsigned long long)path_leaf_bh(path)->b_blocknr);
  3150. status = -EROFS;
  3151. goto bail;
  3152. }
  3153. i = le16_to_cpu(el->l_next_free_rec) - 1;
  3154. range = le32_to_cpu(el->l_recs[i].e_cpos) +
  3155. ocfs2_rec_clusters(el, &el->l_recs[i]);
  3156. if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
  3157. clusters_to_del = 0;
  3158. } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
  3159. clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
  3160. } else if (range > new_highest_cpos) {
  3161. clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
  3162. le32_to_cpu(el->l_recs[i].e_cpos)) -
  3163. new_highest_cpos;
  3164. } else {
  3165. status = 0;
  3166. goto bail;
  3167. }
  3168. mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
  3169. clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
  3170. BUG_ON(clusters_to_del == 0);
  3171. mutex_lock(&tl_inode->i_mutex);
  3172. tl_sem = 1;
  3173. /* ocfs2_truncate_log_needs_flush guarantees us at least one
  3174. * record is free for use. If there isn't any, we flush to get
  3175. * an empty truncate log. */
  3176. if (ocfs2_truncate_log_needs_flush(osb)) {
  3177. status = __ocfs2_flush_truncate_log(osb);
  3178. if (status < 0) {
  3179. mlog_errno(status);
  3180. goto bail;
  3181. }
  3182. }
  3183. credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
  3184. (struct ocfs2_dinode *)fe_bh->b_data,
  3185. el);
  3186. handle = ocfs2_start_trans(osb, credits);
  3187. if (IS_ERR(handle)) {
  3188. status = PTR_ERR(handle);
  3189. handle = NULL;
  3190. mlog_errno(status);
  3191. goto bail;
  3192. }
  3193. status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
  3194. tc, path);
  3195. if (status < 0) {
  3196. mlog_errno(status);
  3197. goto bail;
  3198. }
  3199. mutex_unlock(&tl_inode->i_mutex);
  3200. tl_sem = 0;
  3201. ocfs2_commit_trans(osb, handle);
  3202. handle = NULL;
  3203. ocfs2_reinit_path(path, 1);
  3204. /*
  3205. * The check above will catch the case where we've truncated
  3206. * away all allocation.
  3207. */
  3208. goto start;
  3209. bail:
  3210. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  3211. ocfs2_schedule_truncate_log_flush(osb, 1);
  3212. if (tl_sem)
  3213. mutex_unlock(&tl_inode->i_mutex);
  3214. if (handle)
  3215. ocfs2_commit_trans(osb, handle);
  3216. ocfs2_free_path(path);
  3217. /* This will drop the ext_alloc cluster lock for us */
  3218. ocfs2_free_truncate_context(tc);
  3219. mlog_exit(status);
  3220. return status;
  3221. }
  3222. /*
  3223. * Expects the inode to already be locked. This will figure out which
  3224. * inodes need to be locked and will put them on the returned truncate
  3225. * context.
  3226. */
  3227. int ocfs2_prepare_truncate(struct ocfs2_super *osb,
  3228. struct inode *inode,
  3229. struct buffer_head *fe_bh,
  3230. struct ocfs2_truncate_context **tc)
  3231. {
  3232. int status, metadata_delete, i;
  3233. unsigned int new_i_clusters;
  3234. struct ocfs2_dinode *fe;
  3235. struct ocfs2_extent_block *eb;
  3236. struct ocfs2_extent_list *el;
  3237. struct buffer_head *last_eb_bh = NULL;
  3238. struct inode *ext_alloc_inode = NULL;
  3239. struct buffer_head *ext_alloc_bh = NULL;
  3240. mlog_entry_void();
  3241. *tc = NULL;
  3242. new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
  3243. i_size_read(inode));
  3244. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  3245. mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
  3246. "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
  3247. (unsigned long long)le64_to_cpu(fe->i_size));
  3248. *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
  3249. if (!(*tc)) {
  3250. status = -ENOMEM;
  3251. mlog_errno(status);
  3252. goto bail;
  3253. }
  3254. metadata_delete = 0;
  3255. if (fe->id2.i_list.l_tree_depth) {
  3256. /* If we have a tree, then the truncate may result in
  3257. * metadata deletes. Figure this out from the
  3258. * rightmost leaf block.*/
  3259. status = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
  3260. &last_eb_bh, OCFS2_BH_CACHED, inode);
  3261. if (status < 0) {
  3262. mlog_errno(status);
  3263. goto bail;
  3264. }
  3265. eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
  3266. if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
  3267. OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
  3268. brelse(last_eb_bh);
  3269. status = -EIO;
  3270. goto bail;
  3271. }
  3272. el = &(eb->h_list);
  3273. i = 0;
  3274. if (ocfs2_is_empty_extent(&el->l_recs[0]))
  3275. i = 1;
  3276. /*
  3277. * XXX: Should we check that next_free_rec contains
  3278. * the extent?
  3279. */
  3280. if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_i_clusters)
  3281. metadata_delete = 1;
  3282. }
  3283. (*tc)->tc_last_eb_bh = last_eb_bh;
  3284. if (metadata_delete) {
  3285. mlog(0, "Will have to delete metadata for this trunc. "
  3286. "locking allocator.\n");
  3287. ext_alloc_inode = ocfs2_get_system_file_inode(osb, EXTENT_ALLOC_SYSTEM_INODE, 0);
  3288. if (!ext_alloc_inode) {
  3289. status = -ENOMEM;
  3290. mlog_errno(status);
  3291. goto bail;
  3292. }
  3293. mutex_lock(&ext_alloc_inode->i_mutex);
  3294. (*tc)->tc_ext_alloc_inode = ext_alloc_inode;
  3295. status = ocfs2_meta_lock(ext_alloc_inode, &ext_alloc_bh, 1);
  3296. if (status < 0) {
  3297. mlog_errno(status);
  3298. goto bail;
  3299. }
  3300. (*tc)->tc_ext_alloc_bh = ext_alloc_bh;
  3301. (*tc)->tc_ext_alloc_locked = 1;
  3302. }
  3303. status = 0;
  3304. bail:
  3305. if (status < 0) {
  3306. if (*tc)
  3307. ocfs2_free_truncate_context(*tc);
  3308. *tc = NULL;
  3309. }
  3310. mlog_exit_void();
  3311. return status;
  3312. }
  3313. static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
  3314. {
  3315. if (tc->tc_ext_alloc_inode) {
  3316. if (tc->tc_ext_alloc_locked)
  3317. ocfs2_meta_unlock(tc->tc_ext_alloc_inode, 1);
  3318. mutex_unlock(&tc->tc_ext_alloc_inode->i_mutex);
  3319. iput(tc->tc_ext_alloc_inode);
  3320. }
  3321. if (tc->tc_ext_alloc_bh)
  3322. brelse(tc->tc_ext_alloc_bh);
  3323. if (tc->tc_last_eb_bh)
  3324. brelse(tc->tc_last_eb_bh);
  3325. kfree(tc);
  3326. }