refcounttree.c 123 KB

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