refcounttree.c 120 KB

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