refcounttree.c 114 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * refcounttree.c
  5. *
  6. * Copyright (C) 2009 Oracle. All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. */
  17. #include <linux/sort.h>
  18. #define MLOG_MASK_PREFIX ML_REFCOUNT
  19. #include <cluster/masklog.h>
  20. #include "ocfs2.h"
  21. #include "inode.h"
  22. #include "alloc.h"
  23. #include "suballoc.h"
  24. #include "journal.h"
  25. #include "uptodate.h"
  26. #include "super.h"
  27. #include "buffer_head_io.h"
  28. #include "blockcheck.h"
  29. #include "refcounttree.h"
  30. #include "sysfile.h"
  31. #include "dlmglue.h"
  32. #include "extent_map.h"
  33. #include "aops.h"
  34. #include "xattr.h"
  35. #include "namei.h"
  36. #include <linux/bio.h>
  37. #include <linux/blkdev.h>
  38. #include <linux/slab.h>
  39. #include <linux/writeback.h>
  40. #include <linux/pagevec.h>
  41. #include <linux/swap.h>
  42. #include <linux/security.h>
  43. #include <linux/fsnotify.h>
  44. #include <linux/quotaops.h>
  45. #include <linux/namei.h>
  46. #include <linux/mount.h>
  47. struct ocfs2_cow_context {
  48. struct inode *inode;
  49. struct file *file;
  50. u32 cow_start;
  51. u32 cow_len;
  52. struct ocfs2_extent_tree data_et;
  53. struct ocfs2_refcount_tree *ref_tree;
  54. struct buffer_head *ref_root_bh;
  55. struct ocfs2_alloc_context *meta_ac;
  56. struct ocfs2_alloc_context *data_ac;
  57. struct ocfs2_cached_dealloc_ctxt dealloc;
  58. void *cow_object;
  59. struct ocfs2_post_refcount *post_refcount;
  60. int extra_credits;
  61. int (*get_clusters)(struct ocfs2_cow_context *context,
  62. u32 v_cluster, u32 *p_cluster,
  63. u32 *num_clusters,
  64. unsigned int *extent_flags);
  65. int (*cow_duplicate_clusters)(handle_t *handle,
  66. struct ocfs2_cow_context *context,
  67. u32 cpos, u32 old_cluster,
  68. u32 new_cluster, u32 new_len);
  69. };
  70. static inline struct ocfs2_refcount_tree *
  71. cache_info_to_refcount(struct ocfs2_caching_info *ci)
  72. {
  73. return container_of(ci, struct ocfs2_refcount_tree, rf_ci);
  74. }
  75. static int ocfs2_validate_refcount_block(struct super_block *sb,
  76. struct buffer_head *bh)
  77. {
  78. int rc;
  79. struct ocfs2_refcount_block *rb =
  80. (struct ocfs2_refcount_block *)bh->b_data;
  81. mlog(0, "Validating refcount block %llu\n",
  82. (unsigned long long)bh->b_blocknr);
  83. BUG_ON(!buffer_uptodate(bh));
  84. /*
  85. * If the ecc fails, we return the error but otherwise
  86. * leave the filesystem running. We know any error is
  87. * local to this block.
  88. */
  89. rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &rb->rf_check);
  90. if (rc) {
  91. mlog(ML_ERROR, "Checksum failed for refcount block %llu\n",
  92. (unsigned long long)bh->b_blocknr);
  93. return rc;
  94. }
  95. if (!OCFS2_IS_VALID_REFCOUNT_BLOCK(rb)) {
  96. ocfs2_error(sb,
  97. "Refcount block #%llu has bad signature %.*s",
  98. (unsigned long long)bh->b_blocknr, 7,
  99. rb->rf_signature);
  100. return -EINVAL;
  101. }
  102. if (le64_to_cpu(rb->rf_blkno) != bh->b_blocknr) {
  103. ocfs2_error(sb,
  104. "Refcount block #%llu has an invalid rf_blkno "
  105. "of %llu",
  106. (unsigned long long)bh->b_blocknr,
  107. (unsigned long long)le64_to_cpu(rb->rf_blkno));
  108. return -EINVAL;
  109. }
  110. if (le32_to_cpu(rb->rf_fs_generation) != OCFS2_SB(sb)->fs_generation) {
  111. ocfs2_error(sb,
  112. "Refcount block #%llu has an invalid "
  113. "rf_fs_generation of #%u",
  114. (unsigned long long)bh->b_blocknr,
  115. le32_to_cpu(rb->rf_fs_generation));
  116. return -EINVAL;
  117. }
  118. return 0;
  119. }
  120. static int ocfs2_read_refcount_block(struct ocfs2_caching_info *ci,
  121. u64 rb_blkno,
  122. struct buffer_head **bh)
  123. {
  124. int rc;
  125. struct buffer_head *tmp = *bh;
  126. rc = ocfs2_read_block(ci, rb_blkno, &tmp,
  127. ocfs2_validate_refcount_block);
  128. /* If ocfs2_read_block() got us a new bh, pass it up. */
  129. if (!rc && !*bh)
  130. *bh = tmp;
  131. return rc;
  132. }
  133. static u64 ocfs2_refcount_cache_owner(struct ocfs2_caching_info *ci)
  134. {
  135. struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
  136. return rf->rf_blkno;
  137. }
  138. static struct super_block *
  139. ocfs2_refcount_cache_get_super(struct ocfs2_caching_info *ci)
  140. {
  141. struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
  142. return rf->rf_sb;
  143. }
  144. static void ocfs2_refcount_cache_lock(struct ocfs2_caching_info *ci)
  145. {
  146. struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
  147. spin_lock(&rf->rf_lock);
  148. }
  149. static void ocfs2_refcount_cache_unlock(struct ocfs2_caching_info *ci)
  150. {
  151. struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
  152. spin_unlock(&rf->rf_lock);
  153. }
  154. static void ocfs2_refcount_cache_io_lock(struct ocfs2_caching_info *ci)
  155. {
  156. struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
  157. mutex_lock(&rf->rf_io_mutex);
  158. }
  159. static void ocfs2_refcount_cache_io_unlock(struct ocfs2_caching_info *ci)
  160. {
  161. struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
  162. mutex_unlock(&rf->rf_io_mutex);
  163. }
  164. static const struct ocfs2_caching_operations ocfs2_refcount_caching_ops = {
  165. .co_owner = ocfs2_refcount_cache_owner,
  166. .co_get_super = ocfs2_refcount_cache_get_super,
  167. .co_cache_lock = ocfs2_refcount_cache_lock,
  168. .co_cache_unlock = ocfs2_refcount_cache_unlock,
  169. .co_io_lock = ocfs2_refcount_cache_io_lock,
  170. .co_io_unlock = ocfs2_refcount_cache_io_unlock,
  171. };
  172. static struct ocfs2_refcount_tree *
  173. ocfs2_find_refcount_tree(struct ocfs2_super *osb, u64 blkno)
  174. {
  175. struct rb_node *n = osb->osb_rf_lock_tree.rb_node;
  176. struct ocfs2_refcount_tree *tree = NULL;
  177. while (n) {
  178. tree = rb_entry(n, struct ocfs2_refcount_tree, rf_node);
  179. if (blkno < tree->rf_blkno)
  180. n = n->rb_left;
  181. else if (blkno > tree->rf_blkno)
  182. n = n->rb_right;
  183. else
  184. return tree;
  185. }
  186. return NULL;
  187. }
  188. /* osb_lock is already locked. */
  189. static void ocfs2_insert_refcount_tree(struct ocfs2_super *osb,
  190. struct ocfs2_refcount_tree *new)
  191. {
  192. u64 rf_blkno = new->rf_blkno;
  193. struct rb_node *parent = NULL;
  194. struct rb_node **p = &osb->osb_rf_lock_tree.rb_node;
  195. struct ocfs2_refcount_tree *tmp;
  196. while (*p) {
  197. parent = *p;
  198. tmp = rb_entry(parent, struct ocfs2_refcount_tree,
  199. rf_node);
  200. if (rf_blkno < tmp->rf_blkno)
  201. p = &(*p)->rb_left;
  202. else if (rf_blkno > tmp->rf_blkno)
  203. p = &(*p)->rb_right;
  204. else {
  205. /* This should never happen! */
  206. mlog(ML_ERROR, "Duplicate refcount block %llu found!\n",
  207. (unsigned long long)rf_blkno);
  208. BUG();
  209. }
  210. }
  211. rb_link_node(&new->rf_node, parent, p);
  212. rb_insert_color(&new->rf_node, &osb->osb_rf_lock_tree);
  213. }
  214. static void ocfs2_free_refcount_tree(struct ocfs2_refcount_tree *tree)
  215. {
  216. ocfs2_metadata_cache_exit(&tree->rf_ci);
  217. ocfs2_simple_drop_lockres(OCFS2_SB(tree->rf_sb), &tree->rf_lockres);
  218. ocfs2_lock_res_free(&tree->rf_lockres);
  219. kfree(tree);
  220. }
  221. static inline void
  222. ocfs2_erase_refcount_tree_from_list_no_lock(struct ocfs2_super *osb,
  223. struct ocfs2_refcount_tree *tree)
  224. {
  225. rb_erase(&tree->rf_node, &osb->osb_rf_lock_tree);
  226. if (osb->osb_ref_tree_lru && osb->osb_ref_tree_lru == tree)
  227. osb->osb_ref_tree_lru = NULL;
  228. }
  229. static void ocfs2_erase_refcount_tree_from_list(struct ocfs2_super *osb,
  230. struct ocfs2_refcount_tree *tree)
  231. {
  232. spin_lock(&osb->osb_lock);
  233. ocfs2_erase_refcount_tree_from_list_no_lock(osb, tree);
  234. spin_unlock(&osb->osb_lock);
  235. }
  236. static void ocfs2_kref_remove_refcount_tree(struct kref *kref)
  237. {
  238. struct ocfs2_refcount_tree *tree =
  239. container_of(kref, struct ocfs2_refcount_tree, rf_getcnt);
  240. ocfs2_free_refcount_tree(tree);
  241. }
  242. static inline void
  243. ocfs2_refcount_tree_get(struct ocfs2_refcount_tree *tree)
  244. {
  245. kref_get(&tree->rf_getcnt);
  246. }
  247. static inline void
  248. ocfs2_refcount_tree_put(struct ocfs2_refcount_tree *tree)
  249. {
  250. kref_put(&tree->rf_getcnt, ocfs2_kref_remove_refcount_tree);
  251. }
  252. static inline void ocfs2_init_refcount_tree_ci(struct ocfs2_refcount_tree *new,
  253. struct super_block *sb)
  254. {
  255. ocfs2_metadata_cache_init(&new->rf_ci, &ocfs2_refcount_caching_ops);
  256. mutex_init(&new->rf_io_mutex);
  257. new->rf_sb = sb;
  258. spin_lock_init(&new->rf_lock);
  259. }
  260. static inline void ocfs2_init_refcount_tree_lock(struct ocfs2_super *osb,
  261. struct ocfs2_refcount_tree *new,
  262. u64 rf_blkno, u32 generation)
  263. {
  264. init_rwsem(&new->rf_sem);
  265. ocfs2_refcount_lock_res_init(&new->rf_lockres, osb,
  266. rf_blkno, generation);
  267. }
  268. static struct ocfs2_refcount_tree*
  269. ocfs2_allocate_refcount_tree(struct ocfs2_super *osb, u64 rf_blkno)
  270. {
  271. struct ocfs2_refcount_tree *new;
  272. new = kzalloc(sizeof(struct ocfs2_refcount_tree), GFP_NOFS);
  273. if (!new)
  274. return NULL;
  275. new->rf_blkno = rf_blkno;
  276. kref_init(&new->rf_getcnt);
  277. ocfs2_init_refcount_tree_ci(new, osb->sb);
  278. return new;
  279. }
  280. static int ocfs2_get_refcount_tree(struct ocfs2_super *osb, u64 rf_blkno,
  281. struct ocfs2_refcount_tree **ret_tree)
  282. {
  283. int ret = 0;
  284. struct ocfs2_refcount_tree *tree, *new = NULL;
  285. struct buffer_head *ref_root_bh = NULL;
  286. struct ocfs2_refcount_block *ref_rb;
  287. spin_lock(&osb->osb_lock);
  288. if (osb->osb_ref_tree_lru &&
  289. osb->osb_ref_tree_lru->rf_blkno == rf_blkno)
  290. tree = osb->osb_ref_tree_lru;
  291. else
  292. tree = ocfs2_find_refcount_tree(osb, rf_blkno);
  293. if (tree)
  294. goto out;
  295. spin_unlock(&osb->osb_lock);
  296. new = ocfs2_allocate_refcount_tree(osb, rf_blkno);
  297. if (!new) {
  298. ret = -ENOMEM;
  299. mlog_errno(ret);
  300. return ret;
  301. }
  302. /*
  303. * We need the generation to create the refcount tree lock and since
  304. * it isn't changed during the tree modification, we are safe here to
  305. * read without protection.
  306. * We also have to purge the cache after we create the lock since the
  307. * refcount block may have the stale data. It can only be trusted when
  308. * we hold the refcount lock.
  309. */
  310. ret = ocfs2_read_refcount_block(&new->rf_ci, rf_blkno, &ref_root_bh);
  311. if (ret) {
  312. mlog_errno(ret);
  313. ocfs2_metadata_cache_exit(&new->rf_ci);
  314. kfree(new);
  315. return ret;
  316. }
  317. ref_rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
  318. new->rf_generation = le32_to_cpu(ref_rb->rf_generation);
  319. ocfs2_init_refcount_tree_lock(osb, new, rf_blkno,
  320. new->rf_generation);
  321. ocfs2_metadata_cache_purge(&new->rf_ci);
  322. spin_lock(&osb->osb_lock);
  323. tree = ocfs2_find_refcount_tree(osb, rf_blkno);
  324. if (tree)
  325. goto out;
  326. ocfs2_insert_refcount_tree(osb, new);
  327. tree = new;
  328. new = NULL;
  329. out:
  330. *ret_tree = tree;
  331. osb->osb_ref_tree_lru = tree;
  332. spin_unlock(&osb->osb_lock);
  333. if (new)
  334. ocfs2_free_refcount_tree(new);
  335. brelse(ref_root_bh);
  336. return ret;
  337. }
  338. static int ocfs2_get_refcount_block(struct inode *inode, u64 *ref_blkno)
  339. {
  340. int ret;
  341. struct buffer_head *di_bh = NULL;
  342. struct ocfs2_dinode *di;
  343. ret = ocfs2_read_inode_block(inode, &di_bh);
  344. if (ret) {
  345. mlog_errno(ret);
  346. goto out;
  347. }
  348. BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
  349. di = (struct ocfs2_dinode *)di_bh->b_data;
  350. *ref_blkno = le64_to_cpu(di->i_refcount_loc);
  351. brelse(di_bh);
  352. out:
  353. return ret;
  354. }
  355. static int __ocfs2_lock_refcount_tree(struct ocfs2_super *osb,
  356. struct ocfs2_refcount_tree *tree, int rw)
  357. {
  358. int ret;
  359. ret = ocfs2_refcount_lock(tree, rw);
  360. if (ret) {
  361. mlog_errno(ret);
  362. goto out;
  363. }
  364. if (rw)
  365. down_write(&tree->rf_sem);
  366. else
  367. down_read(&tree->rf_sem);
  368. out:
  369. return ret;
  370. }
  371. /*
  372. * Lock the refcount tree pointed by ref_blkno and return the tree.
  373. * In most case, we lock the tree and read the refcount block.
  374. * So read it here if the caller really needs it.
  375. *
  376. * If the tree has been re-created by other node, it will free the
  377. * old one and re-create it.
  378. */
  379. int ocfs2_lock_refcount_tree(struct ocfs2_super *osb,
  380. u64 ref_blkno, int rw,
  381. struct ocfs2_refcount_tree **ret_tree,
  382. struct buffer_head **ref_bh)
  383. {
  384. int ret, delete_tree = 0;
  385. struct ocfs2_refcount_tree *tree = NULL;
  386. struct buffer_head *ref_root_bh = NULL;
  387. struct ocfs2_refcount_block *rb;
  388. again:
  389. ret = ocfs2_get_refcount_tree(osb, ref_blkno, &tree);
  390. if (ret) {
  391. mlog_errno(ret);
  392. return ret;
  393. }
  394. ocfs2_refcount_tree_get(tree);
  395. ret = __ocfs2_lock_refcount_tree(osb, tree, rw);
  396. if (ret) {
  397. mlog_errno(ret);
  398. ocfs2_refcount_tree_put(tree);
  399. goto out;
  400. }
  401. ret = ocfs2_read_refcount_block(&tree->rf_ci, tree->rf_blkno,
  402. &ref_root_bh);
  403. if (ret) {
  404. mlog_errno(ret);
  405. ocfs2_unlock_refcount_tree(osb, tree, rw);
  406. ocfs2_refcount_tree_put(tree);
  407. goto out;
  408. }
  409. rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
  410. /*
  411. * If the refcount block has been freed and re-created, we may need
  412. * to recreate the refcount tree also.
  413. *
  414. * Here we just remove the tree from the rb-tree, and the last
  415. * kref holder will unlock and delete this refcount_tree.
  416. * Then we goto "again" and ocfs2_get_refcount_tree will create
  417. * the new refcount tree for us.
  418. */
  419. if (tree->rf_generation != le32_to_cpu(rb->rf_generation)) {
  420. if (!tree->rf_removed) {
  421. ocfs2_erase_refcount_tree_from_list(osb, tree);
  422. tree->rf_removed = 1;
  423. delete_tree = 1;
  424. }
  425. ocfs2_unlock_refcount_tree(osb, tree, rw);
  426. /*
  427. * We get an extra reference when we create the refcount
  428. * tree, so another put will destroy it.
  429. */
  430. if (delete_tree)
  431. ocfs2_refcount_tree_put(tree);
  432. brelse(ref_root_bh);
  433. ref_root_bh = NULL;
  434. goto again;
  435. }
  436. *ret_tree = tree;
  437. if (ref_bh) {
  438. *ref_bh = ref_root_bh;
  439. ref_root_bh = NULL;
  440. }
  441. out:
  442. brelse(ref_root_bh);
  443. return ret;
  444. }
  445. void ocfs2_unlock_refcount_tree(struct ocfs2_super *osb,
  446. struct ocfs2_refcount_tree *tree, int rw)
  447. {
  448. if (rw)
  449. up_write(&tree->rf_sem);
  450. else
  451. up_read(&tree->rf_sem);
  452. ocfs2_refcount_unlock(tree, rw);
  453. ocfs2_refcount_tree_put(tree);
  454. }
  455. void ocfs2_purge_refcount_trees(struct ocfs2_super *osb)
  456. {
  457. struct rb_node *node;
  458. struct ocfs2_refcount_tree *tree;
  459. struct rb_root *root = &osb->osb_rf_lock_tree;
  460. while ((node = rb_last(root)) != NULL) {
  461. tree = rb_entry(node, struct ocfs2_refcount_tree, rf_node);
  462. mlog(0, "Purge tree %llu\n",
  463. (unsigned long long) tree->rf_blkno);
  464. rb_erase(&tree->rf_node, root);
  465. ocfs2_free_refcount_tree(tree);
  466. }
  467. }
  468. /*
  469. * Create a refcount tree for an inode.
  470. * We take for granted that the inode is already locked.
  471. */
  472. static int ocfs2_create_refcount_tree(struct inode *inode,
  473. struct buffer_head *di_bh)
  474. {
  475. int ret;
  476. handle_t *handle = NULL;
  477. struct ocfs2_alloc_context *meta_ac = NULL;
  478. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  479. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  480. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  481. struct buffer_head *new_bh = NULL;
  482. struct ocfs2_refcount_block *rb;
  483. struct ocfs2_refcount_tree *new_tree = NULL, *tree = NULL;
  484. u16 suballoc_bit_start;
  485. u32 num_got;
  486. u64 suballoc_loc, first_blkno;
  487. BUG_ON(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL);
  488. mlog(0, "create tree for inode %lu\n", inode->i_ino);
  489. ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
  490. if (ret) {
  491. mlog_errno(ret);
  492. goto out;
  493. }
  494. handle = ocfs2_start_trans(osb, OCFS2_REFCOUNT_TREE_CREATE_CREDITS);
  495. if (IS_ERR(handle)) {
  496. ret = PTR_ERR(handle);
  497. mlog_errno(ret);
  498. goto out;
  499. }
  500. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  501. OCFS2_JOURNAL_ACCESS_WRITE);
  502. if (ret) {
  503. mlog_errno(ret);
  504. goto out_commit;
  505. }
  506. ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc,
  507. &suballoc_bit_start, &num_got,
  508. &first_blkno);
  509. if (ret) {
  510. mlog_errno(ret);
  511. goto out_commit;
  512. }
  513. new_tree = ocfs2_allocate_refcount_tree(osb, first_blkno);
  514. if (!new_tree) {
  515. ret = -ENOMEM;
  516. mlog_errno(ret);
  517. goto out_commit;
  518. }
  519. new_bh = sb_getblk(inode->i_sb, first_blkno);
  520. ocfs2_set_new_buffer_uptodate(&new_tree->rf_ci, new_bh);
  521. ret = ocfs2_journal_access_rb(handle, &new_tree->rf_ci, new_bh,
  522. OCFS2_JOURNAL_ACCESS_CREATE);
  523. if (ret) {
  524. mlog_errno(ret);
  525. goto out_commit;
  526. }
  527. /* Initialize ocfs2_refcount_block. */
  528. rb = (struct ocfs2_refcount_block *)new_bh->b_data;
  529. memset(rb, 0, inode->i_sb->s_blocksize);
  530. strcpy((void *)rb, OCFS2_REFCOUNT_BLOCK_SIGNATURE);
  531. rb->rf_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
  532. rb->rf_suballoc_loc = cpu_to_le64(suballoc_loc);
  533. rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
  534. rb->rf_fs_generation = cpu_to_le32(osb->fs_generation);
  535. rb->rf_blkno = cpu_to_le64(first_blkno);
  536. rb->rf_count = cpu_to_le32(1);
  537. rb->rf_records.rl_count =
  538. cpu_to_le16(ocfs2_refcount_recs_per_rb(osb->sb));
  539. spin_lock(&osb->osb_lock);
  540. rb->rf_generation = osb->s_next_generation++;
  541. spin_unlock(&osb->osb_lock);
  542. ocfs2_journal_dirty(handle, new_bh);
  543. spin_lock(&oi->ip_lock);
  544. oi->ip_dyn_features |= OCFS2_HAS_REFCOUNT_FL;
  545. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  546. di->i_refcount_loc = cpu_to_le64(first_blkno);
  547. spin_unlock(&oi->ip_lock);
  548. mlog(0, "created tree for inode %lu, refblock %llu\n",
  549. inode->i_ino, (unsigned long long)first_blkno);
  550. ocfs2_journal_dirty(handle, di_bh);
  551. /*
  552. * We have to init the tree lock here since it will use
  553. * the generation number to create it.
  554. */
  555. new_tree->rf_generation = le32_to_cpu(rb->rf_generation);
  556. ocfs2_init_refcount_tree_lock(osb, new_tree, first_blkno,
  557. new_tree->rf_generation);
  558. spin_lock(&osb->osb_lock);
  559. tree = ocfs2_find_refcount_tree(osb, first_blkno);
  560. /*
  561. * We've just created a new refcount tree in this block. If
  562. * we found a refcount tree on the ocfs2_super, it must be
  563. * one we just deleted. We free the old tree before
  564. * inserting the new tree.
  565. */
  566. BUG_ON(tree && tree->rf_generation == new_tree->rf_generation);
  567. if (tree)
  568. ocfs2_erase_refcount_tree_from_list_no_lock(osb, tree);
  569. ocfs2_insert_refcount_tree(osb, new_tree);
  570. spin_unlock(&osb->osb_lock);
  571. new_tree = NULL;
  572. if (tree)
  573. ocfs2_refcount_tree_put(tree);
  574. out_commit:
  575. ocfs2_commit_trans(osb, handle);
  576. out:
  577. if (new_tree) {
  578. ocfs2_metadata_cache_exit(&new_tree->rf_ci);
  579. kfree(new_tree);
  580. }
  581. brelse(new_bh);
  582. if (meta_ac)
  583. ocfs2_free_alloc_context(meta_ac);
  584. return ret;
  585. }
  586. static int ocfs2_set_refcount_tree(struct inode *inode,
  587. struct buffer_head *di_bh,
  588. u64 refcount_loc)
  589. {
  590. int ret;
  591. handle_t *handle = NULL;
  592. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  593. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  594. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  595. struct buffer_head *ref_root_bh = NULL;
  596. struct ocfs2_refcount_block *rb;
  597. struct ocfs2_refcount_tree *ref_tree;
  598. BUG_ON(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL);
  599. ret = ocfs2_lock_refcount_tree(osb, refcount_loc, 1,
  600. &ref_tree, &ref_root_bh);
  601. if (ret) {
  602. mlog_errno(ret);
  603. return ret;
  604. }
  605. handle = ocfs2_start_trans(osb, OCFS2_REFCOUNT_TREE_SET_CREDITS);
  606. if (IS_ERR(handle)) {
  607. ret = PTR_ERR(handle);
  608. mlog_errno(ret);
  609. goto out;
  610. }
  611. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  612. OCFS2_JOURNAL_ACCESS_WRITE);
  613. if (ret) {
  614. mlog_errno(ret);
  615. goto out_commit;
  616. }
  617. ret = ocfs2_journal_access_rb(handle, &ref_tree->rf_ci, ref_root_bh,
  618. OCFS2_JOURNAL_ACCESS_WRITE);
  619. if (ret) {
  620. mlog_errno(ret);
  621. goto out_commit;
  622. }
  623. rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
  624. le32_add_cpu(&rb->rf_count, 1);
  625. ocfs2_journal_dirty(handle, ref_root_bh);
  626. spin_lock(&oi->ip_lock);
  627. oi->ip_dyn_features |= OCFS2_HAS_REFCOUNT_FL;
  628. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  629. di->i_refcount_loc = cpu_to_le64(refcount_loc);
  630. spin_unlock(&oi->ip_lock);
  631. ocfs2_journal_dirty(handle, di_bh);
  632. out_commit:
  633. ocfs2_commit_trans(osb, handle);
  634. out:
  635. ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
  636. brelse(ref_root_bh);
  637. return ret;
  638. }
  639. int ocfs2_remove_refcount_tree(struct inode *inode, struct buffer_head *di_bh)
  640. {
  641. int ret, delete_tree = 0;
  642. handle_t *handle = NULL;
  643. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  644. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  645. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  646. struct ocfs2_refcount_block *rb;
  647. struct inode *alloc_inode = NULL;
  648. struct buffer_head *alloc_bh = NULL;
  649. struct buffer_head *blk_bh = NULL;
  650. struct ocfs2_refcount_tree *ref_tree;
  651. int credits = OCFS2_REFCOUNT_TREE_REMOVE_CREDITS;
  652. u64 blk = 0, bg_blkno = 0, ref_blkno = le64_to_cpu(di->i_refcount_loc);
  653. u16 bit = 0;
  654. if (!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL))
  655. return 0;
  656. BUG_ON(!ref_blkno);
  657. ret = ocfs2_lock_refcount_tree(osb, ref_blkno, 1, &ref_tree, &blk_bh);
  658. if (ret) {
  659. mlog_errno(ret);
  660. return ret;
  661. }
  662. rb = (struct ocfs2_refcount_block *)blk_bh->b_data;
  663. /*
  664. * If we are the last user, we need to free the block.
  665. * So lock the allocator ahead.
  666. */
  667. if (le32_to_cpu(rb->rf_count) == 1) {
  668. blk = le64_to_cpu(rb->rf_blkno);
  669. bit = le16_to_cpu(rb->rf_suballoc_bit);
  670. if (rb->rf_suballoc_loc)
  671. bg_blkno = le64_to_cpu(rb->rf_suballoc_loc);
  672. else
  673. bg_blkno = ocfs2_which_suballoc_group(blk, bit);
  674. alloc_inode = ocfs2_get_system_file_inode(osb,
  675. EXTENT_ALLOC_SYSTEM_INODE,
  676. le16_to_cpu(rb->rf_suballoc_slot));
  677. if (!alloc_inode) {
  678. ret = -ENOMEM;
  679. mlog_errno(ret);
  680. goto out;
  681. }
  682. mutex_lock(&alloc_inode->i_mutex);
  683. ret = ocfs2_inode_lock(alloc_inode, &alloc_bh, 1);
  684. if (ret) {
  685. mlog_errno(ret);
  686. goto out_mutex;
  687. }
  688. credits += OCFS2_SUBALLOC_FREE;
  689. }
  690. handle = ocfs2_start_trans(osb, credits);
  691. if (IS_ERR(handle)) {
  692. ret = PTR_ERR(handle);
  693. mlog_errno(ret);
  694. goto out_unlock;
  695. }
  696. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  697. OCFS2_JOURNAL_ACCESS_WRITE);
  698. if (ret) {
  699. mlog_errno(ret);
  700. goto out_commit;
  701. }
  702. ret = ocfs2_journal_access_rb(handle, &ref_tree->rf_ci, blk_bh,
  703. OCFS2_JOURNAL_ACCESS_WRITE);
  704. if (ret) {
  705. mlog_errno(ret);
  706. goto out_commit;
  707. }
  708. spin_lock(&oi->ip_lock);
  709. oi->ip_dyn_features &= ~OCFS2_HAS_REFCOUNT_FL;
  710. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  711. di->i_refcount_loc = 0;
  712. spin_unlock(&oi->ip_lock);
  713. ocfs2_journal_dirty(handle, di_bh);
  714. le32_add_cpu(&rb->rf_count , -1);
  715. ocfs2_journal_dirty(handle, blk_bh);
  716. if (!rb->rf_count) {
  717. delete_tree = 1;
  718. ocfs2_erase_refcount_tree_from_list(osb, ref_tree);
  719. ret = ocfs2_free_suballoc_bits(handle, alloc_inode,
  720. alloc_bh, bit, bg_blkno, 1);
  721. if (ret)
  722. mlog_errno(ret);
  723. }
  724. out_commit:
  725. ocfs2_commit_trans(osb, handle);
  726. out_unlock:
  727. if (alloc_inode) {
  728. ocfs2_inode_unlock(alloc_inode, 1);
  729. brelse(alloc_bh);
  730. }
  731. out_mutex:
  732. if (alloc_inode) {
  733. mutex_unlock(&alloc_inode->i_mutex);
  734. iput(alloc_inode);
  735. }
  736. out:
  737. ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
  738. if (delete_tree)
  739. ocfs2_refcount_tree_put(ref_tree);
  740. brelse(blk_bh);
  741. return ret;
  742. }
  743. static void ocfs2_find_refcount_rec_in_rl(struct ocfs2_caching_info *ci,
  744. struct buffer_head *ref_leaf_bh,
  745. u64 cpos, unsigned int len,
  746. struct ocfs2_refcount_rec *ret_rec,
  747. int *index)
  748. {
  749. int i = 0;
  750. struct ocfs2_refcount_block *rb =
  751. (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
  752. struct ocfs2_refcount_rec *rec = NULL;
  753. for (; i < le16_to_cpu(rb->rf_records.rl_used); i++) {
  754. rec = &rb->rf_records.rl_recs[i];
  755. if (le64_to_cpu(rec->r_cpos) +
  756. le32_to_cpu(rec->r_clusters) <= cpos)
  757. continue;
  758. else if (le64_to_cpu(rec->r_cpos) > cpos)
  759. break;
  760. /* ok, cpos fail in this rec. Just return. */
  761. if (ret_rec)
  762. *ret_rec = *rec;
  763. goto out;
  764. }
  765. if (ret_rec) {
  766. /* We meet with a hole here, so fake the rec. */
  767. ret_rec->r_cpos = cpu_to_le64(cpos);
  768. ret_rec->r_refcount = 0;
  769. if (i < le16_to_cpu(rb->rf_records.rl_used) &&
  770. le64_to_cpu(rec->r_cpos) < cpos + len)
  771. ret_rec->r_clusters =
  772. cpu_to_le32(le64_to_cpu(rec->r_cpos) - cpos);
  773. else
  774. ret_rec->r_clusters = cpu_to_le32(len);
  775. }
  776. out:
  777. *index = i;
  778. }
  779. /*
  780. * Try to remove refcount tree. The mechanism is:
  781. * 1) Check whether i_clusters == 0, if no, exit.
  782. * 2) check whether we have i_xattr_loc in dinode. if yes, exit.
  783. * 3) Check whether we have inline xattr stored outside, if yes, exit.
  784. * 4) Remove the tree.
  785. */
  786. int ocfs2_try_remove_refcount_tree(struct inode *inode,
  787. struct buffer_head *di_bh)
  788. {
  789. int ret;
  790. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  791. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  792. down_write(&oi->ip_xattr_sem);
  793. down_write(&oi->ip_alloc_sem);
  794. if (oi->ip_clusters)
  795. goto out;
  796. if ((oi->ip_dyn_features & OCFS2_HAS_XATTR_FL) && di->i_xattr_loc)
  797. goto out;
  798. if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL &&
  799. ocfs2_has_inline_xattr_value_outside(inode, di))
  800. goto out;
  801. ret = ocfs2_remove_refcount_tree(inode, di_bh);
  802. if (ret)
  803. mlog_errno(ret);
  804. out:
  805. up_write(&oi->ip_alloc_sem);
  806. up_write(&oi->ip_xattr_sem);
  807. return 0;
  808. }
  809. /*
  810. * Find the end range for a leaf refcount block indicated by
  811. * el->l_recs[index].e_blkno.
  812. */
  813. static int ocfs2_get_refcount_cpos_end(struct ocfs2_caching_info *ci,
  814. struct buffer_head *ref_root_bh,
  815. struct ocfs2_extent_block *eb,
  816. struct ocfs2_extent_list *el,
  817. int index, u32 *cpos_end)
  818. {
  819. int ret, i, subtree_root;
  820. u32 cpos;
  821. u64 blkno;
  822. struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
  823. struct ocfs2_path *left_path = NULL, *right_path = NULL;
  824. struct ocfs2_extent_tree et;
  825. struct ocfs2_extent_list *tmp_el;
  826. if (index < le16_to_cpu(el->l_next_free_rec) - 1) {
  827. /*
  828. * We have a extent rec after index, so just use the e_cpos
  829. * of the next extent rec.
  830. */
  831. *cpos_end = le32_to_cpu(el->l_recs[index+1].e_cpos);
  832. return 0;
  833. }
  834. if (!eb || (eb && !eb->h_next_leaf_blk)) {
  835. /*
  836. * We are the last extent rec, so any high cpos should
  837. * be stored in this leaf refcount block.
  838. */
  839. *cpos_end = UINT_MAX;
  840. return 0;
  841. }
  842. /*
  843. * If the extent block isn't the last one, we have to find
  844. * the subtree root between this extent block and the next
  845. * leaf extent block and get the corresponding e_cpos from
  846. * the subroot. Otherwise we may corrupt the b-tree.
  847. */
  848. ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
  849. left_path = ocfs2_new_path_from_et(&et);
  850. if (!left_path) {
  851. ret = -ENOMEM;
  852. mlog_errno(ret);
  853. goto out;
  854. }
  855. cpos = le32_to_cpu(eb->h_list.l_recs[index].e_cpos);
  856. ret = ocfs2_find_path(ci, left_path, cpos);
  857. if (ret) {
  858. mlog_errno(ret);
  859. goto out;
  860. }
  861. right_path = ocfs2_new_path_from_path(left_path);
  862. if (!right_path) {
  863. ret = -ENOMEM;
  864. mlog_errno(ret);
  865. goto out;
  866. }
  867. ret = ocfs2_find_cpos_for_right_leaf(sb, left_path, &cpos);
  868. if (ret) {
  869. mlog_errno(ret);
  870. goto out;
  871. }
  872. ret = ocfs2_find_path(ci, right_path, cpos);
  873. if (ret) {
  874. mlog_errno(ret);
  875. goto out;
  876. }
  877. subtree_root = ocfs2_find_subtree_root(&et, left_path,
  878. right_path);
  879. tmp_el = left_path->p_node[subtree_root].el;
  880. blkno = left_path->p_node[subtree_root+1].bh->b_blocknr;
  881. for (i = 0; i < le32_to_cpu(tmp_el->l_next_free_rec); i++) {
  882. if (le64_to_cpu(tmp_el->l_recs[i].e_blkno) == blkno) {
  883. *cpos_end = le32_to_cpu(tmp_el->l_recs[i+1].e_cpos);
  884. break;
  885. }
  886. }
  887. BUG_ON(i == le32_to_cpu(tmp_el->l_next_free_rec));
  888. out:
  889. ocfs2_free_path(left_path);
  890. ocfs2_free_path(right_path);
  891. return ret;
  892. }
  893. /*
  894. * Given a cpos and len, try to find the refcount record which contains cpos.
  895. * 1. If cpos can be found in one refcount record, return the record.
  896. * 2. If cpos can't be found, return a fake record which start from cpos
  897. * and end at a small value between cpos+len and start of the next record.
  898. * This fake record has r_refcount = 0.
  899. */
  900. static int ocfs2_get_refcount_rec(struct ocfs2_caching_info *ci,
  901. struct buffer_head *ref_root_bh,
  902. u64 cpos, unsigned int len,
  903. struct ocfs2_refcount_rec *ret_rec,
  904. int *index,
  905. struct buffer_head **ret_bh)
  906. {
  907. int ret = 0, i, found;
  908. u32 low_cpos, uninitialized_var(cpos_end);
  909. struct ocfs2_extent_list *el;
  910. struct ocfs2_extent_rec *rec = NULL;
  911. struct ocfs2_extent_block *eb = NULL;
  912. struct buffer_head *eb_bh = NULL, *ref_leaf_bh = NULL;
  913. struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
  914. struct ocfs2_refcount_block *rb =
  915. (struct ocfs2_refcount_block *)ref_root_bh->b_data;
  916. if (!(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)) {
  917. ocfs2_find_refcount_rec_in_rl(ci, ref_root_bh, cpos, len,
  918. ret_rec, index);
  919. *ret_bh = ref_root_bh;
  920. get_bh(ref_root_bh);
  921. return 0;
  922. }
  923. el = &rb->rf_list;
  924. low_cpos = cpos & OCFS2_32BIT_POS_MASK;
  925. if (el->l_tree_depth) {
  926. ret = ocfs2_find_leaf(ci, el, low_cpos, &eb_bh);
  927. if (ret) {
  928. mlog_errno(ret);
  929. goto out;
  930. }
  931. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  932. el = &eb->h_list;
  933. if (el->l_tree_depth) {
  934. ocfs2_error(sb,
  935. "refcount tree %llu has non zero tree "
  936. "depth in leaf btree tree block %llu\n",
  937. (unsigned long long)ocfs2_metadata_cache_owner(ci),
  938. (unsigned long long)eb_bh->b_blocknr);
  939. ret = -EROFS;
  940. goto out;
  941. }
  942. }
  943. found = 0;
  944. for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
  945. rec = &el->l_recs[i];
  946. if (le32_to_cpu(rec->e_cpos) <= low_cpos) {
  947. found = 1;
  948. break;
  949. }
  950. }
  951. if (found) {
  952. ret = ocfs2_get_refcount_cpos_end(ci, ref_root_bh,
  953. eb, el, i, &cpos_end);
  954. if (ret) {
  955. mlog_errno(ret);
  956. goto out;
  957. }
  958. if (cpos_end < low_cpos + len)
  959. len = cpos_end - low_cpos;
  960. }
  961. ret = ocfs2_read_refcount_block(ci, le64_to_cpu(rec->e_blkno),
  962. &ref_leaf_bh);
  963. if (ret) {
  964. mlog_errno(ret);
  965. goto out;
  966. }
  967. ocfs2_find_refcount_rec_in_rl(ci, ref_leaf_bh, cpos, len,
  968. ret_rec, index);
  969. *ret_bh = ref_leaf_bh;
  970. out:
  971. brelse(eb_bh);
  972. return ret;
  973. }
  974. enum ocfs2_ref_rec_contig {
  975. REF_CONTIG_NONE = 0,
  976. REF_CONTIG_LEFT,
  977. REF_CONTIG_RIGHT,
  978. REF_CONTIG_LEFTRIGHT,
  979. };
  980. static enum ocfs2_ref_rec_contig
  981. ocfs2_refcount_rec_adjacent(struct ocfs2_refcount_block *rb,
  982. int index)
  983. {
  984. if ((rb->rf_records.rl_recs[index].r_refcount ==
  985. rb->rf_records.rl_recs[index + 1].r_refcount) &&
  986. (le64_to_cpu(rb->rf_records.rl_recs[index].r_cpos) +
  987. le32_to_cpu(rb->rf_records.rl_recs[index].r_clusters) ==
  988. le64_to_cpu(rb->rf_records.rl_recs[index + 1].r_cpos)))
  989. return REF_CONTIG_RIGHT;
  990. return REF_CONTIG_NONE;
  991. }
  992. static enum ocfs2_ref_rec_contig
  993. ocfs2_refcount_rec_contig(struct ocfs2_refcount_block *rb,
  994. int index)
  995. {
  996. enum ocfs2_ref_rec_contig ret = REF_CONTIG_NONE;
  997. if (index < le16_to_cpu(rb->rf_records.rl_used) - 1)
  998. ret = ocfs2_refcount_rec_adjacent(rb, index);
  999. if (index > 0) {
  1000. enum ocfs2_ref_rec_contig tmp;
  1001. tmp = ocfs2_refcount_rec_adjacent(rb, index - 1);
  1002. if (tmp == REF_CONTIG_RIGHT) {
  1003. if (ret == REF_CONTIG_RIGHT)
  1004. ret = REF_CONTIG_LEFTRIGHT;
  1005. else
  1006. ret = REF_CONTIG_LEFT;
  1007. }
  1008. }
  1009. return ret;
  1010. }
  1011. static void ocfs2_rotate_refcount_rec_left(struct ocfs2_refcount_block *rb,
  1012. int index)
  1013. {
  1014. BUG_ON(rb->rf_records.rl_recs[index].r_refcount !=
  1015. rb->rf_records.rl_recs[index+1].r_refcount);
  1016. le32_add_cpu(&rb->rf_records.rl_recs[index].r_clusters,
  1017. le32_to_cpu(rb->rf_records.rl_recs[index+1].r_clusters));
  1018. if (index < le16_to_cpu(rb->rf_records.rl_used) - 2)
  1019. memmove(&rb->rf_records.rl_recs[index + 1],
  1020. &rb->rf_records.rl_recs[index + 2],
  1021. sizeof(struct ocfs2_refcount_rec) *
  1022. (le16_to_cpu(rb->rf_records.rl_used) - index - 2));
  1023. memset(&rb->rf_records.rl_recs[le16_to_cpu(rb->rf_records.rl_used) - 1],
  1024. 0, sizeof(struct ocfs2_refcount_rec));
  1025. le16_add_cpu(&rb->rf_records.rl_used, -1);
  1026. }
  1027. /*
  1028. * Merge the refcount rec if we are contiguous with the adjacent recs.
  1029. */
  1030. static void ocfs2_refcount_rec_merge(struct ocfs2_refcount_block *rb,
  1031. int index)
  1032. {
  1033. enum ocfs2_ref_rec_contig contig =
  1034. ocfs2_refcount_rec_contig(rb, index);
  1035. if (contig == REF_CONTIG_NONE)
  1036. return;
  1037. if (contig == REF_CONTIG_LEFT || contig == REF_CONTIG_LEFTRIGHT) {
  1038. BUG_ON(index == 0);
  1039. index--;
  1040. }
  1041. ocfs2_rotate_refcount_rec_left(rb, index);
  1042. if (contig == REF_CONTIG_LEFTRIGHT)
  1043. ocfs2_rotate_refcount_rec_left(rb, index);
  1044. }
  1045. /*
  1046. * Change the refcount indexed by "index" in ref_bh.
  1047. * If refcount reaches 0, remove it.
  1048. */
  1049. static int ocfs2_change_refcount_rec(handle_t *handle,
  1050. struct ocfs2_caching_info *ci,
  1051. struct buffer_head *ref_leaf_bh,
  1052. int index, int merge, int change)
  1053. {
  1054. int ret;
  1055. struct ocfs2_refcount_block *rb =
  1056. (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
  1057. struct ocfs2_refcount_list *rl = &rb->rf_records;
  1058. struct ocfs2_refcount_rec *rec = &rl->rl_recs[index];
  1059. ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
  1060. OCFS2_JOURNAL_ACCESS_WRITE);
  1061. if (ret) {
  1062. mlog_errno(ret);
  1063. goto out;
  1064. }
  1065. mlog(0, "change index %d, old count %u, change %d\n", index,
  1066. le32_to_cpu(rec->r_refcount), change);
  1067. le32_add_cpu(&rec->r_refcount, change);
  1068. if (!rec->r_refcount) {
  1069. if (index != le16_to_cpu(rl->rl_used) - 1) {
  1070. memmove(rec, rec + 1,
  1071. (le16_to_cpu(rl->rl_used) - index - 1) *
  1072. sizeof(struct ocfs2_refcount_rec));
  1073. memset(&rl->rl_recs[le16_to_cpu(rl->rl_used) - 1],
  1074. 0, sizeof(struct ocfs2_refcount_rec));
  1075. }
  1076. le16_add_cpu(&rl->rl_used, -1);
  1077. } else if (merge)
  1078. ocfs2_refcount_rec_merge(rb, index);
  1079. ocfs2_journal_dirty(handle, ref_leaf_bh);
  1080. out:
  1081. return ret;
  1082. }
  1083. static int ocfs2_expand_inline_ref_root(handle_t *handle,
  1084. struct ocfs2_caching_info *ci,
  1085. struct buffer_head *ref_root_bh,
  1086. struct buffer_head **ref_leaf_bh,
  1087. struct ocfs2_alloc_context *meta_ac)
  1088. {
  1089. int ret;
  1090. u16 suballoc_bit_start;
  1091. u32 num_got;
  1092. u64 suballoc_loc, blkno;
  1093. struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
  1094. struct buffer_head *new_bh = NULL;
  1095. struct ocfs2_refcount_block *new_rb;
  1096. struct ocfs2_refcount_block *root_rb =
  1097. (struct ocfs2_refcount_block *)ref_root_bh->b_data;
  1098. ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh,
  1099. OCFS2_JOURNAL_ACCESS_WRITE);
  1100. if (ret) {
  1101. mlog_errno(ret);
  1102. goto out;
  1103. }
  1104. ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc,
  1105. &suballoc_bit_start, &num_got,
  1106. &blkno);
  1107. if (ret) {
  1108. mlog_errno(ret);
  1109. goto out;
  1110. }
  1111. new_bh = sb_getblk(sb, blkno);
  1112. if (new_bh == NULL) {
  1113. ret = -EIO;
  1114. mlog_errno(ret);
  1115. goto out;
  1116. }
  1117. ocfs2_set_new_buffer_uptodate(ci, new_bh);
  1118. ret = ocfs2_journal_access_rb(handle, ci, new_bh,
  1119. OCFS2_JOURNAL_ACCESS_CREATE);
  1120. if (ret) {
  1121. mlog_errno(ret);
  1122. goto out;
  1123. }
  1124. /*
  1125. * Initialize ocfs2_refcount_block.
  1126. * It should contain the same information as the old root.
  1127. * so just memcpy it and change the corresponding field.
  1128. */
  1129. memcpy(new_bh->b_data, ref_root_bh->b_data, sb->s_blocksize);
  1130. new_rb = (struct ocfs2_refcount_block *)new_bh->b_data;
  1131. new_rb->rf_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
  1132. new_rb->rf_suballoc_loc = cpu_to_le64(suballoc_loc);
  1133. new_rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
  1134. new_rb->rf_blkno = cpu_to_le64(blkno);
  1135. new_rb->rf_cpos = cpu_to_le32(0);
  1136. new_rb->rf_parent = cpu_to_le64(ref_root_bh->b_blocknr);
  1137. new_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_LEAF_FL);
  1138. ocfs2_journal_dirty(handle, new_bh);
  1139. /* Now change the root. */
  1140. memset(&root_rb->rf_list, 0, sb->s_blocksize -
  1141. offsetof(struct ocfs2_refcount_block, rf_list));
  1142. root_rb->rf_list.l_count = cpu_to_le16(ocfs2_extent_recs_per_rb(sb));
  1143. root_rb->rf_clusters = cpu_to_le32(1);
  1144. root_rb->rf_list.l_next_free_rec = cpu_to_le16(1);
  1145. root_rb->rf_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
  1146. root_rb->rf_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
  1147. root_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_TREE_FL);
  1148. ocfs2_journal_dirty(handle, ref_root_bh);
  1149. mlog(0, "new leaf block %llu, used %u\n", (unsigned long long)blkno,
  1150. le16_to_cpu(new_rb->rf_records.rl_used));
  1151. *ref_leaf_bh = new_bh;
  1152. new_bh = NULL;
  1153. out:
  1154. brelse(new_bh);
  1155. return ret;
  1156. }
  1157. static int ocfs2_refcount_rec_no_intersect(struct ocfs2_refcount_rec *prev,
  1158. struct ocfs2_refcount_rec *next)
  1159. {
  1160. if (ocfs2_get_ref_rec_low_cpos(prev) + le32_to_cpu(prev->r_clusters) <=
  1161. ocfs2_get_ref_rec_low_cpos(next))
  1162. return 1;
  1163. return 0;
  1164. }
  1165. static int cmp_refcount_rec_by_low_cpos(const void *a, const void *b)
  1166. {
  1167. const struct ocfs2_refcount_rec *l = a, *r = b;
  1168. u32 l_cpos = ocfs2_get_ref_rec_low_cpos(l);
  1169. u32 r_cpos = ocfs2_get_ref_rec_low_cpos(r);
  1170. if (l_cpos > r_cpos)
  1171. return 1;
  1172. if (l_cpos < r_cpos)
  1173. return -1;
  1174. return 0;
  1175. }
  1176. static int cmp_refcount_rec_by_cpos(const void *a, const void *b)
  1177. {
  1178. const struct ocfs2_refcount_rec *l = a, *r = b;
  1179. u64 l_cpos = le64_to_cpu(l->r_cpos);
  1180. u64 r_cpos = le64_to_cpu(r->r_cpos);
  1181. if (l_cpos > r_cpos)
  1182. return 1;
  1183. if (l_cpos < r_cpos)
  1184. return -1;
  1185. return 0;
  1186. }
  1187. static void swap_refcount_rec(void *a, void *b, int size)
  1188. {
  1189. struct ocfs2_refcount_rec *l = a, *r = b, tmp;
  1190. tmp = *(struct ocfs2_refcount_rec *)l;
  1191. *(struct ocfs2_refcount_rec *)l =
  1192. *(struct ocfs2_refcount_rec *)r;
  1193. *(struct ocfs2_refcount_rec *)r = tmp;
  1194. }
  1195. /*
  1196. * The refcount cpos are ordered by their 64bit cpos,
  1197. * But we will use the low 32 bit to be the e_cpos in the b-tree.
  1198. * So we need to make sure that this pos isn't intersected with others.
  1199. *
  1200. * Note: The refcount block is already sorted by their low 32 bit cpos,
  1201. * So just try the middle pos first, and we will exit when we find
  1202. * the good position.
  1203. */
  1204. static int ocfs2_find_refcount_split_pos(struct ocfs2_refcount_list *rl,
  1205. u32 *split_pos, int *split_index)
  1206. {
  1207. int num_used = le16_to_cpu(rl->rl_used);
  1208. int delta, middle = num_used / 2;
  1209. for (delta = 0; delta < middle; delta++) {
  1210. /* Let's check delta earlier than middle */
  1211. if (ocfs2_refcount_rec_no_intersect(
  1212. &rl->rl_recs[middle - delta - 1],
  1213. &rl->rl_recs[middle - delta])) {
  1214. *split_index = middle - delta;
  1215. break;
  1216. }
  1217. /* For even counts, don't walk off the end */
  1218. if ((middle + delta + 1) == num_used)
  1219. continue;
  1220. /* Now try delta past middle */
  1221. if (ocfs2_refcount_rec_no_intersect(
  1222. &rl->rl_recs[middle + delta],
  1223. &rl->rl_recs[middle + delta + 1])) {
  1224. *split_index = middle + delta + 1;
  1225. break;
  1226. }
  1227. }
  1228. if (delta >= middle)
  1229. return -ENOSPC;
  1230. *split_pos = ocfs2_get_ref_rec_low_cpos(&rl->rl_recs[*split_index]);
  1231. return 0;
  1232. }
  1233. static int ocfs2_divide_leaf_refcount_block(struct buffer_head *ref_leaf_bh,
  1234. struct buffer_head *new_bh,
  1235. u32 *split_cpos)
  1236. {
  1237. int split_index = 0, num_moved, ret;
  1238. u32 cpos = 0;
  1239. struct ocfs2_refcount_block *rb =
  1240. (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
  1241. struct ocfs2_refcount_list *rl = &rb->rf_records;
  1242. struct ocfs2_refcount_block *new_rb =
  1243. (struct ocfs2_refcount_block *)new_bh->b_data;
  1244. struct ocfs2_refcount_list *new_rl = &new_rb->rf_records;
  1245. mlog(0, "split old leaf refcount block %llu, count = %u, used = %u\n",
  1246. (unsigned long long)ref_leaf_bh->b_blocknr,
  1247. le32_to_cpu(rl->rl_count), le32_to_cpu(rl->rl_used));
  1248. /*
  1249. * XXX: Improvement later.
  1250. * If we know all the high 32 bit cpos is the same, no need to sort.
  1251. *
  1252. * In order to make the whole process safe, we do:
  1253. * 1. sort the entries by their low 32 bit cpos first so that we can
  1254. * find the split cpos easily.
  1255. * 2. call ocfs2_insert_extent to insert the new refcount block.
  1256. * 3. move the refcount rec to the new block.
  1257. * 4. sort the entries by their 64 bit cpos.
  1258. * 5. dirty the new_rb and rb.
  1259. */
  1260. sort(&rl->rl_recs, le16_to_cpu(rl->rl_used),
  1261. sizeof(struct ocfs2_refcount_rec),
  1262. cmp_refcount_rec_by_low_cpos, swap_refcount_rec);
  1263. ret = ocfs2_find_refcount_split_pos(rl, &cpos, &split_index);
  1264. if (ret) {
  1265. mlog_errno(ret);
  1266. return ret;
  1267. }
  1268. new_rb->rf_cpos = cpu_to_le32(cpos);
  1269. /* move refcount records starting from split_index to the new block. */
  1270. num_moved = le16_to_cpu(rl->rl_used) - split_index;
  1271. memcpy(new_rl->rl_recs, &rl->rl_recs[split_index],
  1272. num_moved * sizeof(struct ocfs2_refcount_rec));
  1273. /*ok, remove the entries we just moved over to the other block. */
  1274. memset(&rl->rl_recs[split_index], 0,
  1275. num_moved * sizeof(struct ocfs2_refcount_rec));
  1276. /* change old and new rl_used accordingly. */
  1277. le16_add_cpu(&rl->rl_used, -num_moved);
  1278. new_rl->rl_used = cpu_to_le16(num_moved);
  1279. sort(&rl->rl_recs, le16_to_cpu(rl->rl_used),
  1280. sizeof(struct ocfs2_refcount_rec),
  1281. cmp_refcount_rec_by_cpos, swap_refcount_rec);
  1282. sort(&new_rl->rl_recs, le16_to_cpu(new_rl->rl_used),
  1283. sizeof(struct ocfs2_refcount_rec),
  1284. cmp_refcount_rec_by_cpos, swap_refcount_rec);
  1285. *split_cpos = cpos;
  1286. return 0;
  1287. }
  1288. static int ocfs2_new_leaf_refcount_block(handle_t *handle,
  1289. struct ocfs2_caching_info *ci,
  1290. struct buffer_head *ref_root_bh,
  1291. struct buffer_head *ref_leaf_bh,
  1292. struct ocfs2_alloc_context *meta_ac)
  1293. {
  1294. int ret;
  1295. u16 suballoc_bit_start;
  1296. u32 num_got, new_cpos;
  1297. u64 suballoc_loc, blkno;
  1298. struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
  1299. struct ocfs2_refcount_block *root_rb =
  1300. (struct ocfs2_refcount_block *)ref_root_bh->b_data;
  1301. struct buffer_head *new_bh = NULL;
  1302. struct ocfs2_refcount_block *new_rb;
  1303. struct ocfs2_extent_tree ref_et;
  1304. BUG_ON(!(le32_to_cpu(root_rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL));
  1305. ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh,
  1306. OCFS2_JOURNAL_ACCESS_WRITE);
  1307. if (ret) {
  1308. mlog_errno(ret);
  1309. goto out;
  1310. }
  1311. ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
  1312. OCFS2_JOURNAL_ACCESS_WRITE);
  1313. if (ret) {
  1314. mlog_errno(ret);
  1315. goto out;
  1316. }
  1317. ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc,
  1318. &suballoc_bit_start, &num_got,
  1319. &blkno);
  1320. if (ret) {
  1321. mlog_errno(ret);
  1322. goto out;
  1323. }
  1324. new_bh = sb_getblk(sb, blkno);
  1325. if (new_bh == NULL) {
  1326. ret = -EIO;
  1327. mlog_errno(ret);
  1328. goto out;
  1329. }
  1330. ocfs2_set_new_buffer_uptodate(ci, new_bh);
  1331. ret = ocfs2_journal_access_rb(handle, ci, new_bh,
  1332. OCFS2_JOURNAL_ACCESS_CREATE);
  1333. if (ret) {
  1334. mlog_errno(ret);
  1335. goto out;
  1336. }
  1337. /* Initialize ocfs2_refcount_block. */
  1338. new_rb = (struct ocfs2_refcount_block *)new_bh->b_data;
  1339. memset(new_rb, 0, sb->s_blocksize);
  1340. strcpy((void *)new_rb, OCFS2_REFCOUNT_BLOCK_SIGNATURE);
  1341. new_rb->rf_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
  1342. new_rb->rf_suballoc_loc = cpu_to_le64(suballoc_loc);
  1343. new_rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
  1344. new_rb->rf_fs_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
  1345. new_rb->rf_blkno = cpu_to_le64(blkno);
  1346. new_rb->rf_parent = cpu_to_le64(ref_root_bh->b_blocknr);
  1347. new_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_LEAF_FL);
  1348. new_rb->rf_records.rl_count =
  1349. cpu_to_le16(ocfs2_refcount_recs_per_rb(sb));
  1350. new_rb->rf_generation = root_rb->rf_generation;
  1351. ret = ocfs2_divide_leaf_refcount_block(ref_leaf_bh, new_bh, &new_cpos);
  1352. if (ret) {
  1353. mlog_errno(ret);
  1354. goto out;
  1355. }
  1356. ocfs2_journal_dirty(handle, ref_leaf_bh);
  1357. ocfs2_journal_dirty(handle, new_bh);
  1358. ocfs2_init_refcount_extent_tree(&ref_et, ci, ref_root_bh);
  1359. mlog(0, "insert new leaf block %llu at %u\n",
  1360. (unsigned long long)new_bh->b_blocknr, new_cpos);
  1361. /* Insert the new leaf block with the specific offset cpos. */
  1362. ret = ocfs2_insert_extent(handle, &ref_et, new_cpos, new_bh->b_blocknr,
  1363. 1, 0, meta_ac);
  1364. if (ret)
  1365. mlog_errno(ret);
  1366. out:
  1367. brelse(new_bh);
  1368. return ret;
  1369. }
  1370. static int ocfs2_expand_refcount_tree(handle_t *handle,
  1371. struct ocfs2_caching_info *ci,
  1372. struct buffer_head *ref_root_bh,
  1373. struct buffer_head *ref_leaf_bh,
  1374. struct ocfs2_alloc_context *meta_ac)
  1375. {
  1376. int ret;
  1377. struct buffer_head *expand_bh = NULL;
  1378. if (ref_root_bh == ref_leaf_bh) {
  1379. /*
  1380. * the old root bh hasn't been expanded to a b-tree,
  1381. * so expand it first.
  1382. */
  1383. ret = ocfs2_expand_inline_ref_root(handle, ci, ref_root_bh,
  1384. &expand_bh, meta_ac);
  1385. if (ret) {
  1386. mlog_errno(ret);
  1387. goto out;
  1388. }
  1389. } else {
  1390. expand_bh = ref_leaf_bh;
  1391. get_bh(expand_bh);
  1392. }
  1393. /* Now add a new refcount block into the tree.*/
  1394. ret = ocfs2_new_leaf_refcount_block(handle, ci, ref_root_bh,
  1395. expand_bh, meta_ac);
  1396. if (ret)
  1397. mlog_errno(ret);
  1398. out:
  1399. brelse(expand_bh);
  1400. return ret;
  1401. }
  1402. /*
  1403. * Adjust the extent rec in b-tree representing ref_leaf_bh.
  1404. *
  1405. * Only called when we have inserted a new refcount rec at index 0
  1406. * which means ocfs2_extent_rec.e_cpos may need some change.
  1407. */
  1408. static int ocfs2_adjust_refcount_rec(handle_t *handle,
  1409. struct ocfs2_caching_info *ci,
  1410. struct buffer_head *ref_root_bh,
  1411. struct buffer_head *ref_leaf_bh,
  1412. struct ocfs2_refcount_rec *rec)
  1413. {
  1414. int ret = 0, i;
  1415. u32 new_cpos, old_cpos;
  1416. struct ocfs2_path *path = NULL;
  1417. struct ocfs2_extent_tree et;
  1418. struct ocfs2_refcount_block *rb =
  1419. (struct ocfs2_refcount_block *)ref_root_bh->b_data;
  1420. struct ocfs2_extent_list *el;
  1421. if (!(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL))
  1422. goto out;
  1423. rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
  1424. old_cpos = le32_to_cpu(rb->rf_cpos);
  1425. new_cpos = le64_to_cpu(rec->r_cpos) & OCFS2_32BIT_POS_MASK;
  1426. if (old_cpos <= new_cpos)
  1427. goto out;
  1428. ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
  1429. path = ocfs2_new_path_from_et(&et);
  1430. if (!path) {
  1431. ret = -ENOMEM;
  1432. mlog_errno(ret);
  1433. goto out;
  1434. }
  1435. ret = ocfs2_find_path(ci, path, old_cpos);
  1436. if (ret) {
  1437. mlog_errno(ret);
  1438. goto out;
  1439. }
  1440. /*
  1441. * 2 more credits, one for the leaf refcount block, one for
  1442. * the extent block contains the extent rec.
  1443. */
  1444. ret = ocfs2_extend_trans(handle, 2);
  1445. if (ret < 0) {
  1446. mlog_errno(ret);
  1447. goto out;
  1448. }
  1449. ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
  1450. OCFS2_JOURNAL_ACCESS_WRITE);
  1451. if (ret < 0) {
  1452. mlog_errno(ret);
  1453. goto out;
  1454. }
  1455. ret = ocfs2_journal_access_eb(handle, ci, path_leaf_bh(path),
  1456. OCFS2_JOURNAL_ACCESS_WRITE);
  1457. if (ret < 0) {
  1458. mlog_errno(ret);
  1459. goto out;
  1460. }
  1461. /* change the leaf extent block first. */
  1462. el = path_leaf_el(path);
  1463. for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++)
  1464. if (le32_to_cpu(el->l_recs[i].e_cpos) == old_cpos)
  1465. break;
  1466. BUG_ON(i == le16_to_cpu(el->l_next_free_rec));
  1467. el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
  1468. /* change the r_cpos in the leaf block. */
  1469. rb->rf_cpos = cpu_to_le32(new_cpos);
  1470. ocfs2_journal_dirty(handle, path_leaf_bh(path));
  1471. ocfs2_journal_dirty(handle, ref_leaf_bh);
  1472. out:
  1473. ocfs2_free_path(path);
  1474. return ret;
  1475. }
  1476. static int ocfs2_insert_refcount_rec(handle_t *handle,
  1477. struct ocfs2_caching_info *ci,
  1478. struct buffer_head *ref_root_bh,
  1479. struct buffer_head *ref_leaf_bh,
  1480. struct ocfs2_refcount_rec *rec,
  1481. int index, int merge,
  1482. struct ocfs2_alloc_context *meta_ac)
  1483. {
  1484. int ret;
  1485. struct ocfs2_refcount_block *rb =
  1486. (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
  1487. struct ocfs2_refcount_list *rf_list = &rb->rf_records;
  1488. struct buffer_head *new_bh = NULL;
  1489. BUG_ON(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL);
  1490. if (rf_list->rl_used == rf_list->rl_count) {
  1491. u64 cpos = le64_to_cpu(rec->r_cpos);
  1492. u32 len = le32_to_cpu(rec->r_clusters);
  1493. ret = ocfs2_expand_refcount_tree(handle, ci, ref_root_bh,
  1494. ref_leaf_bh, meta_ac);
  1495. if (ret) {
  1496. mlog_errno(ret);
  1497. goto out;
  1498. }
  1499. ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
  1500. cpos, len, NULL, &index,
  1501. &new_bh);
  1502. if (ret) {
  1503. mlog_errno(ret);
  1504. goto out;
  1505. }
  1506. ref_leaf_bh = new_bh;
  1507. rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
  1508. rf_list = &rb->rf_records;
  1509. }
  1510. ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
  1511. OCFS2_JOURNAL_ACCESS_WRITE);
  1512. if (ret) {
  1513. mlog_errno(ret);
  1514. goto out;
  1515. }
  1516. if (index < le16_to_cpu(rf_list->rl_used))
  1517. memmove(&rf_list->rl_recs[index + 1],
  1518. &rf_list->rl_recs[index],
  1519. (le16_to_cpu(rf_list->rl_used) - index) *
  1520. sizeof(struct ocfs2_refcount_rec));
  1521. mlog(0, "insert refcount record start %llu, len %u, count %u "
  1522. "to leaf block %llu at index %d\n",
  1523. (unsigned long long)le64_to_cpu(rec->r_cpos),
  1524. le32_to_cpu(rec->r_clusters), le32_to_cpu(rec->r_refcount),
  1525. (unsigned long long)ref_leaf_bh->b_blocknr, index);
  1526. rf_list->rl_recs[index] = *rec;
  1527. le16_add_cpu(&rf_list->rl_used, 1);
  1528. if (merge)
  1529. ocfs2_refcount_rec_merge(rb, index);
  1530. ocfs2_journal_dirty(handle, ref_leaf_bh);
  1531. if (index == 0) {
  1532. ret = ocfs2_adjust_refcount_rec(handle, ci,
  1533. ref_root_bh,
  1534. ref_leaf_bh, rec);
  1535. if (ret)
  1536. mlog_errno(ret);
  1537. }
  1538. out:
  1539. brelse(new_bh);
  1540. return ret;
  1541. }
  1542. /*
  1543. * Split the refcount_rec indexed by "index" in ref_leaf_bh.
  1544. * This is much simple than our b-tree code.
  1545. * split_rec is the new refcount rec we want to insert.
  1546. * If split_rec->r_refcount > 0, we are changing the refcount(in case we
  1547. * increase refcount or decrease a refcount to non-zero).
  1548. * If split_rec->r_refcount == 0, we are punching a hole in current refcount
  1549. * rec( in case we decrease a refcount to zero).
  1550. */
  1551. static int ocfs2_split_refcount_rec(handle_t *handle,
  1552. struct ocfs2_caching_info *ci,
  1553. struct buffer_head *ref_root_bh,
  1554. struct buffer_head *ref_leaf_bh,
  1555. struct ocfs2_refcount_rec *split_rec,
  1556. int index, int merge,
  1557. struct ocfs2_alloc_context *meta_ac,
  1558. struct ocfs2_cached_dealloc_ctxt *dealloc)
  1559. {
  1560. int ret, recs_need;
  1561. u32 len;
  1562. struct ocfs2_refcount_block *rb =
  1563. (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
  1564. struct ocfs2_refcount_list *rf_list = &rb->rf_records;
  1565. struct ocfs2_refcount_rec *orig_rec = &rf_list->rl_recs[index];
  1566. struct ocfs2_refcount_rec *tail_rec = NULL;
  1567. struct buffer_head *new_bh = NULL;
  1568. BUG_ON(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL);
  1569. mlog(0, "original r_pos %llu, cluster %u, split %llu, cluster %u\n",
  1570. le64_to_cpu(orig_rec->r_cpos), le32_to_cpu(orig_rec->r_clusters),
  1571. le64_to_cpu(split_rec->r_cpos),
  1572. le32_to_cpu(split_rec->r_clusters));
  1573. /*
  1574. * If we just need to split the header or tail clusters,
  1575. * no more recs are needed, just split is OK.
  1576. * Otherwise we at least need one new recs.
  1577. */
  1578. if (!split_rec->r_refcount &&
  1579. (split_rec->r_cpos == orig_rec->r_cpos ||
  1580. le64_to_cpu(split_rec->r_cpos) +
  1581. le32_to_cpu(split_rec->r_clusters) ==
  1582. le64_to_cpu(orig_rec->r_cpos) + le32_to_cpu(orig_rec->r_clusters)))
  1583. recs_need = 0;
  1584. else
  1585. recs_need = 1;
  1586. /*
  1587. * We need one more rec if we split in the middle and the new rec have
  1588. * some refcount in it.
  1589. */
  1590. if (split_rec->r_refcount &&
  1591. (split_rec->r_cpos != orig_rec->r_cpos &&
  1592. le64_to_cpu(split_rec->r_cpos) +
  1593. le32_to_cpu(split_rec->r_clusters) !=
  1594. le64_to_cpu(orig_rec->r_cpos) + le32_to_cpu(orig_rec->r_clusters)))
  1595. recs_need++;
  1596. /* If the leaf block don't have enough record, expand it. */
  1597. if (le16_to_cpu(rf_list->rl_used) + recs_need >
  1598. le16_to_cpu(rf_list->rl_count)) {
  1599. struct ocfs2_refcount_rec tmp_rec;
  1600. u64 cpos = le64_to_cpu(orig_rec->r_cpos);
  1601. len = le32_to_cpu(orig_rec->r_clusters);
  1602. ret = ocfs2_expand_refcount_tree(handle, ci, ref_root_bh,
  1603. ref_leaf_bh, meta_ac);
  1604. if (ret) {
  1605. mlog_errno(ret);
  1606. goto out;
  1607. }
  1608. /*
  1609. * We have to re-get it since now cpos may be moved to
  1610. * another leaf block.
  1611. */
  1612. ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
  1613. cpos, len, &tmp_rec, &index,
  1614. &new_bh);
  1615. if (ret) {
  1616. mlog_errno(ret);
  1617. goto out;
  1618. }
  1619. ref_leaf_bh = new_bh;
  1620. rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
  1621. rf_list = &rb->rf_records;
  1622. orig_rec = &rf_list->rl_recs[index];
  1623. }
  1624. ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
  1625. OCFS2_JOURNAL_ACCESS_WRITE);
  1626. if (ret) {
  1627. mlog_errno(ret);
  1628. goto out;
  1629. }
  1630. /*
  1631. * We have calculated out how many new records we need and store
  1632. * in recs_need, so spare enough space first by moving the records
  1633. * after "index" to the end.
  1634. */
  1635. if (index != le16_to_cpu(rf_list->rl_used) - 1)
  1636. memmove(&rf_list->rl_recs[index + 1 + recs_need],
  1637. &rf_list->rl_recs[index + 1],
  1638. (le16_to_cpu(rf_list->rl_used) - index - 1) *
  1639. sizeof(struct ocfs2_refcount_rec));
  1640. len = (le64_to_cpu(orig_rec->r_cpos) +
  1641. le32_to_cpu(orig_rec->r_clusters)) -
  1642. (le64_to_cpu(split_rec->r_cpos) +
  1643. le32_to_cpu(split_rec->r_clusters));
  1644. /*
  1645. * If we have "len", the we will split in the tail and move it
  1646. * to the end of the space we have just spared.
  1647. */
  1648. if (len) {
  1649. tail_rec = &rf_list->rl_recs[index + recs_need];
  1650. memcpy(tail_rec, orig_rec, sizeof(struct ocfs2_refcount_rec));
  1651. le64_add_cpu(&tail_rec->r_cpos,
  1652. le32_to_cpu(tail_rec->r_clusters) - len);
  1653. tail_rec->r_clusters = cpu_to_le32(len);
  1654. }
  1655. /*
  1656. * If the split pos isn't the same as the original one, we need to
  1657. * split in the head.
  1658. *
  1659. * Note: We have the chance that split_rec.r_refcount = 0,
  1660. * recs_need = 0 and len > 0, which means we just cut the head from
  1661. * the orig_rec and in that case we have done some modification in
  1662. * orig_rec above, so the check for r_cpos is faked.
  1663. */
  1664. if (split_rec->r_cpos != orig_rec->r_cpos && tail_rec != orig_rec) {
  1665. len = le64_to_cpu(split_rec->r_cpos) -
  1666. le64_to_cpu(orig_rec->r_cpos);
  1667. orig_rec->r_clusters = cpu_to_le32(len);
  1668. index++;
  1669. }
  1670. le16_add_cpu(&rf_list->rl_used, recs_need);
  1671. if (split_rec->r_refcount) {
  1672. rf_list->rl_recs[index] = *split_rec;
  1673. mlog(0, "insert refcount record start %llu, len %u, count %u "
  1674. "to leaf block %llu at index %d\n",
  1675. (unsigned long long)le64_to_cpu(split_rec->r_cpos),
  1676. le32_to_cpu(split_rec->r_clusters),
  1677. le32_to_cpu(split_rec->r_refcount),
  1678. (unsigned long long)ref_leaf_bh->b_blocknr, index);
  1679. if (merge)
  1680. ocfs2_refcount_rec_merge(rb, index);
  1681. }
  1682. ocfs2_journal_dirty(handle, ref_leaf_bh);
  1683. out:
  1684. brelse(new_bh);
  1685. return ret;
  1686. }
  1687. static int __ocfs2_increase_refcount(handle_t *handle,
  1688. struct ocfs2_caching_info *ci,
  1689. struct buffer_head *ref_root_bh,
  1690. u64 cpos, u32 len, int merge,
  1691. struct ocfs2_alloc_context *meta_ac,
  1692. struct ocfs2_cached_dealloc_ctxt *dealloc)
  1693. {
  1694. int ret = 0, index;
  1695. struct buffer_head *ref_leaf_bh = NULL;
  1696. struct ocfs2_refcount_rec rec;
  1697. unsigned int set_len = 0;
  1698. mlog(0, "Tree owner %llu, add refcount start %llu, len %u\n",
  1699. (unsigned long long)ocfs2_metadata_cache_owner(ci),
  1700. (unsigned long long)cpos, len);
  1701. while (len) {
  1702. ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
  1703. cpos, len, &rec, &index,
  1704. &ref_leaf_bh);
  1705. if (ret) {
  1706. mlog_errno(ret);
  1707. goto out;
  1708. }
  1709. set_len = le32_to_cpu(rec.r_clusters);
  1710. /*
  1711. * Here we may meet with 3 situations:
  1712. *
  1713. * 1. If we find an already existing record, and the length
  1714. * is the same, cool, we just need to increase the r_refcount
  1715. * and it is OK.
  1716. * 2. If we find a hole, just insert it with r_refcount = 1.
  1717. * 3. If we are in the middle of one extent record, split
  1718. * it.
  1719. */
  1720. if (rec.r_refcount && le64_to_cpu(rec.r_cpos) == cpos &&
  1721. set_len <= len) {
  1722. mlog(0, "increase refcount rec, start %llu, len %u, "
  1723. "count %u\n", (unsigned long long)cpos, set_len,
  1724. le32_to_cpu(rec.r_refcount));
  1725. ret = ocfs2_change_refcount_rec(handle, ci,
  1726. ref_leaf_bh, index,
  1727. merge, 1);
  1728. if (ret) {
  1729. mlog_errno(ret);
  1730. goto out;
  1731. }
  1732. } else if (!rec.r_refcount) {
  1733. rec.r_refcount = cpu_to_le32(1);
  1734. mlog(0, "insert refcount rec, start %llu, len %u\n",
  1735. (unsigned long long)le64_to_cpu(rec.r_cpos),
  1736. set_len);
  1737. ret = ocfs2_insert_refcount_rec(handle, ci, ref_root_bh,
  1738. ref_leaf_bh,
  1739. &rec, index,
  1740. merge, meta_ac);
  1741. if (ret) {
  1742. mlog_errno(ret);
  1743. goto out;
  1744. }
  1745. } else {
  1746. set_len = min((u64)(cpos + len),
  1747. le64_to_cpu(rec.r_cpos) + set_len) - cpos;
  1748. rec.r_cpos = cpu_to_le64(cpos);
  1749. rec.r_clusters = cpu_to_le32(set_len);
  1750. le32_add_cpu(&rec.r_refcount, 1);
  1751. mlog(0, "split refcount rec, start %llu, "
  1752. "len %u, count %u\n",
  1753. (unsigned long long)le64_to_cpu(rec.r_cpos),
  1754. set_len, le32_to_cpu(rec.r_refcount));
  1755. ret = ocfs2_split_refcount_rec(handle, ci,
  1756. ref_root_bh, ref_leaf_bh,
  1757. &rec, index, merge,
  1758. meta_ac, dealloc);
  1759. if (ret) {
  1760. mlog_errno(ret);
  1761. goto out;
  1762. }
  1763. }
  1764. cpos += set_len;
  1765. len -= set_len;
  1766. brelse(ref_leaf_bh);
  1767. ref_leaf_bh = NULL;
  1768. }
  1769. out:
  1770. brelse(ref_leaf_bh);
  1771. return ret;
  1772. }
  1773. static int ocfs2_remove_refcount_extent(handle_t *handle,
  1774. struct ocfs2_caching_info *ci,
  1775. struct buffer_head *ref_root_bh,
  1776. struct buffer_head *ref_leaf_bh,
  1777. struct ocfs2_alloc_context *meta_ac,
  1778. struct ocfs2_cached_dealloc_ctxt *dealloc)
  1779. {
  1780. int ret;
  1781. struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
  1782. struct ocfs2_refcount_block *rb =
  1783. (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
  1784. struct ocfs2_extent_tree et;
  1785. BUG_ON(rb->rf_records.rl_used);
  1786. ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
  1787. ret = ocfs2_remove_extent(handle, &et, le32_to_cpu(rb->rf_cpos),
  1788. 1, meta_ac, dealloc);
  1789. if (ret) {
  1790. mlog_errno(ret);
  1791. goto out;
  1792. }
  1793. ocfs2_remove_from_cache(ci, ref_leaf_bh);
  1794. /*
  1795. * add the freed block to the dealloc so that it will be freed
  1796. * when we run dealloc.
  1797. */
  1798. ret = ocfs2_cache_block_dealloc(dealloc, EXTENT_ALLOC_SYSTEM_INODE,
  1799. le16_to_cpu(rb->rf_suballoc_slot),
  1800. le64_to_cpu(rb->rf_suballoc_loc),
  1801. le64_to_cpu(rb->rf_blkno),
  1802. le16_to_cpu(rb->rf_suballoc_bit));
  1803. if (ret) {
  1804. mlog_errno(ret);
  1805. goto out;
  1806. }
  1807. ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh,
  1808. OCFS2_JOURNAL_ACCESS_WRITE);
  1809. if (ret) {
  1810. mlog_errno(ret);
  1811. goto out;
  1812. }
  1813. rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
  1814. le32_add_cpu(&rb->rf_clusters, -1);
  1815. /*
  1816. * check whether we need to restore the root refcount block if
  1817. * there is no leaf extent block at atll.
  1818. */
  1819. if (!rb->rf_list.l_next_free_rec) {
  1820. BUG_ON(rb->rf_clusters);
  1821. mlog(0, "reset refcount tree root %llu to be a record block.\n",
  1822. (unsigned long long)ref_root_bh->b_blocknr);
  1823. rb->rf_flags = 0;
  1824. rb->rf_parent = 0;
  1825. rb->rf_cpos = 0;
  1826. memset(&rb->rf_records, 0, sb->s_blocksize -
  1827. offsetof(struct ocfs2_refcount_block, rf_records));
  1828. rb->rf_records.rl_count =
  1829. cpu_to_le16(ocfs2_refcount_recs_per_rb(sb));
  1830. }
  1831. ocfs2_journal_dirty(handle, ref_root_bh);
  1832. out:
  1833. return ret;
  1834. }
  1835. int ocfs2_increase_refcount(handle_t *handle,
  1836. struct ocfs2_caching_info *ci,
  1837. struct buffer_head *ref_root_bh,
  1838. u64 cpos, u32 len,
  1839. struct ocfs2_alloc_context *meta_ac,
  1840. struct ocfs2_cached_dealloc_ctxt *dealloc)
  1841. {
  1842. return __ocfs2_increase_refcount(handle, ci, ref_root_bh,
  1843. cpos, len, 1,
  1844. meta_ac, dealloc);
  1845. }
  1846. static int ocfs2_decrease_refcount_rec(handle_t *handle,
  1847. struct ocfs2_caching_info *ci,
  1848. struct buffer_head *ref_root_bh,
  1849. struct buffer_head *ref_leaf_bh,
  1850. int index, u64 cpos, unsigned int len,
  1851. struct ocfs2_alloc_context *meta_ac,
  1852. struct ocfs2_cached_dealloc_ctxt *dealloc)
  1853. {
  1854. int ret;
  1855. struct ocfs2_refcount_block *rb =
  1856. (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
  1857. struct ocfs2_refcount_rec *rec = &rb->rf_records.rl_recs[index];
  1858. BUG_ON(cpos < le64_to_cpu(rec->r_cpos));
  1859. BUG_ON(cpos + len >
  1860. le64_to_cpu(rec->r_cpos) + le32_to_cpu(rec->r_clusters));
  1861. if (cpos == le64_to_cpu(rec->r_cpos) &&
  1862. len == le32_to_cpu(rec->r_clusters))
  1863. ret = ocfs2_change_refcount_rec(handle, ci,
  1864. ref_leaf_bh, index, 1, -1);
  1865. else {
  1866. struct ocfs2_refcount_rec split = *rec;
  1867. split.r_cpos = cpu_to_le64(cpos);
  1868. split.r_clusters = cpu_to_le32(len);
  1869. le32_add_cpu(&split.r_refcount, -1);
  1870. mlog(0, "split refcount rec, start %llu, "
  1871. "len %u, count %u, original start %llu, len %u\n",
  1872. (unsigned long long)le64_to_cpu(split.r_cpos),
  1873. len, le32_to_cpu(split.r_refcount),
  1874. (unsigned long long)le64_to_cpu(rec->r_cpos),
  1875. le32_to_cpu(rec->r_clusters));
  1876. ret = ocfs2_split_refcount_rec(handle, ci,
  1877. ref_root_bh, ref_leaf_bh,
  1878. &split, index, 1,
  1879. meta_ac, dealloc);
  1880. }
  1881. if (ret) {
  1882. mlog_errno(ret);
  1883. goto out;
  1884. }
  1885. /* Remove the leaf refcount block if it contains no refcount record. */
  1886. if (!rb->rf_records.rl_used && ref_leaf_bh != ref_root_bh) {
  1887. ret = ocfs2_remove_refcount_extent(handle, ci, ref_root_bh,
  1888. ref_leaf_bh, meta_ac,
  1889. dealloc);
  1890. if (ret)
  1891. mlog_errno(ret);
  1892. }
  1893. out:
  1894. return ret;
  1895. }
  1896. static int __ocfs2_decrease_refcount(handle_t *handle,
  1897. struct ocfs2_caching_info *ci,
  1898. struct buffer_head *ref_root_bh,
  1899. u64 cpos, u32 len,
  1900. struct ocfs2_alloc_context *meta_ac,
  1901. struct ocfs2_cached_dealloc_ctxt *dealloc,
  1902. int delete)
  1903. {
  1904. int ret = 0, index = 0;
  1905. struct ocfs2_refcount_rec rec;
  1906. unsigned int r_count = 0, r_len;
  1907. struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
  1908. struct buffer_head *ref_leaf_bh = NULL;
  1909. mlog(0, "Tree owner %llu, decrease refcount start %llu, "
  1910. "len %u, delete %u\n",
  1911. (unsigned long long)ocfs2_metadata_cache_owner(ci),
  1912. (unsigned long long)cpos, len, delete);
  1913. while (len) {
  1914. ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
  1915. cpos, len, &rec, &index,
  1916. &ref_leaf_bh);
  1917. if (ret) {
  1918. mlog_errno(ret);
  1919. goto out;
  1920. }
  1921. r_count = le32_to_cpu(rec.r_refcount);
  1922. BUG_ON(r_count == 0);
  1923. if (!delete)
  1924. BUG_ON(r_count > 1);
  1925. r_len = min((u64)(cpos + len), le64_to_cpu(rec.r_cpos) +
  1926. le32_to_cpu(rec.r_clusters)) - cpos;
  1927. ret = ocfs2_decrease_refcount_rec(handle, ci, ref_root_bh,
  1928. ref_leaf_bh, index,
  1929. cpos, r_len,
  1930. meta_ac, dealloc);
  1931. if (ret) {
  1932. mlog_errno(ret);
  1933. goto out;
  1934. }
  1935. if (le32_to_cpu(rec.r_refcount) == 1 && delete) {
  1936. ret = ocfs2_cache_cluster_dealloc(dealloc,
  1937. ocfs2_clusters_to_blocks(sb, cpos),
  1938. r_len);
  1939. if (ret) {
  1940. mlog_errno(ret);
  1941. goto out;
  1942. }
  1943. }
  1944. cpos += r_len;
  1945. len -= r_len;
  1946. brelse(ref_leaf_bh);
  1947. ref_leaf_bh = NULL;
  1948. }
  1949. out:
  1950. brelse(ref_leaf_bh);
  1951. return ret;
  1952. }
  1953. /* Caller must hold refcount tree lock. */
  1954. int ocfs2_decrease_refcount(struct inode *inode,
  1955. handle_t *handle, u32 cpos, u32 len,
  1956. struct ocfs2_alloc_context *meta_ac,
  1957. struct ocfs2_cached_dealloc_ctxt *dealloc,
  1958. int delete)
  1959. {
  1960. int ret;
  1961. u64 ref_blkno;
  1962. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1963. struct buffer_head *ref_root_bh = NULL;
  1964. struct ocfs2_refcount_tree *tree;
  1965. BUG_ON(!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
  1966. ret = ocfs2_get_refcount_block(inode, &ref_blkno);
  1967. if (ret) {
  1968. mlog_errno(ret);
  1969. goto out;
  1970. }
  1971. ret = ocfs2_get_refcount_tree(OCFS2_SB(inode->i_sb), ref_blkno, &tree);
  1972. if (ret) {
  1973. mlog_errno(ret);
  1974. goto out;
  1975. }
  1976. ret = ocfs2_read_refcount_block(&tree->rf_ci, tree->rf_blkno,
  1977. &ref_root_bh);
  1978. if (ret) {
  1979. mlog_errno(ret);
  1980. goto out;
  1981. }
  1982. ret = __ocfs2_decrease_refcount(handle, &tree->rf_ci, ref_root_bh,
  1983. cpos, len, meta_ac, dealloc, delete);
  1984. if (ret)
  1985. mlog_errno(ret);
  1986. out:
  1987. brelse(ref_root_bh);
  1988. return ret;
  1989. }
  1990. /*
  1991. * Mark the already-existing extent at cpos as refcounted for len clusters.
  1992. * This adds the refcount extent flag.
  1993. *
  1994. * If the existing extent is larger than the request, initiate a
  1995. * split. An attempt will be made at merging with adjacent extents.
  1996. *
  1997. * The caller is responsible for passing down meta_ac if we'll need it.
  1998. */
  1999. static int ocfs2_mark_extent_refcounted(struct inode *inode,
  2000. struct ocfs2_extent_tree *et,
  2001. handle_t *handle, u32 cpos,
  2002. u32 len, u32 phys,
  2003. struct ocfs2_alloc_context *meta_ac,
  2004. struct ocfs2_cached_dealloc_ctxt *dealloc)
  2005. {
  2006. int ret;
  2007. mlog(0, "Inode %lu refcount tree cpos %u, len %u, phys cluster %u\n",
  2008. inode->i_ino, cpos, len, phys);
  2009. if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) {
  2010. ocfs2_error(inode->i_sb, "Inode %lu want to use refcount "
  2011. "tree, but the feature bit is not set in the "
  2012. "super block.", inode->i_ino);
  2013. ret = -EROFS;
  2014. goto out;
  2015. }
  2016. ret = ocfs2_change_extent_flag(handle, et, cpos,
  2017. len, phys, meta_ac, dealloc,
  2018. OCFS2_EXT_REFCOUNTED, 0);
  2019. if (ret)
  2020. mlog_errno(ret);
  2021. out:
  2022. return ret;
  2023. }
  2024. /*
  2025. * Given some contiguous physical clusters, calculate what we need
  2026. * for modifying their refcount.
  2027. */
  2028. static int ocfs2_calc_refcount_meta_credits(struct super_block *sb,
  2029. struct ocfs2_caching_info *ci,
  2030. struct buffer_head *ref_root_bh,
  2031. u64 start_cpos,
  2032. u32 clusters,
  2033. int *meta_add,
  2034. int *credits)
  2035. {
  2036. int ret = 0, index, ref_blocks = 0, recs_add = 0;
  2037. u64 cpos = start_cpos;
  2038. struct ocfs2_refcount_block *rb;
  2039. struct ocfs2_refcount_rec rec;
  2040. struct buffer_head *ref_leaf_bh = NULL, *prev_bh = NULL;
  2041. u32 len;
  2042. mlog(0, "start_cpos %llu, clusters %u\n",
  2043. (unsigned long long)start_cpos, clusters);
  2044. while (clusters) {
  2045. ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
  2046. cpos, clusters, &rec,
  2047. &index, &ref_leaf_bh);
  2048. if (ret) {
  2049. mlog_errno(ret);
  2050. goto out;
  2051. }
  2052. if (ref_leaf_bh != prev_bh) {
  2053. /*
  2054. * Now we encounter a new leaf block, so calculate
  2055. * whether we need to extend the old leaf.
  2056. */
  2057. if (prev_bh) {
  2058. rb = (struct ocfs2_refcount_block *)
  2059. prev_bh->b_data;
  2060. if (le64_to_cpu(rb->rf_records.rl_used) +
  2061. recs_add >
  2062. le16_to_cpu(rb->rf_records.rl_count))
  2063. ref_blocks++;
  2064. }
  2065. recs_add = 0;
  2066. *credits += 1;
  2067. brelse(prev_bh);
  2068. prev_bh = ref_leaf_bh;
  2069. get_bh(prev_bh);
  2070. }
  2071. rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
  2072. mlog(0, "recs_add %d,cpos %llu, clusters %u, rec->r_cpos %llu,"
  2073. "rec->r_clusters %u, rec->r_refcount %u, index %d\n",
  2074. recs_add, (unsigned long long)cpos, clusters,
  2075. (unsigned long long)le64_to_cpu(rec.r_cpos),
  2076. le32_to_cpu(rec.r_clusters),
  2077. le32_to_cpu(rec.r_refcount), index);
  2078. len = min((u64)cpos + clusters, le64_to_cpu(rec.r_cpos) +
  2079. le32_to_cpu(rec.r_clusters)) - cpos;
  2080. /*
  2081. * We record all the records which will be inserted to the
  2082. * same refcount block, so that we can tell exactly whether
  2083. * we need a new refcount block or not.
  2084. *
  2085. * If we will insert a new one, this is easy and only happens
  2086. * during adding refcounted flag to the extent, so we don't
  2087. * have a chance of spliting. We just need one record.
  2088. *
  2089. * If the refcount rec already exists, that would be a little
  2090. * complicated. we may have to:
  2091. * 1) split at the beginning if the start pos isn't aligned.
  2092. * we need 1 more record in this case.
  2093. * 2) split int the end if the end pos isn't aligned.
  2094. * we need 1 more record in this case.
  2095. * 3) split in the middle because of file system fragmentation.
  2096. * we need 2 more records in this case(we can't detect this
  2097. * beforehand, so always think of the worst case).
  2098. */
  2099. if (rec.r_refcount) {
  2100. recs_add += 2;
  2101. /* Check whether we need a split at the beginning. */
  2102. if (cpos == start_cpos &&
  2103. cpos != le64_to_cpu(rec.r_cpos))
  2104. recs_add++;
  2105. /* Check whether we need a split in the end. */
  2106. if (cpos + clusters < le64_to_cpu(rec.r_cpos) +
  2107. le32_to_cpu(rec.r_clusters))
  2108. recs_add++;
  2109. } else
  2110. recs_add++;
  2111. brelse(ref_leaf_bh);
  2112. ref_leaf_bh = NULL;
  2113. clusters -= len;
  2114. cpos += len;
  2115. }
  2116. if (prev_bh) {
  2117. rb = (struct ocfs2_refcount_block *)prev_bh->b_data;
  2118. if (le64_to_cpu(rb->rf_records.rl_used) + recs_add >
  2119. le16_to_cpu(rb->rf_records.rl_count))
  2120. ref_blocks++;
  2121. *credits += 1;
  2122. }
  2123. if (!ref_blocks)
  2124. goto out;
  2125. mlog(0, "we need ref_blocks %d\n", ref_blocks);
  2126. *meta_add += ref_blocks;
  2127. *credits += ref_blocks;
  2128. /*
  2129. * So we may need ref_blocks to insert into the tree.
  2130. * That also means we need to change the b-tree and add that number
  2131. * of records since we never merge them.
  2132. * We need one more block for expansion since the new created leaf
  2133. * block is also full and needs split.
  2134. */
  2135. rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
  2136. if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL) {
  2137. struct ocfs2_extent_tree et;
  2138. ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
  2139. *meta_add += ocfs2_extend_meta_needed(et.et_root_el);
  2140. *credits += ocfs2_calc_extend_credits(sb,
  2141. et.et_root_el,
  2142. ref_blocks);
  2143. } else {
  2144. *credits += OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
  2145. *meta_add += 1;
  2146. }
  2147. out:
  2148. brelse(ref_leaf_bh);
  2149. brelse(prev_bh);
  2150. return ret;
  2151. }
  2152. /*
  2153. * For refcount tree, we will decrease some contiguous clusters
  2154. * refcount count, so just go through it to see how many blocks
  2155. * we gonna touch and whether we need to create new blocks.
  2156. *
  2157. * Normally the refcount blocks store these refcount should be
  2158. * contiguous also, so that we can get the number easily.
  2159. * We will at most add split 2 refcount records and 2 more
  2160. * refcount blocks, so just check it in a rough way.
  2161. *
  2162. * Caller must hold refcount tree lock.
  2163. */
  2164. int ocfs2_prepare_refcount_change_for_del(struct inode *inode,
  2165. u64 refcount_loc,
  2166. u64 phys_blkno,
  2167. u32 clusters,
  2168. int *credits,
  2169. int *ref_blocks)
  2170. {
  2171. int ret;
  2172. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  2173. struct buffer_head *ref_root_bh = NULL;
  2174. struct ocfs2_refcount_tree *tree;
  2175. u64 start_cpos = ocfs2_blocks_to_clusters(inode->i_sb, phys_blkno);
  2176. if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) {
  2177. ocfs2_error(inode->i_sb, "Inode %lu want to use refcount "
  2178. "tree, but the feature bit is not set in the "
  2179. "super block.", inode->i_ino);
  2180. ret = -EROFS;
  2181. goto out;
  2182. }
  2183. BUG_ON(!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
  2184. ret = ocfs2_get_refcount_tree(OCFS2_SB(inode->i_sb),
  2185. refcount_loc, &tree);
  2186. if (ret) {
  2187. mlog_errno(ret);
  2188. goto out;
  2189. }
  2190. ret = ocfs2_read_refcount_block(&tree->rf_ci, refcount_loc,
  2191. &ref_root_bh);
  2192. if (ret) {
  2193. mlog_errno(ret);
  2194. goto out;
  2195. }
  2196. ret = ocfs2_calc_refcount_meta_credits(inode->i_sb,
  2197. &tree->rf_ci,
  2198. ref_root_bh,
  2199. start_cpos, clusters,
  2200. ref_blocks, credits);
  2201. if (ret) {
  2202. mlog_errno(ret);
  2203. goto out;
  2204. }
  2205. mlog(0, "reserve new metadata %d blocks, credits = %d\n",
  2206. *ref_blocks, *credits);
  2207. out:
  2208. brelse(ref_root_bh);
  2209. return ret;
  2210. }
  2211. #define MAX_CONTIG_BYTES 1048576
  2212. static inline unsigned int ocfs2_cow_contig_clusters(struct super_block *sb)
  2213. {
  2214. return ocfs2_clusters_for_bytes(sb, MAX_CONTIG_BYTES);
  2215. }
  2216. static inline unsigned int ocfs2_cow_contig_mask(struct super_block *sb)
  2217. {
  2218. return ~(ocfs2_cow_contig_clusters(sb) - 1);
  2219. }
  2220. /*
  2221. * Given an extent that starts at 'start' and an I/O that starts at 'cpos',
  2222. * find an offset (start + (n * contig_clusters)) that is closest to cpos
  2223. * while still being less than or equal to it.
  2224. *
  2225. * The goal is to break the extent at a multiple of contig_clusters.
  2226. */
  2227. static inline unsigned int ocfs2_cow_align_start(struct super_block *sb,
  2228. unsigned int start,
  2229. unsigned int cpos)
  2230. {
  2231. BUG_ON(start > cpos);
  2232. return start + ((cpos - start) & ocfs2_cow_contig_mask(sb));
  2233. }
  2234. /*
  2235. * Given a cluster count of len, pad it out so that it is a multiple
  2236. * of contig_clusters.
  2237. */
  2238. static inline unsigned int ocfs2_cow_align_length(struct super_block *sb,
  2239. unsigned int len)
  2240. {
  2241. unsigned int padded =
  2242. (len + (ocfs2_cow_contig_clusters(sb) - 1)) &
  2243. ocfs2_cow_contig_mask(sb);
  2244. /* Did we wrap? */
  2245. if (padded < len)
  2246. padded = UINT_MAX;
  2247. return padded;
  2248. }
  2249. /*
  2250. * Calculate out the start and number of virtual clusters we need to to CoW.
  2251. *
  2252. * cpos is vitual start cluster position we want to do CoW in a
  2253. * file and write_len is the cluster length.
  2254. * max_cpos is the place where we want to stop CoW intentionally.
  2255. *
  2256. * Normal we will start CoW from the beginning of extent record cotaining cpos.
  2257. * We try to break up extents on boundaries of MAX_CONTIG_BYTES so that we
  2258. * get good I/O from the resulting extent tree.
  2259. */
  2260. static int ocfs2_refcount_cal_cow_clusters(struct inode *inode,
  2261. struct ocfs2_extent_list *el,
  2262. u32 cpos,
  2263. u32 write_len,
  2264. u32 max_cpos,
  2265. u32 *cow_start,
  2266. u32 *cow_len)
  2267. {
  2268. int ret = 0;
  2269. int tree_height = le16_to_cpu(el->l_tree_depth), i;
  2270. struct buffer_head *eb_bh = NULL;
  2271. struct ocfs2_extent_block *eb = NULL;
  2272. struct ocfs2_extent_rec *rec;
  2273. unsigned int want_clusters, rec_end = 0;
  2274. int contig_clusters = ocfs2_cow_contig_clusters(inode->i_sb);
  2275. int leaf_clusters;
  2276. BUG_ON(cpos + write_len > max_cpos);
  2277. if (tree_height > 0) {
  2278. ret = ocfs2_find_leaf(INODE_CACHE(inode), el, cpos, &eb_bh);
  2279. if (ret) {
  2280. mlog_errno(ret);
  2281. goto out;
  2282. }
  2283. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  2284. el = &eb->h_list;
  2285. if (el->l_tree_depth) {
  2286. ocfs2_error(inode->i_sb,
  2287. "Inode %lu has non zero tree depth in "
  2288. "leaf block %llu\n", inode->i_ino,
  2289. (unsigned long long)eb_bh->b_blocknr);
  2290. ret = -EROFS;
  2291. goto out;
  2292. }
  2293. }
  2294. *cow_len = 0;
  2295. for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
  2296. rec = &el->l_recs[i];
  2297. if (ocfs2_is_empty_extent(rec)) {
  2298. mlog_bug_on_msg(i != 0, "Inode %lu has empty record in "
  2299. "index %d\n", inode->i_ino, i);
  2300. continue;
  2301. }
  2302. if (le32_to_cpu(rec->e_cpos) +
  2303. le16_to_cpu(rec->e_leaf_clusters) <= cpos)
  2304. continue;
  2305. if (*cow_len == 0) {
  2306. /*
  2307. * We should find a refcounted record in the
  2308. * first pass.
  2309. */
  2310. BUG_ON(!(rec->e_flags & OCFS2_EXT_REFCOUNTED));
  2311. *cow_start = le32_to_cpu(rec->e_cpos);
  2312. }
  2313. /*
  2314. * If we encounter a hole, a non-refcounted record or
  2315. * pass the max_cpos, stop the search.
  2316. */
  2317. if ((!(rec->e_flags & OCFS2_EXT_REFCOUNTED)) ||
  2318. (*cow_len && rec_end != le32_to_cpu(rec->e_cpos)) ||
  2319. (max_cpos <= le32_to_cpu(rec->e_cpos)))
  2320. break;
  2321. leaf_clusters = le16_to_cpu(rec->e_leaf_clusters);
  2322. rec_end = le32_to_cpu(rec->e_cpos) + leaf_clusters;
  2323. if (rec_end > max_cpos) {
  2324. rec_end = max_cpos;
  2325. leaf_clusters = rec_end - le32_to_cpu(rec->e_cpos);
  2326. }
  2327. /*
  2328. * How many clusters do we actually need from
  2329. * this extent? First we see how many we actually
  2330. * need to complete the write. If that's smaller
  2331. * than contig_clusters, we try for contig_clusters.
  2332. */
  2333. if (!*cow_len)
  2334. want_clusters = write_len;
  2335. else
  2336. want_clusters = (cpos + write_len) -
  2337. (*cow_start + *cow_len);
  2338. if (want_clusters < contig_clusters)
  2339. want_clusters = contig_clusters;
  2340. /*
  2341. * If the write does not cover the whole extent, we
  2342. * need to calculate how we're going to split the extent.
  2343. * We try to do it on contig_clusters boundaries.
  2344. *
  2345. * Any extent smaller than contig_clusters will be
  2346. * CoWed in its entirety.
  2347. */
  2348. if (leaf_clusters <= contig_clusters)
  2349. *cow_len += leaf_clusters;
  2350. else if (*cow_len || (*cow_start == cpos)) {
  2351. /*
  2352. * This extent needs to be CoW'd from its
  2353. * beginning, so all we have to do is compute
  2354. * how many clusters to grab. We align
  2355. * want_clusters to the edge of contig_clusters
  2356. * to get better I/O.
  2357. */
  2358. want_clusters = ocfs2_cow_align_length(inode->i_sb,
  2359. want_clusters);
  2360. if (leaf_clusters < want_clusters)
  2361. *cow_len += leaf_clusters;
  2362. else
  2363. *cow_len += want_clusters;
  2364. } else if ((*cow_start + contig_clusters) >=
  2365. (cpos + write_len)) {
  2366. /*
  2367. * Breaking off contig_clusters at the front
  2368. * of the extent will cover our write. That's
  2369. * easy.
  2370. */
  2371. *cow_len = contig_clusters;
  2372. } else if ((rec_end - cpos) <= contig_clusters) {
  2373. /*
  2374. * Breaking off contig_clusters at the tail of
  2375. * this extent will cover cpos.
  2376. */
  2377. *cow_start = rec_end - contig_clusters;
  2378. *cow_len = contig_clusters;
  2379. } else if ((rec_end - cpos) <= want_clusters) {
  2380. /*
  2381. * While we can't fit the entire write in this
  2382. * extent, we know that the write goes from cpos
  2383. * to the end of the extent. Break that off.
  2384. * We try to break it at some multiple of
  2385. * contig_clusters from the front of the extent.
  2386. * Failing that (ie, cpos is within
  2387. * contig_clusters of the front), we'll CoW the
  2388. * entire extent.
  2389. */
  2390. *cow_start = ocfs2_cow_align_start(inode->i_sb,
  2391. *cow_start, cpos);
  2392. *cow_len = rec_end - *cow_start;
  2393. } else {
  2394. /*
  2395. * Ok, the entire write lives in the middle of
  2396. * this extent. Let's try to slice the extent up
  2397. * nicely. Optimally, our CoW region starts at
  2398. * m*contig_clusters from the beginning of the
  2399. * extent and goes for n*contig_clusters,
  2400. * covering the entire write.
  2401. */
  2402. *cow_start = ocfs2_cow_align_start(inode->i_sb,
  2403. *cow_start, cpos);
  2404. want_clusters = (cpos + write_len) - *cow_start;
  2405. want_clusters = ocfs2_cow_align_length(inode->i_sb,
  2406. want_clusters);
  2407. if (*cow_start + want_clusters <= rec_end)
  2408. *cow_len = want_clusters;
  2409. else
  2410. *cow_len = rec_end - *cow_start;
  2411. }
  2412. /* Have we covered our entire write yet? */
  2413. if ((*cow_start + *cow_len) >= (cpos + write_len))
  2414. break;
  2415. /*
  2416. * If we reach the end of the extent block and don't get enough
  2417. * clusters, continue with the next extent block if possible.
  2418. */
  2419. if (i + 1 == le16_to_cpu(el->l_next_free_rec) &&
  2420. eb && eb->h_next_leaf_blk) {
  2421. brelse(eb_bh);
  2422. eb_bh = NULL;
  2423. ret = ocfs2_read_extent_block(INODE_CACHE(inode),
  2424. le64_to_cpu(eb->h_next_leaf_blk),
  2425. &eb_bh);
  2426. if (ret) {
  2427. mlog_errno(ret);
  2428. goto out;
  2429. }
  2430. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  2431. el = &eb->h_list;
  2432. i = -1;
  2433. }
  2434. }
  2435. out:
  2436. brelse(eb_bh);
  2437. return ret;
  2438. }
  2439. /*
  2440. * Prepare meta_ac, data_ac and calculate credits when we want to add some
  2441. * num_clusters in data_tree "et" and change the refcount for the old
  2442. * clusters(starting form p_cluster) in the refcount tree.
  2443. *
  2444. * Note:
  2445. * 1. since we may split the old tree, so we at most will need num_clusters + 2
  2446. * more new leaf records.
  2447. * 2. In some case, we may not need to reserve new clusters(e.g, reflink), so
  2448. * just give data_ac = NULL.
  2449. */
  2450. static int ocfs2_lock_refcount_allocators(struct super_block *sb,
  2451. u32 p_cluster, u32 num_clusters,
  2452. struct ocfs2_extent_tree *et,
  2453. struct ocfs2_caching_info *ref_ci,
  2454. struct buffer_head *ref_root_bh,
  2455. struct ocfs2_alloc_context **meta_ac,
  2456. struct ocfs2_alloc_context **data_ac,
  2457. int *credits)
  2458. {
  2459. int ret = 0, meta_add = 0;
  2460. int num_free_extents = ocfs2_num_free_extents(OCFS2_SB(sb), et);
  2461. if (num_free_extents < 0) {
  2462. ret = num_free_extents;
  2463. mlog_errno(ret);
  2464. goto out;
  2465. }
  2466. if (num_free_extents < num_clusters + 2)
  2467. meta_add =
  2468. ocfs2_extend_meta_needed(et->et_root_el);
  2469. *credits += ocfs2_calc_extend_credits(sb, et->et_root_el,
  2470. num_clusters + 2);
  2471. ret = ocfs2_calc_refcount_meta_credits(sb, ref_ci, ref_root_bh,
  2472. p_cluster, num_clusters,
  2473. &meta_add, credits);
  2474. if (ret) {
  2475. mlog_errno(ret);
  2476. goto out;
  2477. }
  2478. mlog(0, "reserve new metadata %d, clusters %u, credits = %d\n",
  2479. meta_add, num_clusters, *credits);
  2480. ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(sb), meta_add,
  2481. meta_ac);
  2482. if (ret) {
  2483. mlog_errno(ret);
  2484. goto out;
  2485. }
  2486. if (data_ac) {
  2487. ret = ocfs2_reserve_clusters(OCFS2_SB(sb), num_clusters,
  2488. data_ac);
  2489. if (ret)
  2490. mlog_errno(ret);
  2491. }
  2492. out:
  2493. if (ret) {
  2494. if (*meta_ac) {
  2495. ocfs2_free_alloc_context(*meta_ac);
  2496. *meta_ac = NULL;
  2497. }
  2498. }
  2499. return ret;
  2500. }
  2501. static int ocfs2_clear_cow_buffer(handle_t *handle, struct buffer_head *bh)
  2502. {
  2503. BUG_ON(buffer_dirty(bh));
  2504. clear_buffer_mapped(bh);
  2505. return 0;
  2506. }
  2507. static int ocfs2_duplicate_clusters_by_page(handle_t *handle,
  2508. struct ocfs2_cow_context *context,
  2509. u32 cpos, u32 old_cluster,
  2510. u32 new_cluster, u32 new_len)
  2511. {
  2512. int ret = 0, partial;
  2513. struct ocfs2_caching_info *ci = context->data_et.et_ci;
  2514. struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
  2515. u64 new_block = ocfs2_clusters_to_blocks(sb, new_cluster);
  2516. struct page *page;
  2517. pgoff_t page_index;
  2518. unsigned int from, to, readahead_pages;
  2519. loff_t offset, end, map_end;
  2520. struct address_space *mapping = context->inode->i_mapping;
  2521. mlog(0, "old_cluster %u, new %u, len %u at offset %u\n", old_cluster,
  2522. new_cluster, new_len, cpos);
  2523. readahead_pages =
  2524. (ocfs2_cow_contig_clusters(sb) <<
  2525. OCFS2_SB(sb)->s_clustersize_bits) >> PAGE_CACHE_SHIFT;
  2526. offset = ((loff_t)cpos) << OCFS2_SB(sb)->s_clustersize_bits;
  2527. end = offset + (new_len << OCFS2_SB(sb)->s_clustersize_bits);
  2528. /*
  2529. * We only duplicate pages until we reach the page contains i_size - 1.
  2530. * So trim 'end' to i_size.
  2531. */
  2532. if (end > i_size_read(context->inode))
  2533. end = i_size_read(context->inode);
  2534. while (offset < end) {
  2535. page_index = offset >> PAGE_CACHE_SHIFT;
  2536. map_end = ((loff_t)page_index + 1) << PAGE_CACHE_SHIFT;
  2537. if (map_end > end)
  2538. map_end = end;
  2539. /* from, to is the offset within the page. */
  2540. from = offset & (PAGE_CACHE_SIZE - 1);
  2541. to = PAGE_CACHE_SIZE;
  2542. if (map_end & (PAGE_CACHE_SIZE - 1))
  2543. to = map_end & (PAGE_CACHE_SIZE - 1);
  2544. page = find_or_create_page(mapping, page_index, GFP_NOFS);
  2545. /*
  2546. * In case PAGE_CACHE_SIZE <= CLUSTER_SIZE, This page
  2547. * can't be dirtied before we CoW it out.
  2548. */
  2549. if (PAGE_CACHE_SIZE <= OCFS2_SB(sb)->s_clustersize)
  2550. BUG_ON(PageDirty(page));
  2551. if (PageReadahead(page) && context->file) {
  2552. page_cache_async_readahead(mapping,
  2553. &context->file->f_ra,
  2554. context->file,
  2555. page, page_index,
  2556. readahead_pages);
  2557. }
  2558. if (!PageUptodate(page)) {
  2559. ret = block_read_full_page(page, ocfs2_get_block);
  2560. if (ret) {
  2561. mlog_errno(ret);
  2562. goto unlock;
  2563. }
  2564. lock_page(page);
  2565. }
  2566. if (page_has_buffers(page)) {
  2567. ret = walk_page_buffers(handle, page_buffers(page),
  2568. from, to, &partial,
  2569. ocfs2_clear_cow_buffer);
  2570. if (ret) {
  2571. mlog_errno(ret);
  2572. goto unlock;
  2573. }
  2574. }
  2575. ocfs2_map_and_dirty_page(context->inode,
  2576. handle, from, to,
  2577. page, 0, &new_block);
  2578. mark_page_accessed(page);
  2579. unlock:
  2580. unlock_page(page);
  2581. page_cache_release(page);
  2582. page = NULL;
  2583. offset = map_end;
  2584. if (ret)
  2585. break;
  2586. }
  2587. return ret;
  2588. }
  2589. static int ocfs2_duplicate_clusters_by_jbd(handle_t *handle,
  2590. struct ocfs2_cow_context *context,
  2591. u32 cpos, u32 old_cluster,
  2592. u32 new_cluster, u32 new_len)
  2593. {
  2594. int ret = 0;
  2595. struct super_block *sb = context->inode->i_sb;
  2596. struct ocfs2_caching_info *ci = context->data_et.et_ci;
  2597. int i, blocks = ocfs2_clusters_to_blocks(sb, new_len);
  2598. u64 old_block = ocfs2_clusters_to_blocks(sb, old_cluster);
  2599. u64 new_block = ocfs2_clusters_to_blocks(sb, new_cluster);
  2600. struct ocfs2_super *osb = OCFS2_SB(sb);
  2601. struct buffer_head *old_bh = NULL;
  2602. struct buffer_head *new_bh = NULL;
  2603. mlog(0, "old_cluster %u, new %u, len %u\n", old_cluster,
  2604. new_cluster, new_len);
  2605. for (i = 0; i < blocks; i++, old_block++, new_block++) {
  2606. new_bh = sb_getblk(osb->sb, new_block);
  2607. if (new_bh == NULL) {
  2608. ret = -EIO;
  2609. mlog_errno(ret);
  2610. break;
  2611. }
  2612. ocfs2_set_new_buffer_uptodate(ci, new_bh);
  2613. ret = ocfs2_read_block(ci, old_block, &old_bh, NULL);
  2614. if (ret) {
  2615. mlog_errno(ret);
  2616. break;
  2617. }
  2618. ret = ocfs2_journal_access(handle, ci, new_bh,
  2619. OCFS2_JOURNAL_ACCESS_CREATE);
  2620. if (ret) {
  2621. mlog_errno(ret);
  2622. break;
  2623. }
  2624. memcpy(new_bh->b_data, old_bh->b_data, sb->s_blocksize);
  2625. ocfs2_journal_dirty(handle, new_bh);
  2626. brelse(new_bh);
  2627. brelse(old_bh);
  2628. new_bh = NULL;
  2629. old_bh = NULL;
  2630. }
  2631. brelse(new_bh);
  2632. brelse(old_bh);
  2633. return ret;
  2634. }
  2635. static int ocfs2_clear_ext_refcount(handle_t *handle,
  2636. struct ocfs2_extent_tree *et,
  2637. u32 cpos, u32 p_cluster, u32 len,
  2638. unsigned int ext_flags,
  2639. struct ocfs2_alloc_context *meta_ac,
  2640. struct ocfs2_cached_dealloc_ctxt *dealloc)
  2641. {
  2642. int ret, index;
  2643. struct ocfs2_extent_rec replace_rec;
  2644. struct ocfs2_path *path = NULL;
  2645. struct ocfs2_extent_list *el;
  2646. struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
  2647. u64 ino = ocfs2_metadata_cache_owner(et->et_ci);
  2648. mlog(0, "inode %llu cpos %u, len %u, p_cluster %u, ext_flags %u\n",
  2649. (unsigned long long)ino, cpos, len, p_cluster, ext_flags);
  2650. memset(&replace_rec, 0, sizeof(replace_rec));
  2651. replace_rec.e_cpos = cpu_to_le32(cpos);
  2652. replace_rec.e_leaf_clusters = cpu_to_le16(len);
  2653. replace_rec.e_blkno = cpu_to_le64(ocfs2_clusters_to_blocks(sb,
  2654. p_cluster));
  2655. replace_rec.e_flags = ext_flags;
  2656. replace_rec.e_flags &= ~OCFS2_EXT_REFCOUNTED;
  2657. path = ocfs2_new_path_from_et(et);
  2658. if (!path) {
  2659. ret = -ENOMEM;
  2660. mlog_errno(ret);
  2661. goto out;
  2662. }
  2663. ret = ocfs2_find_path(et->et_ci, path, cpos);
  2664. if (ret) {
  2665. mlog_errno(ret);
  2666. goto out;
  2667. }
  2668. el = path_leaf_el(path);
  2669. index = ocfs2_search_extent_list(el, cpos);
  2670. if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
  2671. ocfs2_error(sb,
  2672. "Inode %llu has an extent at cpos %u which can no "
  2673. "longer be found.\n",
  2674. (unsigned long long)ino, cpos);
  2675. ret = -EROFS;
  2676. goto out;
  2677. }
  2678. ret = ocfs2_split_extent(handle, et, path, index,
  2679. &replace_rec, meta_ac, dealloc);
  2680. if (ret)
  2681. mlog_errno(ret);
  2682. out:
  2683. ocfs2_free_path(path);
  2684. return ret;
  2685. }
  2686. static int ocfs2_replace_clusters(handle_t *handle,
  2687. struct ocfs2_cow_context *context,
  2688. u32 cpos, u32 old,
  2689. u32 new, u32 len,
  2690. unsigned int ext_flags)
  2691. {
  2692. int ret;
  2693. struct ocfs2_caching_info *ci = context->data_et.et_ci;
  2694. u64 ino = ocfs2_metadata_cache_owner(ci);
  2695. mlog(0, "inode %llu, cpos %u, old %u, new %u, len %u, ext_flags %u\n",
  2696. (unsigned long long)ino, cpos, old, new, len, ext_flags);
  2697. /*If the old clusters is unwritten, no need to duplicate. */
  2698. if (!(ext_flags & OCFS2_EXT_UNWRITTEN)) {
  2699. ret = context->cow_duplicate_clusters(handle, context, cpos,
  2700. old, new, len);
  2701. if (ret) {
  2702. mlog_errno(ret);
  2703. goto out;
  2704. }
  2705. }
  2706. ret = ocfs2_clear_ext_refcount(handle, &context->data_et,
  2707. cpos, new, len, ext_flags,
  2708. context->meta_ac, &context->dealloc);
  2709. if (ret)
  2710. mlog_errno(ret);
  2711. out:
  2712. return ret;
  2713. }
  2714. static int ocfs2_cow_sync_writeback(struct super_block *sb,
  2715. struct ocfs2_cow_context *context,
  2716. u32 cpos, u32 num_clusters)
  2717. {
  2718. int ret = 0;
  2719. loff_t offset, end, map_end;
  2720. pgoff_t page_index;
  2721. struct page *page;
  2722. if (ocfs2_should_order_data(context->inode))
  2723. return 0;
  2724. offset = ((loff_t)cpos) << OCFS2_SB(sb)->s_clustersize_bits;
  2725. end = offset + (num_clusters << OCFS2_SB(sb)->s_clustersize_bits);
  2726. ret = filemap_fdatawrite_range(context->inode->i_mapping,
  2727. offset, end - 1);
  2728. if (ret < 0) {
  2729. mlog_errno(ret);
  2730. return ret;
  2731. }
  2732. while (offset < end) {
  2733. page_index = offset >> PAGE_CACHE_SHIFT;
  2734. map_end = ((loff_t)page_index + 1) << PAGE_CACHE_SHIFT;
  2735. if (map_end > end)
  2736. map_end = end;
  2737. page = find_or_create_page(context->inode->i_mapping,
  2738. page_index, GFP_NOFS);
  2739. BUG_ON(!page);
  2740. wait_on_page_writeback(page);
  2741. if (PageError(page)) {
  2742. ret = -EIO;
  2743. mlog_errno(ret);
  2744. } else
  2745. mark_page_accessed(page);
  2746. unlock_page(page);
  2747. page_cache_release(page);
  2748. page = NULL;
  2749. offset = map_end;
  2750. if (ret)
  2751. break;
  2752. }
  2753. return ret;
  2754. }
  2755. static int ocfs2_di_get_clusters(struct ocfs2_cow_context *context,
  2756. u32 v_cluster, u32 *p_cluster,
  2757. u32 *num_clusters,
  2758. unsigned int *extent_flags)
  2759. {
  2760. return ocfs2_get_clusters(context->inode, v_cluster, p_cluster,
  2761. num_clusters, extent_flags);
  2762. }
  2763. static int ocfs2_make_clusters_writable(struct super_block *sb,
  2764. struct ocfs2_cow_context *context,
  2765. u32 cpos, u32 p_cluster,
  2766. u32 num_clusters, unsigned int e_flags)
  2767. {
  2768. int ret, delete, index, credits = 0;
  2769. u32 new_bit, new_len, orig_num_clusters;
  2770. unsigned int set_len;
  2771. struct ocfs2_super *osb = OCFS2_SB(sb);
  2772. handle_t *handle;
  2773. struct buffer_head *ref_leaf_bh = NULL;
  2774. struct ocfs2_caching_info *ref_ci = &context->ref_tree->rf_ci;
  2775. struct ocfs2_refcount_rec rec;
  2776. mlog(0, "cpos %u, p_cluster %u, num_clusters %u, e_flags %u\n",
  2777. cpos, p_cluster, num_clusters, e_flags);
  2778. ret = ocfs2_lock_refcount_allocators(sb, p_cluster, num_clusters,
  2779. &context->data_et,
  2780. ref_ci,
  2781. context->ref_root_bh,
  2782. &context->meta_ac,
  2783. &context->data_ac, &credits);
  2784. if (ret) {
  2785. mlog_errno(ret);
  2786. return ret;
  2787. }
  2788. if (context->post_refcount)
  2789. credits += context->post_refcount->credits;
  2790. credits += context->extra_credits;
  2791. handle = ocfs2_start_trans(osb, credits);
  2792. if (IS_ERR(handle)) {
  2793. ret = PTR_ERR(handle);
  2794. mlog_errno(ret);
  2795. goto out;
  2796. }
  2797. orig_num_clusters = num_clusters;
  2798. while (num_clusters) {
  2799. ret = ocfs2_get_refcount_rec(ref_ci, context->ref_root_bh,
  2800. p_cluster, num_clusters,
  2801. &rec, &index, &ref_leaf_bh);
  2802. if (ret) {
  2803. mlog_errno(ret);
  2804. goto out_commit;
  2805. }
  2806. BUG_ON(!rec.r_refcount);
  2807. set_len = min((u64)p_cluster + num_clusters,
  2808. le64_to_cpu(rec.r_cpos) +
  2809. le32_to_cpu(rec.r_clusters)) - p_cluster;
  2810. /*
  2811. * There are many different situation here.
  2812. * 1. If refcount == 1, remove the flag and don't COW.
  2813. * 2. If refcount > 1, allocate clusters.
  2814. * Here we may not allocate r_len once at a time, so continue
  2815. * until we reach num_clusters.
  2816. */
  2817. if (le32_to_cpu(rec.r_refcount) == 1) {
  2818. delete = 0;
  2819. ret = ocfs2_clear_ext_refcount(handle,
  2820. &context->data_et,
  2821. cpos, p_cluster,
  2822. set_len, e_flags,
  2823. context->meta_ac,
  2824. &context->dealloc);
  2825. if (ret) {
  2826. mlog_errno(ret);
  2827. goto out_commit;
  2828. }
  2829. } else {
  2830. delete = 1;
  2831. ret = __ocfs2_claim_clusters(handle,
  2832. context->data_ac,
  2833. 1, set_len,
  2834. &new_bit, &new_len);
  2835. if (ret) {
  2836. mlog_errno(ret);
  2837. goto out_commit;
  2838. }
  2839. ret = ocfs2_replace_clusters(handle, context,
  2840. cpos, p_cluster, new_bit,
  2841. new_len, e_flags);
  2842. if (ret) {
  2843. mlog_errno(ret);
  2844. goto out_commit;
  2845. }
  2846. set_len = new_len;
  2847. }
  2848. ret = __ocfs2_decrease_refcount(handle, ref_ci,
  2849. context->ref_root_bh,
  2850. p_cluster, set_len,
  2851. context->meta_ac,
  2852. &context->dealloc, delete);
  2853. if (ret) {
  2854. mlog_errno(ret);
  2855. goto out_commit;
  2856. }
  2857. cpos += set_len;
  2858. p_cluster += set_len;
  2859. num_clusters -= set_len;
  2860. brelse(ref_leaf_bh);
  2861. ref_leaf_bh = NULL;
  2862. }
  2863. /* handle any post_cow action. */
  2864. if (context->post_refcount && context->post_refcount->func) {
  2865. ret = context->post_refcount->func(context->inode, handle,
  2866. context->post_refcount->para);
  2867. if (ret) {
  2868. mlog_errno(ret);
  2869. goto out_commit;
  2870. }
  2871. }
  2872. /*
  2873. * Here we should write the new page out first if we are
  2874. * in write-back mode.
  2875. */
  2876. if (context->get_clusters == ocfs2_di_get_clusters) {
  2877. ret = ocfs2_cow_sync_writeback(sb, context, cpos,
  2878. orig_num_clusters);
  2879. if (ret)
  2880. mlog_errno(ret);
  2881. }
  2882. out_commit:
  2883. ocfs2_commit_trans(osb, handle);
  2884. out:
  2885. if (context->data_ac) {
  2886. ocfs2_free_alloc_context(context->data_ac);
  2887. context->data_ac = NULL;
  2888. }
  2889. if (context->meta_ac) {
  2890. ocfs2_free_alloc_context(context->meta_ac);
  2891. context->meta_ac = NULL;
  2892. }
  2893. brelse(ref_leaf_bh);
  2894. return ret;
  2895. }
  2896. static int ocfs2_replace_cow(struct ocfs2_cow_context *context)
  2897. {
  2898. int ret = 0;
  2899. struct inode *inode = context->inode;
  2900. u32 cow_start = context->cow_start, cow_len = context->cow_len;
  2901. u32 p_cluster, num_clusters;
  2902. unsigned int ext_flags;
  2903. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2904. if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) {
  2905. ocfs2_error(inode->i_sb, "Inode %lu want to use refcount "
  2906. "tree, but the feature bit is not set in the "
  2907. "super block.", inode->i_ino);
  2908. return -EROFS;
  2909. }
  2910. ocfs2_init_dealloc_ctxt(&context->dealloc);
  2911. while (cow_len) {
  2912. ret = context->get_clusters(context, cow_start, &p_cluster,
  2913. &num_clusters, &ext_flags);
  2914. if (ret) {
  2915. mlog_errno(ret);
  2916. break;
  2917. }
  2918. BUG_ON(!(ext_flags & OCFS2_EXT_REFCOUNTED));
  2919. if (cow_len < num_clusters)
  2920. num_clusters = cow_len;
  2921. ret = ocfs2_make_clusters_writable(inode->i_sb, context,
  2922. cow_start, p_cluster,
  2923. num_clusters, ext_flags);
  2924. if (ret) {
  2925. mlog_errno(ret);
  2926. break;
  2927. }
  2928. cow_len -= num_clusters;
  2929. cow_start += num_clusters;
  2930. }
  2931. if (ocfs2_dealloc_has_cluster(&context->dealloc)) {
  2932. ocfs2_schedule_truncate_log_flush(osb, 1);
  2933. ocfs2_run_deallocs(osb, &context->dealloc);
  2934. }
  2935. return ret;
  2936. }
  2937. static void ocfs2_readahead_for_cow(struct inode *inode,
  2938. struct file *file,
  2939. u32 start, u32 len)
  2940. {
  2941. struct address_space *mapping;
  2942. pgoff_t index;
  2943. unsigned long num_pages;
  2944. int cs_bits = OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  2945. if (!file)
  2946. return;
  2947. mapping = file->f_mapping;
  2948. num_pages = (len << cs_bits) >> PAGE_CACHE_SHIFT;
  2949. if (!num_pages)
  2950. num_pages = 1;
  2951. index = ((loff_t)start << cs_bits) >> PAGE_CACHE_SHIFT;
  2952. page_cache_sync_readahead(mapping, &file->f_ra, file,
  2953. index, num_pages);
  2954. }
  2955. /*
  2956. * Starting at cpos, try to CoW write_len clusters. Don't CoW
  2957. * past max_cpos. This will stop when it runs into a hole or an
  2958. * unrefcounted extent.
  2959. */
  2960. static int ocfs2_refcount_cow_hunk(struct inode *inode,
  2961. struct file *file,
  2962. struct buffer_head *di_bh,
  2963. u32 cpos, u32 write_len, u32 max_cpos)
  2964. {
  2965. int ret;
  2966. u32 cow_start = 0, cow_len = 0;
  2967. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  2968. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2969. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  2970. struct buffer_head *ref_root_bh = NULL;
  2971. struct ocfs2_refcount_tree *ref_tree;
  2972. struct ocfs2_cow_context *context = NULL;
  2973. BUG_ON(!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
  2974. ret = ocfs2_refcount_cal_cow_clusters(inode, &di->id2.i_list,
  2975. cpos, write_len, max_cpos,
  2976. &cow_start, &cow_len);
  2977. if (ret) {
  2978. mlog_errno(ret);
  2979. goto out;
  2980. }
  2981. mlog(0, "CoW inode %lu, cpos %u, write_len %u, cow_start %u, "
  2982. "cow_len %u\n", inode->i_ino,
  2983. cpos, write_len, cow_start, cow_len);
  2984. BUG_ON(cow_len == 0);
  2985. ocfs2_readahead_for_cow(inode, file, cow_start, cow_len);
  2986. context = kzalloc(sizeof(struct ocfs2_cow_context), GFP_NOFS);
  2987. if (!context) {
  2988. ret = -ENOMEM;
  2989. mlog_errno(ret);
  2990. goto out;
  2991. }
  2992. ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
  2993. 1, &ref_tree, &ref_root_bh);
  2994. if (ret) {
  2995. mlog_errno(ret);
  2996. goto out;
  2997. }
  2998. context->inode = inode;
  2999. context->cow_start = cow_start;
  3000. context->cow_len = cow_len;
  3001. context->ref_tree = ref_tree;
  3002. context->ref_root_bh = ref_root_bh;
  3003. context->cow_duplicate_clusters = ocfs2_duplicate_clusters_by_page;
  3004. context->get_clusters = ocfs2_di_get_clusters;
  3005. context->file = file;
  3006. ocfs2_init_dinode_extent_tree(&context->data_et,
  3007. INODE_CACHE(inode), di_bh);
  3008. ret = ocfs2_replace_cow(context);
  3009. if (ret)
  3010. mlog_errno(ret);
  3011. /*
  3012. * truncate the extent map here since no matter whether we meet with
  3013. * any error during the action, we shouldn't trust cached extent map
  3014. * any more.
  3015. */
  3016. ocfs2_extent_map_trunc(inode, cow_start);
  3017. ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
  3018. brelse(ref_root_bh);
  3019. out:
  3020. kfree(context);
  3021. return ret;
  3022. }
  3023. /*
  3024. * CoW any and all clusters between cpos and cpos+write_len.
  3025. * Don't CoW past max_cpos. If this returns successfully, all
  3026. * clusters between cpos and cpos+write_len are safe to modify.
  3027. */
  3028. int ocfs2_refcount_cow(struct inode *inode,
  3029. struct file *file,
  3030. struct buffer_head *di_bh,
  3031. u32 cpos, u32 write_len, u32 max_cpos)
  3032. {
  3033. int ret = 0;
  3034. u32 p_cluster, num_clusters;
  3035. unsigned int ext_flags;
  3036. while (write_len) {
  3037. ret = ocfs2_get_clusters(inode, cpos, &p_cluster,
  3038. &num_clusters, &ext_flags);
  3039. if (ret) {
  3040. mlog_errno(ret);
  3041. break;
  3042. }
  3043. if (write_len < num_clusters)
  3044. num_clusters = write_len;
  3045. if (ext_flags & OCFS2_EXT_REFCOUNTED) {
  3046. ret = ocfs2_refcount_cow_hunk(inode, file, di_bh, cpos,
  3047. num_clusters, max_cpos);
  3048. if (ret) {
  3049. mlog_errno(ret);
  3050. break;
  3051. }
  3052. }
  3053. write_len -= num_clusters;
  3054. cpos += num_clusters;
  3055. }
  3056. return ret;
  3057. }
  3058. static int ocfs2_xattr_value_get_clusters(struct ocfs2_cow_context *context,
  3059. u32 v_cluster, u32 *p_cluster,
  3060. u32 *num_clusters,
  3061. unsigned int *extent_flags)
  3062. {
  3063. struct inode *inode = context->inode;
  3064. struct ocfs2_xattr_value_root *xv = context->cow_object;
  3065. return ocfs2_xattr_get_clusters(inode, v_cluster, p_cluster,
  3066. num_clusters, &xv->xr_list,
  3067. extent_flags);
  3068. }
  3069. /*
  3070. * Given a xattr value root, calculate the most meta/credits we need for
  3071. * refcount tree change if we truncate it to 0.
  3072. */
  3073. int ocfs2_refcounted_xattr_delete_need(struct inode *inode,
  3074. struct ocfs2_caching_info *ref_ci,
  3075. struct buffer_head *ref_root_bh,
  3076. struct ocfs2_xattr_value_root *xv,
  3077. int *meta_add, int *credits)
  3078. {
  3079. int ret = 0, index, ref_blocks = 0;
  3080. u32 p_cluster, num_clusters;
  3081. u32 cpos = 0, clusters = le32_to_cpu(xv->xr_clusters);
  3082. struct ocfs2_refcount_block *rb;
  3083. struct ocfs2_refcount_rec rec;
  3084. struct buffer_head *ref_leaf_bh = NULL;
  3085. while (cpos < clusters) {
  3086. ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
  3087. &num_clusters, &xv->xr_list,
  3088. NULL);
  3089. if (ret) {
  3090. mlog_errno(ret);
  3091. goto out;
  3092. }
  3093. cpos += num_clusters;
  3094. while (num_clusters) {
  3095. ret = ocfs2_get_refcount_rec(ref_ci, ref_root_bh,
  3096. p_cluster, num_clusters,
  3097. &rec, &index,
  3098. &ref_leaf_bh);
  3099. if (ret) {
  3100. mlog_errno(ret);
  3101. goto out;
  3102. }
  3103. BUG_ON(!rec.r_refcount);
  3104. rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
  3105. /*
  3106. * We really don't know whether the other clusters is in
  3107. * this refcount block or not, so just take the worst
  3108. * case that all the clusters are in this block and each
  3109. * one will split a refcount rec, so totally we need
  3110. * clusters * 2 new refcount rec.
  3111. */
  3112. if (le64_to_cpu(rb->rf_records.rl_used) + clusters * 2 >
  3113. le16_to_cpu(rb->rf_records.rl_count))
  3114. ref_blocks++;
  3115. *credits += 1;
  3116. brelse(ref_leaf_bh);
  3117. ref_leaf_bh = NULL;
  3118. if (num_clusters <= le32_to_cpu(rec.r_clusters))
  3119. break;
  3120. else
  3121. num_clusters -= le32_to_cpu(rec.r_clusters);
  3122. p_cluster += num_clusters;
  3123. }
  3124. }
  3125. *meta_add += ref_blocks;
  3126. if (!ref_blocks)
  3127. goto out;
  3128. rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
  3129. if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
  3130. *credits += OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
  3131. else {
  3132. struct ocfs2_extent_tree et;
  3133. ocfs2_init_refcount_extent_tree(&et, ref_ci, ref_root_bh);
  3134. *credits += ocfs2_calc_extend_credits(inode->i_sb,
  3135. et.et_root_el,
  3136. ref_blocks);
  3137. }
  3138. out:
  3139. brelse(ref_leaf_bh);
  3140. return ret;
  3141. }
  3142. /*
  3143. * Do CoW for xattr.
  3144. */
  3145. int ocfs2_refcount_cow_xattr(struct inode *inode,
  3146. struct ocfs2_dinode *di,
  3147. struct ocfs2_xattr_value_buf *vb,
  3148. struct ocfs2_refcount_tree *ref_tree,
  3149. struct buffer_head *ref_root_bh,
  3150. u32 cpos, u32 write_len,
  3151. struct ocfs2_post_refcount *post)
  3152. {
  3153. int ret;
  3154. struct ocfs2_xattr_value_root *xv = vb->vb_xv;
  3155. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  3156. struct ocfs2_cow_context *context = NULL;
  3157. u32 cow_start, cow_len;
  3158. BUG_ON(!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
  3159. ret = ocfs2_refcount_cal_cow_clusters(inode, &xv->xr_list,
  3160. cpos, write_len, UINT_MAX,
  3161. &cow_start, &cow_len);
  3162. if (ret) {
  3163. mlog_errno(ret);
  3164. goto out;
  3165. }
  3166. BUG_ON(cow_len == 0);
  3167. context = kzalloc(sizeof(struct ocfs2_cow_context), GFP_NOFS);
  3168. if (!context) {
  3169. ret = -ENOMEM;
  3170. mlog_errno(ret);
  3171. goto out;
  3172. }
  3173. context->inode = inode;
  3174. context->cow_start = cow_start;
  3175. context->cow_len = cow_len;
  3176. context->ref_tree = ref_tree;
  3177. context->ref_root_bh = ref_root_bh;;
  3178. context->cow_object = xv;
  3179. context->cow_duplicate_clusters = ocfs2_duplicate_clusters_by_jbd;
  3180. /* We need the extra credits for duplicate_clusters by jbd. */
  3181. context->extra_credits =
  3182. ocfs2_clusters_to_blocks(inode->i_sb, 1) * cow_len;
  3183. context->get_clusters = ocfs2_xattr_value_get_clusters;
  3184. context->post_refcount = post;
  3185. ocfs2_init_xattr_value_extent_tree(&context->data_et,
  3186. INODE_CACHE(inode), vb);
  3187. ret = ocfs2_replace_cow(context);
  3188. if (ret)
  3189. mlog_errno(ret);
  3190. out:
  3191. kfree(context);
  3192. return ret;
  3193. }
  3194. /*
  3195. * Insert a new extent into refcount tree and mark a extent rec
  3196. * as refcounted in the dinode tree.
  3197. */
  3198. int ocfs2_add_refcount_flag(struct inode *inode,
  3199. struct ocfs2_extent_tree *data_et,
  3200. struct ocfs2_caching_info *ref_ci,
  3201. struct buffer_head *ref_root_bh,
  3202. u32 cpos, u32 p_cluster, u32 num_clusters,
  3203. struct ocfs2_cached_dealloc_ctxt *dealloc,
  3204. struct ocfs2_post_refcount *post)
  3205. {
  3206. int ret;
  3207. handle_t *handle;
  3208. int credits = 1, ref_blocks = 0;
  3209. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3210. struct ocfs2_alloc_context *meta_ac = NULL;
  3211. ret = ocfs2_calc_refcount_meta_credits(inode->i_sb,
  3212. ref_ci, ref_root_bh,
  3213. p_cluster, num_clusters,
  3214. &ref_blocks, &credits);
  3215. if (ret) {
  3216. mlog_errno(ret);
  3217. goto out;
  3218. }
  3219. mlog(0, "reserve new metadata %d, credits = %d\n",
  3220. ref_blocks, credits);
  3221. if (ref_blocks) {
  3222. ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
  3223. ref_blocks, &meta_ac);
  3224. if (ret) {
  3225. mlog_errno(ret);
  3226. goto out;
  3227. }
  3228. }
  3229. if (post)
  3230. credits += post->credits;
  3231. handle = ocfs2_start_trans(osb, credits);
  3232. if (IS_ERR(handle)) {
  3233. ret = PTR_ERR(handle);
  3234. mlog_errno(ret);
  3235. goto out;
  3236. }
  3237. ret = ocfs2_mark_extent_refcounted(inode, data_et, handle,
  3238. cpos, num_clusters, p_cluster,
  3239. meta_ac, dealloc);
  3240. if (ret) {
  3241. mlog_errno(ret);
  3242. goto out_commit;
  3243. }
  3244. ret = __ocfs2_increase_refcount(handle, ref_ci, ref_root_bh,
  3245. p_cluster, num_clusters, 0,
  3246. meta_ac, dealloc);
  3247. if (ret) {
  3248. mlog_errno(ret);
  3249. goto out_commit;
  3250. }
  3251. if (post && post->func) {
  3252. ret = post->func(inode, handle, post->para);
  3253. if (ret)
  3254. mlog_errno(ret);
  3255. }
  3256. out_commit:
  3257. ocfs2_commit_trans(osb, handle);
  3258. out:
  3259. if (meta_ac)
  3260. ocfs2_free_alloc_context(meta_ac);
  3261. return ret;
  3262. }
  3263. static int ocfs2_change_ctime(struct inode *inode,
  3264. struct buffer_head *di_bh)
  3265. {
  3266. int ret;
  3267. handle_t *handle;
  3268. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  3269. handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb),
  3270. OCFS2_INODE_UPDATE_CREDITS);
  3271. if (IS_ERR(handle)) {
  3272. ret = PTR_ERR(handle);
  3273. mlog_errno(ret);
  3274. goto out;
  3275. }
  3276. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  3277. OCFS2_JOURNAL_ACCESS_WRITE);
  3278. if (ret) {
  3279. mlog_errno(ret);
  3280. goto out_commit;
  3281. }
  3282. inode->i_ctime = CURRENT_TIME;
  3283. di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  3284. di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  3285. ocfs2_journal_dirty(handle, di_bh);
  3286. out_commit:
  3287. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  3288. out:
  3289. return ret;
  3290. }
  3291. static int ocfs2_attach_refcount_tree(struct inode *inode,
  3292. struct buffer_head *di_bh)
  3293. {
  3294. int ret, data_changed = 0;
  3295. struct buffer_head *ref_root_bh = NULL;
  3296. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  3297. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  3298. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3299. struct ocfs2_refcount_tree *ref_tree;
  3300. unsigned int ext_flags;
  3301. loff_t size;
  3302. u32 cpos, num_clusters, clusters, p_cluster;
  3303. struct ocfs2_cached_dealloc_ctxt dealloc;
  3304. struct ocfs2_extent_tree di_et;
  3305. ocfs2_init_dealloc_ctxt(&dealloc);
  3306. if (!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL)) {
  3307. ret = ocfs2_create_refcount_tree(inode, di_bh);
  3308. if (ret) {
  3309. mlog_errno(ret);
  3310. goto out;
  3311. }
  3312. }
  3313. BUG_ON(!di->i_refcount_loc);
  3314. ret = ocfs2_lock_refcount_tree(osb,
  3315. le64_to_cpu(di->i_refcount_loc), 1,
  3316. &ref_tree, &ref_root_bh);
  3317. if (ret) {
  3318. mlog_errno(ret);
  3319. goto out;
  3320. }
  3321. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  3322. goto attach_xattr;
  3323. ocfs2_init_dinode_extent_tree(&di_et, INODE_CACHE(inode), di_bh);
  3324. size = i_size_read(inode);
  3325. clusters = ocfs2_clusters_for_bytes(inode->i_sb, size);
  3326. cpos = 0;
  3327. while (cpos < clusters) {
  3328. ret = ocfs2_get_clusters(inode, cpos, &p_cluster,
  3329. &num_clusters, &ext_flags);
  3330. if (p_cluster && !(ext_flags & OCFS2_EXT_REFCOUNTED)) {
  3331. ret = ocfs2_add_refcount_flag(inode, &di_et,
  3332. &ref_tree->rf_ci,
  3333. ref_root_bh, cpos,
  3334. p_cluster, num_clusters,
  3335. &dealloc, NULL);
  3336. if (ret) {
  3337. mlog_errno(ret);
  3338. goto unlock;
  3339. }
  3340. data_changed = 1;
  3341. }
  3342. cpos += num_clusters;
  3343. }
  3344. attach_xattr:
  3345. if (oi->ip_dyn_features & OCFS2_HAS_XATTR_FL) {
  3346. ret = ocfs2_xattr_attach_refcount_tree(inode, di_bh,
  3347. &ref_tree->rf_ci,
  3348. ref_root_bh,
  3349. &dealloc);
  3350. if (ret) {
  3351. mlog_errno(ret);
  3352. goto unlock;
  3353. }
  3354. }
  3355. if (data_changed) {
  3356. ret = ocfs2_change_ctime(inode, di_bh);
  3357. if (ret)
  3358. mlog_errno(ret);
  3359. }
  3360. unlock:
  3361. ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
  3362. brelse(ref_root_bh);
  3363. if (!ret && ocfs2_dealloc_has_cluster(&dealloc)) {
  3364. ocfs2_schedule_truncate_log_flush(osb, 1);
  3365. ocfs2_run_deallocs(osb, &dealloc);
  3366. }
  3367. out:
  3368. /*
  3369. * Empty the extent map so that we may get the right extent
  3370. * record from the disk.
  3371. */
  3372. ocfs2_extent_map_trunc(inode, 0);
  3373. return ret;
  3374. }
  3375. static int ocfs2_add_refcounted_extent(struct inode *inode,
  3376. struct ocfs2_extent_tree *et,
  3377. struct ocfs2_caching_info *ref_ci,
  3378. struct buffer_head *ref_root_bh,
  3379. u32 cpos, u32 p_cluster, u32 num_clusters,
  3380. unsigned int ext_flags,
  3381. struct ocfs2_cached_dealloc_ctxt *dealloc)
  3382. {
  3383. int ret;
  3384. handle_t *handle;
  3385. int credits = 0;
  3386. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3387. struct ocfs2_alloc_context *meta_ac = NULL;
  3388. ret = ocfs2_lock_refcount_allocators(inode->i_sb,
  3389. p_cluster, num_clusters,
  3390. et, ref_ci,
  3391. ref_root_bh, &meta_ac,
  3392. NULL, &credits);
  3393. if (ret) {
  3394. mlog_errno(ret);
  3395. goto out;
  3396. }
  3397. handle = ocfs2_start_trans(osb, credits);
  3398. if (IS_ERR(handle)) {
  3399. ret = PTR_ERR(handle);
  3400. mlog_errno(ret);
  3401. goto out;
  3402. }
  3403. ret = ocfs2_insert_extent(handle, et, cpos,
  3404. ocfs2_clusters_to_blocks(inode->i_sb, p_cluster),
  3405. num_clusters, ext_flags, meta_ac);
  3406. if (ret) {
  3407. mlog_errno(ret);
  3408. goto out_commit;
  3409. }
  3410. ret = ocfs2_increase_refcount(handle, ref_ci, ref_root_bh,
  3411. p_cluster, num_clusters,
  3412. meta_ac, dealloc);
  3413. if (ret)
  3414. mlog_errno(ret);
  3415. out_commit:
  3416. ocfs2_commit_trans(osb, handle);
  3417. out:
  3418. if (meta_ac)
  3419. ocfs2_free_alloc_context(meta_ac);
  3420. return ret;
  3421. }
  3422. static int ocfs2_duplicate_inline_data(struct inode *s_inode,
  3423. struct buffer_head *s_bh,
  3424. struct inode *t_inode,
  3425. struct buffer_head *t_bh)
  3426. {
  3427. int ret;
  3428. handle_t *handle;
  3429. struct ocfs2_super *osb = OCFS2_SB(s_inode->i_sb);
  3430. struct ocfs2_dinode *s_di = (struct ocfs2_dinode *)s_bh->b_data;
  3431. struct ocfs2_dinode *t_di = (struct ocfs2_dinode *)t_bh->b_data;
  3432. BUG_ON(!(OCFS2_I(s_inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL));
  3433. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  3434. if (IS_ERR(handle)) {
  3435. ret = PTR_ERR(handle);
  3436. mlog_errno(ret);
  3437. goto out;
  3438. }
  3439. ret = ocfs2_journal_access_di(handle, INODE_CACHE(t_inode), t_bh,
  3440. OCFS2_JOURNAL_ACCESS_WRITE);
  3441. if (ret) {
  3442. mlog_errno(ret);
  3443. goto out_commit;
  3444. }
  3445. t_di->id2.i_data.id_count = s_di->id2.i_data.id_count;
  3446. memcpy(t_di->id2.i_data.id_data, s_di->id2.i_data.id_data,
  3447. le16_to_cpu(s_di->id2.i_data.id_count));
  3448. spin_lock(&OCFS2_I(t_inode)->ip_lock);
  3449. OCFS2_I(t_inode)->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
  3450. t_di->i_dyn_features = cpu_to_le16(OCFS2_I(t_inode)->ip_dyn_features);
  3451. spin_unlock(&OCFS2_I(t_inode)->ip_lock);
  3452. ocfs2_journal_dirty(handle, t_bh);
  3453. out_commit:
  3454. ocfs2_commit_trans(osb, handle);
  3455. out:
  3456. return ret;
  3457. }
  3458. static int ocfs2_duplicate_extent_list(struct inode *s_inode,
  3459. struct inode *t_inode,
  3460. struct buffer_head *t_bh,
  3461. struct ocfs2_caching_info *ref_ci,
  3462. struct buffer_head *ref_root_bh,
  3463. struct ocfs2_cached_dealloc_ctxt *dealloc)
  3464. {
  3465. int ret = 0;
  3466. u32 p_cluster, num_clusters, clusters, cpos;
  3467. loff_t size;
  3468. unsigned int ext_flags;
  3469. struct ocfs2_extent_tree et;
  3470. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(t_inode), t_bh);
  3471. size = i_size_read(s_inode);
  3472. clusters = ocfs2_clusters_for_bytes(s_inode->i_sb, size);
  3473. cpos = 0;
  3474. while (cpos < clusters) {
  3475. ret = ocfs2_get_clusters(s_inode, cpos, &p_cluster,
  3476. &num_clusters, &ext_flags);
  3477. if (p_cluster) {
  3478. ret = ocfs2_add_refcounted_extent(t_inode, &et,
  3479. ref_ci, ref_root_bh,
  3480. cpos, p_cluster,
  3481. num_clusters,
  3482. ext_flags,
  3483. dealloc);
  3484. if (ret) {
  3485. mlog_errno(ret);
  3486. goto out;
  3487. }
  3488. }
  3489. cpos += num_clusters;
  3490. }
  3491. out:
  3492. return ret;
  3493. }
  3494. /*
  3495. * change the new file's attributes to the src.
  3496. *
  3497. * reflink creates a snapshot of a file, that means the attributes
  3498. * must be identical except for three exceptions - nlink, ino, and ctime.
  3499. */
  3500. static int ocfs2_complete_reflink(struct inode *s_inode,
  3501. struct buffer_head *s_bh,
  3502. struct inode *t_inode,
  3503. struct buffer_head *t_bh,
  3504. bool preserve)
  3505. {
  3506. int ret;
  3507. handle_t *handle;
  3508. struct ocfs2_dinode *s_di = (struct ocfs2_dinode *)s_bh->b_data;
  3509. struct ocfs2_dinode *di = (struct ocfs2_dinode *)t_bh->b_data;
  3510. loff_t size = i_size_read(s_inode);
  3511. handle = ocfs2_start_trans(OCFS2_SB(t_inode->i_sb),
  3512. OCFS2_INODE_UPDATE_CREDITS);
  3513. if (IS_ERR(handle)) {
  3514. ret = PTR_ERR(handle);
  3515. mlog_errno(ret);
  3516. return ret;
  3517. }
  3518. ret = ocfs2_journal_access_di(handle, INODE_CACHE(t_inode), t_bh,
  3519. OCFS2_JOURNAL_ACCESS_WRITE);
  3520. if (ret) {
  3521. mlog_errno(ret);
  3522. goto out_commit;
  3523. }
  3524. spin_lock(&OCFS2_I(t_inode)->ip_lock);
  3525. OCFS2_I(t_inode)->ip_clusters = OCFS2_I(s_inode)->ip_clusters;
  3526. OCFS2_I(t_inode)->ip_attr = OCFS2_I(s_inode)->ip_attr;
  3527. OCFS2_I(t_inode)->ip_dyn_features = OCFS2_I(s_inode)->ip_dyn_features;
  3528. spin_unlock(&OCFS2_I(t_inode)->ip_lock);
  3529. i_size_write(t_inode, size);
  3530. t_inode->i_blocks = s_inode->i_blocks;
  3531. di->i_xattr_inline_size = s_di->i_xattr_inline_size;
  3532. di->i_clusters = s_di->i_clusters;
  3533. di->i_size = s_di->i_size;
  3534. di->i_dyn_features = s_di->i_dyn_features;
  3535. di->i_attr = s_di->i_attr;
  3536. if (preserve) {
  3537. t_inode->i_uid = s_inode->i_uid;
  3538. t_inode->i_gid = s_inode->i_gid;
  3539. t_inode->i_mode = s_inode->i_mode;
  3540. di->i_uid = s_di->i_uid;
  3541. di->i_gid = s_di->i_gid;
  3542. di->i_mode = s_di->i_mode;
  3543. /*
  3544. * update time.
  3545. * we want mtime to appear identical to the source and
  3546. * update ctime.
  3547. */
  3548. t_inode->i_ctime = CURRENT_TIME;
  3549. di->i_ctime = cpu_to_le64(t_inode->i_ctime.tv_sec);
  3550. di->i_ctime_nsec = cpu_to_le32(t_inode->i_ctime.tv_nsec);
  3551. t_inode->i_mtime = s_inode->i_mtime;
  3552. di->i_mtime = s_di->i_mtime;
  3553. di->i_mtime_nsec = s_di->i_mtime_nsec;
  3554. }
  3555. ocfs2_journal_dirty(handle, t_bh);
  3556. out_commit:
  3557. ocfs2_commit_trans(OCFS2_SB(t_inode->i_sb), handle);
  3558. return ret;
  3559. }
  3560. static int ocfs2_create_reflink_node(struct inode *s_inode,
  3561. struct buffer_head *s_bh,
  3562. struct inode *t_inode,
  3563. struct buffer_head *t_bh,
  3564. bool preserve)
  3565. {
  3566. int ret;
  3567. struct buffer_head *ref_root_bh = NULL;
  3568. struct ocfs2_cached_dealloc_ctxt dealloc;
  3569. struct ocfs2_super *osb = OCFS2_SB(s_inode->i_sb);
  3570. struct ocfs2_refcount_block *rb;
  3571. struct ocfs2_dinode *di = (struct ocfs2_dinode *)s_bh->b_data;
  3572. struct ocfs2_refcount_tree *ref_tree;
  3573. ocfs2_init_dealloc_ctxt(&dealloc);
  3574. ret = ocfs2_set_refcount_tree(t_inode, t_bh,
  3575. le64_to_cpu(di->i_refcount_loc));
  3576. if (ret) {
  3577. mlog_errno(ret);
  3578. goto out;
  3579. }
  3580. if (OCFS2_I(s_inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  3581. ret = ocfs2_duplicate_inline_data(s_inode, s_bh,
  3582. t_inode, t_bh);
  3583. if (ret)
  3584. mlog_errno(ret);
  3585. goto out;
  3586. }
  3587. ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
  3588. 1, &ref_tree, &ref_root_bh);
  3589. if (ret) {
  3590. mlog_errno(ret);
  3591. goto out;
  3592. }
  3593. rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
  3594. ret = ocfs2_duplicate_extent_list(s_inode, t_inode, t_bh,
  3595. &ref_tree->rf_ci, ref_root_bh,
  3596. &dealloc);
  3597. if (ret) {
  3598. mlog_errno(ret);
  3599. goto out_unlock_refcount;
  3600. }
  3601. out_unlock_refcount:
  3602. ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
  3603. brelse(ref_root_bh);
  3604. out:
  3605. if (ocfs2_dealloc_has_cluster(&dealloc)) {
  3606. ocfs2_schedule_truncate_log_flush(osb, 1);
  3607. ocfs2_run_deallocs(osb, &dealloc);
  3608. }
  3609. return ret;
  3610. }
  3611. static int __ocfs2_reflink(struct dentry *old_dentry,
  3612. struct buffer_head *old_bh,
  3613. struct inode *new_inode,
  3614. bool preserve)
  3615. {
  3616. int ret;
  3617. struct inode *inode = old_dentry->d_inode;
  3618. struct buffer_head *new_bh = NULL;
  3619. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SYSTEM_FILE) {
  3620. ret = -EINVAL;
  3621. mlog_errno(ret);
  3622. goto out;
  3623. }
  3624. ret = filemap_fdatawrite(inode->i_mapping);
  3625. if (ret) {
  3626. mlog_errno(ret);
  3627. goto out;
  3628. }
  3629. ret = ocfs2_attach_refcount_tree(inode, old_bh);
  3630. if (ret) {
  3631. mlog_errno(ret);
  3632. goto out;
  3633. }
  3634. mutex_lock_nested(&new_inode->i_mutex, I_MUTEX_CHILD);
  3635. ret = ocfs2_inode_lock_nested(new_inode, &new_bh, 1,
  3636. OI_LS_REFLINK_TARGET);
  3637. if (ret) {
  3638. mlog_errno(ret);
  3639. goto out_unlock;
  3640. }
  3641. ret = ocfs2_create_reflink_node(inode, old_bh,
  3642. new_inode, new_bh, preserve);
  3643. if (ret) {
  3644. mlog_errno(ret);
  3645. goto inode_unlock;
  3646. }
  3647. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_XATTR_FL) {
  3648. ret = ocfs2_reflink_xattrs(inode, old_bh,
  3649. new_inode, new_bh,
  3650. preserve);
  3651. if (ret) {
  3652. mlog_errno(ret);
  3653. goto inode_unlock;
  3654. }
  3655. }
  3656. ret = ocfs2_complete_reflink(inode, old_bh,
  3657. new_inode, new_bh, preserve);
  3658. if (ret)
  3659. mlog_errno(ret);
  3660. inode_unlock:
  3661. ocfs2_inode_unlock(new_inode, 1);
  3662. brelse(new_bh);
  3663. out_unlock:
  3664. mutex_unlock(&new_inode->i_mutex);
  3665. out:
  3666. if (!ret) {
  3667. ret = filemap_fdatawait(inode->i_mapping);
  3668. if (ret)
  3669. mlog_errno(ret);
  3670. }
  3671. return ret;
  3672. }
  3673. static int ocfs2_reflink(struct dentry *old_dentry, struct inode *dir,
  3674. struct dentry *new_dentry, bool preserve)
  3675. {
  3676. int error;
  3677. struct inode *inode = old_dentry->d_inode;
  3678. struct buffer_head *old_bh = NULL;
  3679. struct inode *new_orphan_inode = NULL;
  3680. if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb)))
  3681. return -EOPNOTSUPP;
  3682. error = ocfs2_create_inode_in_orphan(dir, inode->i_mode,
  3683. &new_orphan_inode);
  3684. if (error) {
  3685. mlog_errno(error);
  3686. goto out;
  3687. }
  3688. error = ocfs2_inode_lock(inode, &old_bh, 1);
  3689. if (error) {
  3690. mlog_errno(error);
  3691. goto out;
  3692. }
  3693. down_write(&OCFS2_I(inode)->ip_xattr_sem);
  3694. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  3695. error = __ocfs2_reflink(old_dentry, old_bh,
  3696. new_orphan_inode, preserve);
  3697. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  3698. up_write(&OCFS2_I(inode)->ip_xattr_sem);
  3699. ocfs2_inode_unlock(inode, 1);
  3700. brelse(old_bh);
  3701. if (error) {
  3702. mlog_errno(error);
  3703. goto out;
  3704. }
  3705. /* If the security isn't preserved, we need to re-initialize them. */
  3706. if (!preserve) {
  3707. error = ocfs2_init_security_and_acl(dir, new_orphan_inode,
  3708. &new_dentry->d_name);
  3709. if (error)
  3710. mlog_errno(error);
  3711. }
  3712. out:
  3713. if (!error) {
  3714. error = ocfs2_mv_orphaned_inode_to_new(dir, new_orphan_inode,
  3715. new_dentry);
  3716. if (error)
  3717. mlog_errno(error);
  3718. }
  3719. if (new_orphan_inode) {
  3720. /*
  3721. * We need to open_unlock the inode no matter whether we
  3722. * succeed or not, so that other nodes can delete it later.
  3723. */
  3724. ocfs2_open_unlock(new_orphan_inode);
  3725. if (error)
  3726. iput(new_orphan_inode);
  3727. }
  3728. return error;
  3729. }
  3730. /*
  3731. * Below here are the bits used by OCFS2_IOC_REFLINK() to fake
  3732. * sys_reflink(). This will go away when vfs_reflink() exists in
  3733. * fs/namei.c.
  3734. */
  3735. /* copied from may_create in VFS. */
  3736. static inline int ocfs2_may_create(struct inode *dir, struct dentry *child)
  3737. {
  3738. if (child->d_inode)
  3739. return -EEXIST;
  3740. if (IS_DEADDIR(dir))
  3741. return -ENOENT;
  3742. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  3743. }
  3744. /* copied from user_path_parent. */
  3745. static int ocfs2_user_path_parent(const char __user *path,
  3746. struct nameidata *nd, char **name)
  3747. {
  3748. char *s = getname(path);
  3749. int error;
  3750. if (IS_ERR(s))
  3751. return PTR_ERR(s);
  3752. error = kern_path_parent(s, nd);
  3753. if (error)
  3754. putname(s);
  3755. else
  3756. *name = s;
  3757. return error;
  3758. }
  3759. /**
  3760. * ocfs2_vfs_reflink - Create a reference-counted link
  3761. *
  3762. * @old_dentry: source dentry + inode
  3763. * @dir: directory to create the target
  3764. * @new_dentry: target dentry
  3765. * @preserve: if true, preserve all file attributes
  3766. */
  3767. static int ocfs2_vfs_reflink(struct dentry *old_dentry, struct inode *dir,
  3768. struct dentry *new_dentry, bool preserve)
  3769. {
  3770. struct inode *inode = old_dentry->d_inode;
  3771. int error;
  3772. if (!inode)
  3773. return -ENOENT;
  3774. error = ocfs2_may_create(dir, new_dentry);
  3775. if (error)
  3776. return error;
  3777. if (dir->i_sb != inode->i_sb)
  3778. return -EXDEV;
  3779. /*
  3780. * A reflink to an append-only or immutable file cannot be created.
  3781. */
  3782. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  3783. return -EPERM;
  3784. /* Only regular files can be reflinked. */
  3785. if (!S_ISREG(inode->i_mode))
  3786. return -EPERM;
  3787. /*
  3788. * If the caller wants to preserve ownership, they require the
  3789. * rights to do so.
  3790. */
  3791. if (preserve) {
  3792. if ((current_fsuid() != inode->i_uid) && !capable(CAP_CHOWN))
  3793. return -EPERM;
  3794. if (!in_group_p(inode->i_gid) && !capable(CAP_CHOWN))
  3795. return -EPERM;
  3796. }
  3797. /*
  3798. * If the caller is modifying any aspect of the attributes, they
  3799. * are not creating a snapshot. They need read permission on the
  3800. * file.
  3801. */
  3802. if (!preserve) {
  3803. error = inode_permission(inode, MAY_READ);
  3804. if (error)
  3805. return error;
  3806. }
  3807. mutex_lock(&inode->i_mutex);
  3808. dquot_initialize(dir);
  3809. error = ocfs2_reflink(old_dentry, dir, new_dentry, preserve);
  3810. mutex_unlock(&inode->i_mutex);
  3811. if (!error)
  3812. fsnotify_create(dir, new_dentry);
  3813. return error;
  3814. }
  3815. /*
  3816. * Most codes are copied from sys_linkat.
  3817. */
  3818. int ocfs2_reflink_ioctl(struct inode *inode,
  3819. const char __user *oldname,
  3820. const char __user *newname,
  3821. bool preserve)
  3822. {
  3823. struct dentry *new_dentry;
  3824. struct nameidata nd;
  3825. struct path old_path;
  3826. int error;
  3827. char *to = NULL;
  3828. if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb)))
  3829. return -EOPNOTSUPP;
  3830. error = user_path_at(AT_FDCWD, oldname, 0, &old_path);
  3831. if (error) {
  3832. mlog_errno(error);
  3833. return error;
  3834. }
  3835. error = ocfs2_user_path_parent(newname, &nd, &to);
  3836. if (error) {
  3837. mlog_errno(error);
  3838. goto out;
  3839. }
  3840. error = -EXDEV;
  3841. if (old_path.mnt != nd.path.mnt)
  3842. goto out_release;
  3843. new_dentry = lookup_create(&nd, 0);
  3844. error = PTR_ERR(new_dentry);
  3845. if (IS_ERR(new_dentry)) {
  3846. mlog_errno(error);
  3847. goto out_unlock;
  3848. }
  3849. error = mnt_want_write(nd.path.mnt);
  3850. if (error) {
  3851. mlog_errno(error);
  3852. goto out_dput;
  3853. }
  3854. error = ocfs2_vfs_reflink(old_path.dentry,
  3855. nd.path.dentry->d_inode,
  3856. new_dentry, preserve);
  3857. mnt_drop_write(nd.path.mnt);
  3858. out_dput:
  3859. dput(new_dentry);
  3860. out_unlock:
  3861. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  3862. out_release:
  3863. path_put(&nd.path);
  3864. putname(to);
  3865. out:
  3866. path_put(&old_path);
  3867. return error;
  3868. }