tree-log.c 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501
  1. /*
  2. * Copyright (C) 2008 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/sched.h>
  19. #include <linux/slab.h>
  20. #include <linux/blkdev.h>
  21. #include <linux/list_sort.h>
  22. #include "ctree.h"
  23. #include "transaction.h"
  24. #include "disk-io.h"
  25. #include "locking.h"
  26. #include "print-tree.h"
  27. #include "backref.h"
  28. #include "tree-log.h"
  29. #include "hash.h"
  30. /* magic values for the inode_only field in btrfs_log_inode:
  31. *
  32. * LOG_INODE_ALL means to log everything
  33. * LOG_INODE_EXISTS means to log just enough to recreate the inode
  34. * during log replay
  35. */
  36. #define LOG_INODE_ALL 0
  37. #define LOG_INODE_EXISTS 1
  38. /*
  39. * directory trouble cases
  40. *
  41. * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
  42. * log, we must force a full commit before doing an fsync of the directory
  43. * where the unlink was done.
  44. * ---> record transid of last unlink/rename per directory
  45. *
  46. * mkdir foo/some_dir
  47. * normal commit
  48. * rename foo/some_dir foo2/some_dir
  49. * mkdir foo/some_dir
  50. * fsync foo/some_dir/some_file
  51. *
  52. * The fsync above will unlink the original some_dir without recording
  53. * it in its new location (foo2). After a crash, some_dir will be gone
  54. * unless the fsync of some_file forces a full commit
  55. *
  56. * 2) we must log any new names for any file or dir that is in the fsync
  57. * log. ---> check inode while renaming/linking.
  58. *
  59. * 2a) we must log any new names for any file or dir during rename
  60. * when the directory they are being removed from was logged.
  61. * ---> check inode and old parent dir during rename
  62. *
  63. * 2a is actually the more important variant. With the extra logging
  64. * a crash might unlink the old name without recreating the new one
  65. *
  66. * 3) after a crash, we must go through any directories with a link count
  67. * of zero and redo the rm -rf
  68. *
  69. * mkdir f1/foo
  70. * normal commit
  71. * rm -rf f1/foo
  72. * fsync(f1)
  73. *
  74. * The directory f1 was fully removed from the FS, but fsync was never
  75. * called on f1, only its parent dir. After a crash the rm -rf must
  76. * be replayed. This must be able to recurse down the entire
  77. * directory tree. The inode link count fixup code takes care of the
  78. * ugly details.
  79. */
  80. /*
  81. * stages for the tree walking. The first
  82. * stage (0) is to only pin down the blocks we find
  83. * the second stage (1) is to make sure that all the inodes
  84. * we find in the log are created in the subvolume.
  85. *
  86. * The last stage is to deal with directories and links and extents
  87. * and all the other fun semantics
  88. */
  89. #define LOG_WALK_PIN_ONLY 0
  90. #define LOG_WALK_REPLAY_INODES 1
  91. #define LOG_WALK_REPLAY_DIR_INDEX 2
  92. #define LOG_WALK_REPLAY_ALL 3
  93. static int btrfs_log_inode(struct btrfs_trans_handle *trans,
  94. struct btrfs_root *root, struct inode *inode,
  95. int inode_only);
  96. static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
  97. struct btrfs_root *root,
  98. struct btrfs_path *path, u64 objectid);
  99. static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
  100. struct btrfs_root *root,
  101. struct btrfs_root *log,
  102. struct btrfs_path *path,
  103. u64 dirid, int del_all);
  104. /*
  105. * tree logging is a special write ahead log used to make sure that
  106. * fsyncs and O_SYNCs can happen without doing full tree commits.
  107. *
  108. * Full tree commits are expensive because they require commonly
  109. * modified blocks to be recowed, creating many dirty pages in the
  110. * extent tree an 4x-6x higher write load than ext3.
  111. *
  112. * Instead of doing a tree commit on every fsync, we use the
  113. * key ranges and transaction ids to find items for a given file or directory
  114. * that have changed in this transaction. Those items are copied into
  115. * a special tree (one per subvolume root), that tree is written to disk
  116. * and then the fsync is considered complete.
  117. *
  118. * After a crash, items are copied out of the log-tree back into the
  119. * subvolume tree. Any file data extents found are recorded in the extent
  120. * allocation tree, and the log-tree freed.
  121. *
  122. * The log tree is read three times, once to pin down all the extents it is
  123. * using in ram and once, once to create all the inodes logged in the tree
  124. * and once to do all the other items.
  125. */
  126. /*
  127. * start a sub transaction and setup the log tree
  128. * this increments the log tree writer count to make the people
  129. * syncing the tree wait for us to finish
  130. */
  131. static int start_log_trans(struct btrfs_trans_handle *trans,
  132. struct btrfs_root *root,
  133. struct btrfs_log_ctx *ctx)
  134. {
  135. int index;
  136. int ret;
  137. mutex_lock(&root->log_mutex);
  138. if (root->log_root) {
  139. if (!root->log_start_pid) {
  140. root->log_start_pid = current->pid;
  141. root->log_multiple_pids = false;
  142. } else if (root->log_start_pid != current->pid) {
  143. root->log_multiple_pids = true;
  144. }
  145. atomic_inc(&root->log_batch);
  146. atomic_inc(&root->log_writers);
  147. if (ctx) {
  148. index = root->log_transid % 2;
  149. list_add_tail(&ctx->list, &root->log_ctxs[index]);
  150. ctx->log_transid = root->log_transid;
  151. }
  152. mutex_unlock(&root->log_mutex);
  153. return 0;
  154. }
  155. ret = 0;
  156. mutex_lock(&root->fs_info->tree_log_mutex);
  157. if (!root->fs_info->log_root_tree)
  158. ret = btrfs_init_log_root_tree(trans, root->fs_info);
  159. mutex_unlock(&root->fs_info->tree_log_mutex);
  160. if (ret)
  161. goto out;
  162. if (!root->log_root) {
  163. ret = btrfs_add_log_tree(trans, root);
  164. if (ret)
  165. goto out;
  166. }
  167. root->log_multiple_pids = false;
  168. root->log_start_pid = current->pid;
  169. atomic_inc(&root->log_batch);
  170. atomic_inc(&root->log_writers);
  171. if (ctx) {
  172. index = root->log_transid % 2;
  173. list_add_tail(&ctx->list, &root->log_ctxs[index]);
  174. ctx->log_transid = root->log_transid;
  175. }
  176. out:
  177. mutex_unlock(&root->log_mutex);
  178. return ret;
  179. }
  180. /*
  181. * returns 0 if there was a log transaction running and we were able
  182. * to join, or returns -ENOENT if there were not transactions
  183. * in progress
  184. */
  185. static int join_running_log_trans(struct btrfs_root *root)
  186. {
  187. int ret = -ENOENT;
  188. smp_mb();
  189. if (!root->log_root)
  190. return -ENOENT;
  191. mutex_lock(&root->log_mutex);
  192. if (root->log_root) {
  193. ret = 0;
  194. atomic_inc(&root->log_writers);
  195. }
  196. mutex_unlock(&root->log_mutex);
  197. return ret;
  198. }
  199. /*
  200. * This either makes the current running log transaction wait
  201. * until you call btrfs_end_log_trans() or it makes any future
  202. * log transactions wait until you call btrfs_end_log_trans()
  203. */
  204. int btrfs_pin_log_trans(struct btrfs_root *root)
  205. {
  206. int ret = -ENOENT;
  207. mutex_lock(&root->log_mutex);
  208. atomic_inc(&root->log_writers);
  209. mutex_unlock(&root->log_mutex);
  210. return ret;
  211. }
  212. /*
  213. * indicate we're done making changes to the log tree
  214. * and wake up anyone waiting to do a sync
  215. */
  216. void btrfs_end_log_trans(struct btrfs_root *root)
  217. {
  218. if (atomic_dec_and_test(&root->log_writers)) {
  219. smp_mb();
  220. if (waitqueue_active(&root->log_writer_wait))
  221. wake_up(&root->log_writer_wait);
  222. }
  223. }
  224. /*
  225. * the walk control struct is used to pass state down the chain when
  226. * processing the log tree. The stage field tells us which part
  227. * of the log tree processing we are currently doing. The others
  228. * are state fields used for that specific part
  229. */
  230. struct walk_control {
  231. /* should we free the extent on disk when done? This is used
  232. * at transaction commit time while freeing a log tree
  233. */
  234. int free;
  235. /* should we write out the extent buffer? This is used
  236. * while flushing the log tree to disk during a sync
  237. */
  238. int write;
  239. /* should we wait for the extent buffer io to finish? Also used
  240. * while flushing the log tree to disk for a sync
  241. */
  242. int wait;
  243. /* pin only walk, we record which extents on disk belong to the
  244. * log trees
  245. */
  246. int pin;
  247. /* what stage of the replay code we're currently in */
  248. int stage;
  249. /* the root we are currently replaying */
  250. struct btrfs_root *replay_dest;
  251. /* the trans handle for the current replay */
  252. struct btrfs_trans_handle *trans;
  253. /* the function that gets used to process blocks we find in the
  254. * tree. Note the extent_buffer might not be up to date when it is
  255. * passed in, and it must be checked or read if you need the data
  256. * inside it
  257. */
  258. int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
  259. struct walk_control *wc, u64 gen);
  260. };
  261. /*
  262. * process_func used to pin down extents, write them or wait on them
  263. */
  264. static int process_one_buffer(struct btrfs_root *log,
  265. struct extent_buffer *eb,
  266. struct walk_control *wc, u64 gen)
  267. {
  268. int ret = 0;
  269. /*
  270. * If this fs is mixed then we need to be able to process the leaves to
  271. * pin down any logged extents, so we have to read the block.
  272. */
  273. if (btrfs_fs_incompat(log->fs_info, MIXED_GROUPS)) {
  274. ret = btrfs_read_buffer(eb, gen);
  275. if (ret)
  276. return ret;
  277. }
  278. if (wc->pin)
  279. ret = btrfs_pin_extent_for_log_replay(log->fs_info->extent_root,
  280. eb->start, eb->len);
  281. if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
  282. if (wc->pin && btrfs_header_level(eb) == 0)
  283. ret = btrfs_exclude_logged_extents(log, eb);
  284. if (wc->write)
  285. btrfs_write_tree_block(eb);
  286. if (wc->wait)
  287. btrfs_wait_tree_block_writeback(eb);
  288. }
  289. return ret;
  290. }
  291. /*
  292. * Item overwrite used by replay and tree logging. eb, slot and key all refer
  293. * to the src data we are copying out.
  294. *
  295. * root is the tree we are copying into, and path is a scratch
  296. * path for use in this function (it should be released on entry and
  297. * will be released on exit).
  298. *
  299. * If the key is already in the destination tree the existing item is
  300. * overwritten. If the existing item isn't big enough, it is extended.
  301. * If it is too large, it is truncated.
  302. *
  303. * If the key isn't in the destination yet, a new item is inserted.
  304. */
  305. static noinline int overwrite_item(struct btrfs_trans_handle *trans,
  306. struct btrfs_root *root,
  307. struct btrfs_path *path,
  308. struct extent_buffer *eb, int slot,
  309. struct btrfs_key *key)
  310. {
  311. int ret;
  312. u32 item_size;
  313. u64 saved_i_size = 0;
  314. int save_old_i_size = 0;
  315. unsigned long src_ptr;
  316. unsigned long dst_ptr;
  317. int overwrite_root = 0;
  318. bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
  319. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  320. overwrite_root = 1;
  321. item_size = btrfs_item_size_nr(eb, slot);
  322. src_ptr = btrfs_item_ptr_offset(eb, slot);
  323. /* look for the key in the destination tree */
  324. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  325. if (ret < 0)
  326. return ret;
  327. if (ret == 0) {
  328. char *src_copy;
  329. char *dst_copy;
  330. u32 dst_size = btrfs_item_size_nr(path->nodes[0],
  331. path->slots[0]);
  332. if (dst_size != item_size)
  333. goto insert;
  334. if (item_size == 0) {
  335. btrfs_release_path(path);
  336. return 0;
  337. }
  338. dst_copy = kmalloc(item_size, GFP_NOFS);
  339. src_copy = kmalloc(item_size, GFP_NOFS);
  340. if (!dst_copy || !src_copy) {
  341. btrfs_release_path(path);
  342. kfree(dst_copy);
  343. kfree(src_copy);
  344. return -ENOMEM;
  345. }
  346. read_extent_buffer(eb, src_copy, src_ptr, item_size);
  347. dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
  348. read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
  349. item_size);
  350. ret = memcmp(dst_copy, src_copy, item_size);
  351. kfree(dst_copy);
  352. kfree(src_copy);
  353. /*
  354. * they have the same contents, just return, this saves
  355. * us from cowing blocks in the destination tree and doing
  356. * extra writes that may not have been done by a previous
  357. * sync
  358. */
  359. if (ret == 0) {
  360. btrfs_release_path(path);
  361. return 0;
  362. }
  363. /*
  364. * We need to load the old nbytes into the inode so when we
  365. * replay the extents we've logged we get the right nbytes.
  366. */
  367. if (inode_item) {
  368. struct btrfs_inode_item *item;
  369. u64 nbytes;
  370. u32 mode;
  371. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  372. struct btrfs_inode_item);
  373. nbytes = btrfs_inode_nbytes(path->nodes[0], item);
  374. item = btrfs_item_ptr(eb, slot,
  375. struct btrfs_inode_item);
  376. btrfs_set_inode_nbytes(eb, item, nbytes);
  377. /*
  378. * If this is a directory we need to reset the i_size to
  379. * 0 so that we can set it up properly when replaying
  380. * the rest of the items in this log.
  381. */
  382. mode = btrfs_inode_mode(eb, item);
  383. if (S_ISDIR(mode))
  384. btrfs_set_inode_size(eb, item, 0);
  385. }
  386. } else if (inode_item) {
  387. struct btrfs_inode_item *item;
  388. u32 mode;
  389. /*
  390. * New inode, set nbytes to 0 so that the nbytes comes out
  391. * properly when we replay the extents.
  392. */
  393. item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
  394. btrfs_set_inode_nbytes(eb, item, 0);
  395. /*
  396. * If this is a directory we need to reset the i_size to 0 so
  397. * that we can set it up properly when replaying the rest of
  398. * the items in this log.
  399. */
  400. mode = btrfs_inode_mode(eb, item);
  401. if (S_ISDIR(mode))
  402. btrfs_set_inode_size(eb, item, 0);
  403. }
  404. insert:
  405. btrfs_release_path(path);
  406. /* try to insert the key into the destination tree */
  407. ret = btrfs_insert_empty_item(trans, root, path,
  408. key, item_size);
  409. /* make sure any existing item is the correct size */
  410. if (ret == -EEXIST) {
  411. u32 found_size;
  412. found_size = btrfs_item_size_nr(path->nodes[0],
  413. path->slots[0]);
  414. if (found_size > item_size)
  415. btrfs_truncate_item(root, path, item_size, 1);
  416. else if (found_size < item_size)
  417. btrfs_extend_item(root, path,
  418. item_size - found_size);
  419. } else if (ret) {
  420. return ret;
  421. }
  422. dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
  423. path->slots[0]);
  424. /* don't overwrite an existing inode if the generation number
  425. * was logged as zero. This is done when the tree logging code
  426. * is just logging an inode to make sure it exists after recovery.
  427. *
  428. * Also, don't overwrite i_size on directories during replay.
  429. * log replay inserts and removes directory items based on the
  430. * state of the tree found in the subvolume, and i_size is modified
  431. * as it goes
  432. */
  433. if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
  434. struct btrfs_inode_item *src_item;
  435. struct btrfs_inode_item *dst_item;
  436. src_item = (struct btrfs_inode_item *)src_ptr;
  437. dst_item = (struct btrfs_inode_item *)dst_ptr;
  438. if (btrfs_inode_generation(eb, src_item) == 0)
  439. goto no_copy;
  440. if (overwrite_root &&
  441. S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
  442. S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
  443. save_old_i_size = 1;
  444. saved_i_size = btrfs_inode_size(path->nodes[0],
  445. dst_item);
  446. }
  447. }
  448. copy_extent_buffer(path->nodes[0], eb, dst_ptr,
  449. src_ptr, item_size);
  450. if (save_old_i_size) {
  451. struct btrfs_inode_item *dst_item;
  452. dst_item = (struct btrfs_inode_item *)dst_ptr;
  453. btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
  454. }
  455. /* make sure the generation is filled in */
  456. if (key->type == BTRFS_INODE_ITEM_KEY) {
  457. struct btrfs_inode_item *dst_item;
  458. dst_item = (struct btrfs_inode_item *)dst_ptr;
  459. if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
  460. btrfs_set_inode_generation(path->nodes[0], dst_item,
  461. trans->transid);
  462. }
  463. }
  464. no_copy:
  465. btrfs_mark_buffer_dirty(path->nodes[0]);
  466. btrfs_release_path(path);
  467. return 0;
  468. }
  469. /*
  470. * simple helper to read an inode off the disk from a given root
  471. * This can only be called for subvolume roots and not for the log
  472. */
  473. static noinline struct inode *read_one_inode(struct btrfs_root *root,
  474. u64 objectid)
  475. {
  476. struct btrfs_key key;
  477. struct inode *inode;
  478. key.objectid = objectid;
  479. key.type = BTRFS_INODE_ITEM_KEY;
  480. key.offset = 0;
  481. inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
  482. if (IS_ERR(inode)) {
  483. inode = NULL;
  484. } else if (is_bad_inode(inode)) {
  485. iput(inode);
  486. inode = NULL;
  487. }
  488. return inode;
  489. }
  490. /* replays a single extent in 'eb' at 'slot' with 'key' into the
  491. * subvolume 'root'. path is released on entry and should be released
  492. * on exit.
  493. *
  494. * extents in the log tree have not been allocated out of the extent
  495. * tree yet. So, this completes the allocation, taking a reference
  496. * as required if the extent already exists or creating a new extent
  497. * if it isn't in the extent allocation tree yet.
  498. *
  499. * The extent is inserted into the file, dropping any existing extents
  500. * from the file that overlap the new one.
  501. */
  502. static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
  503. struct btrfs_root *root,
  504. struct btrfs_path *path,
  505. struct extent_buffer *eb, int slot,
  506. struct btrfs_key *key)
  507. {
  508. int found_type;
  509. u64 extent_end;
  510. u64 start = key->offset;
  511. u64 nbytes = 0;
  512. struct btrfs_file_extent_item *item;
  513. struct inode *inode = NULL;
  514. unsigned long size;
  515. int ret = 0;
  516. item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  517. found_type = btrfs_file_extent_type(eb, item);
  518. if (found_type == BTRFS_FILE_EXTENT_REG ||
  519. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  520. nbytes = btrfs_file_extent_num_bytes(eb, item);
  521. extent_end = start + nbytes;
  522. /*
  523. * We don't add to the inodes nbytes if we are prealloc or a
  524. * hole.
  525. */
  526. if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
  527. nbytes = 0;
  528. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  529. size = btrfs_file_extent_inline_len(eb, slot, item);
  530. nbytes = btrfs_file_extent_ram_bytes(eb, item);
  531. extent_end = ALIGN(start + size, root->sectorsize);
  532. } else {
  533. ret = 0;
  534. goto out;
  535. }
  536. inode = read_one_inode(root, key->objectid);
  537. if (!inode) {
  538. ret = -EIO;
  539. goto out;
  540. }
  541. /*
  542. * first check to see if we already have this extent in the
  543. * file. This must be done before the btrfs_drop_extents run
  544. * so we don't try to drop this extent.
  545. */
  546. ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
  547. start, 0);
  548. if (ret == 0 &&
  549. (found_type == BTRFS_FILE_EXTENT_REG ||
  550. found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
  551. struct btrfs_file_extent_item cmp1;
  552. struct btrfs_file_extent_item cmp2;
  553. struct btrfs_file_extent_item *existing;
  554. struct extent_buffer *leaf;
  555. leaf = path->nodes[0];
  556. existing = btrfs_item_ptr(leaf, path->slots[0],
  557. struct btrfs_file_extent_item);
  558. read_extent_buffer(eb, &cmp1, (unsigned long)item,
  559. sizeof(cmp1));
  560. read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
  561. sizeof(cmp2));
  562. /*
  563. * we already have a pointer to this exact extent,
  564. * we don't have to do anything
  565. */
  566. if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
  567. btrfs_release_path(path);
  568. goto out;
  569. }
  570. }
  571. btrfs_release_path(path);
  572. /* drop any overlapping extents */
  573. ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
  574. if (ret)
  575. goto out;
  576. if (found_type == BTRFS_FILE_EXTENT_REG ||
  577. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  578. u64 offset;
  579. unsigned long dest_offset;
  580. struct btrfs_key ins;
  581. ret = btrfs_insert_empty_item(trans, root, path, key,
  582. sizeof(*item));
  583. if (ret)
  584. goto out;
  585. dest_offset = btrfs_item_ptr_offset(path->nodes[0],
  586. path->slots[0]);
  587. copy_extent_buffer(path->nodes[0], eb, dest_offset,
  588. (unsigned long)item, sizeof(*item));
  589. ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
  590. ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
  591. ins.type = BTRFS_EXTENT_ITEM_KEY;
  592. offset = key->offset - btrfs_file_extent_offset(eb, item);
  593. if (ins.objectid > 0) {
  594. u64 csum_start;
  595. u64 csum_end;
  596. LIST_HEAD(ordered_sums);
  597. /*
  598. * is this extent already allocated in the extent
  599. * allocation tree? If so, just add a reference
  600. */
  601. ret = btrfs_lookup_extent(root, ins.objectid,
  602. ins.offset);
  603. if (ret == 0) {
  604. ret = btrfs_inc_extent_ref(trans, root,
  605. ins.objectid, ins.offset,
  606. 0, root->root_key.objectid,
  607. key->objectid, offset, 0);
  608. if (ret)
  609. goto out;
  610. } else {
  611. /*
  612. * insert the extent pointer in the extent
  613. * allocation tree
  614. */
  615. ret = btrfs_alloc_logged_file_extent(trans,
  616. root, root->root_key.objectid,
  617. key->objectid, offset, &ins);
  618. if (ret)
  619. goto out;
  620. }
  621. btrfs_release_path(path);
  622. if (btrfs_file_extent_compression(eb, item)) {
  623. csum_start = ins.objectid;
  624. csum_end = csum_start + ins.offset;
  625. } else {
  626. csum_start = ins.objectid +
  627. btrfs_file_extent_offset(eb, item);
  628. csum_end = csum_start +
  629. btrfs_file_extent_num_bytes(eb, item);
  630. }
  631. ret = btrfs_lookup_csums_range(root->log_root,
  632. csum_start, csum_end - 1,
  633. &ordered_sums, 0);
  634. if (ret)
  635. goto out;
  636. while (!list_empty(&ordered_sums)) {
  637. struct btrfs_ordered_sum *sums;
  638. sums = list_entry(ordered_sums.next,
  639. struct btrfs_ordered_sum,
  640. list);
  641. if (!ret)
  642. ret = btrfs_csum_file_blocks(trans,
  643. root->fs_info->csum_root,
  644. sums);
  645. list_del(&sums->list);
  646. kfree(sums);
  647. }
  648. if (ret)
  649. goto out;
  650. } else {
  651. btrfs_release_path(path);
  652. }
  653. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  654. /* inline extents are easy, we just overwrite them */
  655. ret = overwrite_item(trans, root, path, eb, slot, key);
  656. if (ret)
  657. goto out;
  658. }
  659. inode_add_bytes(inode, nbytes);
  660. ret = btrfs_update_inode(trans, root, inode);
  661. out:
  662. if (inode)
  663. iput(inode);
  664. return ret;
  665. }
  666. /*
  667. * when cleaning up conflicts between the directory names in the
  668. * subvolume, directory names in the log and directory names in the
  669. * inode back references, we may have to unlink inodes from directories.
  670. *
  671. * This is a helper function to do the unlink of a specific directory
  672. * item
  673. */
  674. static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
  675. struct btrfs_root *root,
  676. struct btrfs_path *path,
  677. struct inode *dir,
  678. struct btrfs_dir_item *di)
  679. {
  680. struct inode *inode;
  681. char *name;
  682. int name_len;
  683. struct extent_buffer *leaf;
  684. struct btrfs_key location;
  685. int ret;
  686. leaf = path->nodes[0];
  687. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  688. name_len = btrfs_dir_name_len(leaf, di);
  689. name = kmalloc(name_len, GFP_NOFS);
  690. if (!name)
  691. return -ENOMEM;
  692. read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
  693. btrfs_release_path(path);
  694. inode = read_one_inode(root, location.objectid);
  695. if (!inode) {
  696. ret = -EIO;
  697. goto out;
  698. }
  699. ret = link_to_fixup_dir(trans, root, path, location.objectid);
  700. if (ret)
  701. goto out;
  702. ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
  703. if (ret)
  704. goto out;
  705. else
  706. ret = btrfs_run_delayed_items(trans, root);
  707. out:
  708. kfree(name);
  709. iput(inode);
  710. return ret;
  711. }
  712. /*
  713. * helper function to see if a given name and sequence number found
  714. * in an inode back reference are already in a directory and correctly
  715. * point to this inode
  716. */
  717. static noinline int inode_in_dir(struct btrfs_root *root,
  718. struct btrfs_path *path,
  719. u64 dirid, u64 objectid, u64 index,
  720. const char *name, int name_len)
  721. {
  722. struct btrfs_dir_item *di;
  723. struct btrfs_key location;
  724. int match = 0;
  725. di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
  726. index, name, name_len, 0);
  727. if (di && !IS_ERR(di)) {
  728. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  729. if (location.objectid != objectid)
  730. goto out;
  731. } else
  732. goto out;
  733. btrfs_release_path(path);
  734. di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
  735. if (di && !IS_ERR(di)) {
  736. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  737. if (location.objectid != objectid)
  738. goto out;
  739. } else
  740. goto out;
  741. match = 1;
  742. out:
  743. btrfs_release_path(path);
  744. return match;
  745. }
  746. /*
  747. * helper function to check a log tree for a named back reference in
  748. * an inode. This is used to decide if a back reference that is
  749. * found in the subvolume conflicts with what we find in the log.
  750. *
  751. * inode backreferences may have multiple refs in a single item,
  752. * during replay we process one reference at a time, and we don't
  753. * want to delete valid links to a file from the subvolume if that
  754. * link is also in the log.
  755. */
  756. static noinline int backref_in_log(struct btrfs_root *log,
  757. struct btrfs_key *key,
  758. u64 ref_objectid,
  759. char *name, int namelen)
  760. {
  761. struct btrfs_path *path;
  762. struct btrfs_inode_ref *ref;
  763. unsigned long ptr;
  764. unsigned long ptr_end;
  765. unsigned long name_ptr;
  766. int found_name_len;
  767. int item_size;
  768. int ret;
  769. int match = 0;
  770. path = btrfs_alloc_path();
  771. if (!path)
  772. return -ENOMEM;
  773. ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
  774. if (ret != 0)
  775. goto out;
  776. ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
  777. if (key->type == BTRFS_INODE_EXTREF_KEY) {
  778. if (btrfs_find_name_in_ext_backref(path, ref_objectid,
  779. name, namelen, NULL))
  780. match = 1;
  781. goto out;
  782. }
  783. item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
  784. ptr_end = ptr + item_size;
  785. while (ptr < ptr_end) {
  786. ref = (struct btrfs_inode_ref *)ptr;
  787. found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
  788. if (found_name_len == namelen) {
  789. name_ptr = (unsigned long)(ref + 1);
  790. ret = memcmp_extent_buffer(path->nodes[0], name,
  791. name_ptr, namelen);
  792. if (ret == 0) {
  793. match = 1;
  794. goto out;
  795. }
  796. }
  797. ptr = (unsigned long)(ref + 1) + found_name_len;
  798. }
  799. out:
  800. btrfs_free_path(path);
  801. return match;
  802. }
  803. static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
  804. struct btrfs_root *root,
  805. struct btrfs_path *path,
  806. struct btrfs_root *log_root,
  807. struct inode *dir, struct inode *inode,
  808. struct extent_buffer *eb,
  809. u64 inode_objectid, u64 parent_objectid,
  810. u64 ref_index, char *name, int namelen,
  811. int *search_done)
  812. {
  813. int ret;
  814. char *victim_name;
  815. int victim_name_len;
  816. struct extent_buffer *leaf;
  817. struct btrfs_dir_item *di;
  818. struct btrfs_key search_key;
  819. struct btrfs_inode_extref *extref;
  820. again:
  821. /* Search old style refs */
  822. search_key.objectid = inode_objectid;
  823. search_key.type = BTRFS_INODE_REF_KEY;
  824. search_key.offset = parent_objectid;
  825. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  826. if (ret == 0) {
  827. struct btrfs_inode_ref *victim_ref;
  828. unsigned long ptr;
  829. unsigned long ptr_end;
  830. leaf = path->nodes[0];
  831. /* are we trying to overwrite a back ref for the root directory
  832. * if so, just jump out, we're done
  833. */
  834. if (search_key.objectid == search_key.offset)
  835. return 1;
  836. /* check all the names in this back reference to see
  837. * if they are in the log. if so, we allow them to stay
  838. * otherwise they must be unlinked as a conflict
  839. */
  840. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  841. ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
  842. while (ptr < ptr_end) {
  843. victim_ref = (struct btrfs_inode_ref *)ptr;
  844. victim_name_len = btrfs_inode_ref_name_len(leaf,
  845. victim_ref);
  846. victim_name = kmalloc(victim_name_len, GFP_NOFS);
  847. if (!victim_name)
  848. return -ENOMEM;
  849. read_extent_buffer(leaf, victim_name,
  850. (unsigned long)(victim_ref + 1),
  851. victim_name_len);
  852. if (!backref_in_log(log_root, &search_key,
  853. parent_objectid,
  854. victim_name,
  855. victim_name_len)) {
  856. inc_nlink(inode);
  857. btrfs_release_path(path);
  858. ret = btrfs_unlink_inode(trans, root, dir,
  859. inode, victim_name,
  860. victim_name_len);
  861. kfree(victim_name);
  862. if (ret)
  863. return ret;
  864. ret = btrfs_run_delayed_items(trans, root);
  865. if (ret)
  866. return ret;
  867. *search_done = 1;
  868. goto again;
  869. }
  870. kfree(victim_name);
  871. ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
  872. }
  873. /*
  874. * NOTE: we have searched root tree and checked the
  875. * coresponding ref, it does not need to check again.
  876. */
  877. *search_done = 1;
  878. }
  879. btrfs_release_path(path);
  880. /* Same search but for extended refs */
  881. extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
  882. inode_objectid, parent_objectid, 0,
  883. 0);
  884. if (!IS_ERR_OR_NULL(extref)) {
  885. u32 item_size;
  886. u32 cur_offset = 0;
  887. unsigned long base;
  888. struct inode *victim_parent;
  889. leaf = path->nodes[0];
  890. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  891. base = btrfs_item_ptr_offset(leaf, path->slots[0]);
  892. while (cur_offset < item_size) {
  893. extref = (struct btrfs_inode_extref *)base + cur_offset;
  894. victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
  895. if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
  896. goto next;
  897. victim_name = kmalloc(victim_name_len, GFP_NOFS);
  898. if (!victim_name)
  899. return -ENOMEM;
  900. read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
  901. victim_name_len);
  902. search_key.objectid = inode_objectid;
  903. search_key.type = BTRFS_INODE_EXTREF_KEY;
  904. search_key.offset = btrfs_extref_hash(parent_objectid,
  905. victim_name,
  906. victim_name_len);
  907. ret = 0;
  908. if (!backref_in_log(log_root, &search_key,
  909. parent_objectid, victim_name,
  910. victim_name_len)) {
  911. ret = -ENOENT;
  912. victim_parent = read_one_inode(root,
  913. parent_objectid);
  914. if (victim_parent) {
  915. inc_nlink(inode);
  916. btrfs_release_path(path);
  917. ret = btrfs_unlink_inode(trans, root,
  918. victim_parent,
  919. inode,
  920. victim_name,
  921. victim_name_len);
  922. if (!ret)
  923. ret = btrfs_run_delayed_items(
  924. trans, root);
  925. }
  926. iput(victim_parent);
  927. kfree(victim_name);
  928. if (ret)
  929. return ret;
  930. *search_done = 1;
  931. goto again;
  932. }
  933. kfree(victim_name);
  934. if (ret)
  935. return ret;
  936. next:
  937. cur_offset += victim_name_len + sizeof(*extref);
  938. }
  939. *search_done = 1;
  940. }
  941. btrfs_release_path(path);
  942. /* look for a conflicting sequence number */
  943. di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
  944. ref_index, name, namelen, 0);
  945. if (di && !IS_ERR(di)) {
  946. ret = drop_one_dir_item(trans, root, path, dir, di);
  947. if (ret)
  948. return ret;
  949. }
  950. btrfs_release_path(path);
  951. /* look for a conflicing name */
  952. di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
  953. name, namelen, 0);
  954. if (di && !IS_ERR(di)) {
  955. ret = drop_one_dir_item(trans, root, path, dir, di);
  956. if (ret)
  957. return ret;
  958. }
  959. btrfs_release_path(path);
  960. return 0;
  961. }
  962. static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
  963. u32 *namelen, char **name, u64 *index,
  964. u64 *parent_objectid)
  965. {
  966. struct btrfs_inode_extref *extref;
  967. extref = (struct btrfs_inode_extref *)ref_ptr;
  968. *namelen = btrfs_inode_extref_name_len(eb, extref);
  969. *name = kmalloc(*namelen, GFP_NOFS);
  970. if (*name == NULL)
  971. return -ENOMEM;
  972. read_extent_buffer(eb, *name, (unsigned long)&extref->name,
  973. *namelen);
  974. *index = btrfs_inode_extref_index(eb, extref);
  975. if (parent_objectid)
  976. *parent_objectid = btrfs_inode_extref_parent(eb, extref);
  977. return 0;
  978. }
  979. static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
  980. u32 *namelen, char **name, u64 *index)
  981. {
  982. struct btrfs_inode_ref *ref;
  983. ref = (struct btrfs_inode_ref *)ref_ptr;
  984. *namelen = btrfs_inode_ref_name_len(eb, ref);
  985. *name = kmalloc(*namelen, GFP_NOFS);
  986. if (*name == NULL)
  987. return -ENOMEM;
  988. read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
  989. *index = btrfs_inode_ref_index(eb, ref);
  990. return 0;
  991. }
  992. /*
  993. * replay one inode back reference item found in the log tree.
  994. * eb, slot and key refer to the buffer and key found in the log tree.
  995. * root is the destination we are replaying into, and path is for temp
  996. * use by this function. (it should be released on return).
  997. */
  998. static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
  999. struct btrfs_root *root,
  1000. struct btrfs_root *log,
  1001. struct btrfs_path *path,
  1002. struct extent_buffer *eb, int slot,
  1003. struct btrfs_key *key)
  1004. {
  1005. struct inode *dir = NULL;
  1006. struct inode *inode = NULL;
  1007. unsigned long ref_ptr;
  1008. unsigned long ref_end;
  1009. char *name = NULL;
  1010. int namelen;
  1011. int ret;
  1012. int search_done = 0;
  1013. int log_ref_ver = 0;
  1014. u64 parent_objectid;
  1015. u64 inode_objectid;
  1016. u64 ref_index = 0;
  1017. int ref_struct_size;
  1018. ref_ptr = btrfs_item_ptr_offset(eb, slot);
  1019. ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
  1020. if (key->type == BTRFS_INODE_EXTREF_KEY) {
  1021. struct btrfs_inode_extref *r;
  1022. ref_struct_size = sizeof(struct btrfs_inode_extref);
  1023. log_ref_ver = 1;
  1024. r = (struct btrfs_inode_extref *)ref_ptr;
  1025. parent_objectid = btrfs_inode_extref_parent(eb, r);
  1026. } else {
  1027. ref_struct_size = sizeof(struct btrfs_inode_ref);
  1028. parent_objectid = key->offset;
  1029. }
  1030. inode_objectid = key->objectid;
  1031. /*
  1032. * it is possible that we didn't log all the parent directories
  1033. * for a given inode. If we don't find the dir, just don't
  1034. * copy the back ref in. The link count fixup code will take
  1035. * care of the rest
  1036. */
  1037. dir = read_one_inode(root, parent_objectid);
  1038. if (!dir) {
  1039. ret = -ENOENT;
  1040. goto out;
  1041. }
  1042. inode = read_one_inode(root, inode_objectid);
  1043. if (!inode) {
  1044. ret = -EIO;
  1045. goto out;
  1046. }
  1047. while (ref_ptr < ref_end) {
  1048. if (log_ref_ver) {
  1049. ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
  1050. &ref_index, &parent_objectid);
  1051. /*
  1052. * parent object can change from one array
  1053. * item to another.
  1054. */
  1055. if (!dir)
  1056. dir = read_one_inode(root, parent_objectid);
  1057. if (!dir) {
  1058. ret = -ENOENT;
  1059. goto out;
  1060. }
  1061. } else {
  1062. ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
  1063. &ref_index);
  1064. }
  1065. if (ret)
  1066. goto out;
  1067. /* if we already have a perfect match, we're done */
  1068. if (!inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
  1069. ref_index, name, namelen)) {
  1070. /*
  1071. * look for a conflicting back reference in the
  1072. * metadata. if we find one we have to unlink that name
  1073. * of the file before we add our new link. Later on, we
  1074. * overwrite any existing back reference, and we don't
  1075. * want to create dangling pointers in the directory.
  1076. */
  1077. if (!search_done) {
  1078. ret = __add_inode_ref(trans, root, path, log,
  1079. dir, inode, eb,
  1080. inode_objectid,
  1081. parent_objectid,
  1082. ref_index, name, namelen,
  1083. &search_done);
  1084. if (ret) {
  1085. if (ret == 1)
  1086. ret = 0;
  1087. goto out;
  1088. }
  1089. }
  1090. /* insert our name */
  1091. ret = btrfs_add_link(trans, dir, inode, name, namelen,
  1092. 0, ref_index);
  1093. if (ret)
  1094. goto out;
  1095. btrfs_update_inode(trans, root, inode);
  1096. }
  1097. ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
  1098. kfree(name);
  1099. name = NULL;
  1100. if (log_ref_ver) {
  1101. iput(dir);
  1102. dir = NULL;
  1103. }
  1104. }
  1105. /* finally write the back reference in the inode */
  1106. ret = overwrite_item(trans, root, path, eb, slot, key);
  1107. out:
  1108. btrfs_release_path(path);
  1109. kfree(name);
  1110. iput(dir);
  1111. iput(inode);
  1112. return ret;
  1113. }
  1114. static int insert_orphan_item(struct btrfs_trans_handle *trans,
  1115. struct btrfs_root *root, u64 offset)
  1116. {
  1117. int ret;
  1118. ret = btrfs_find_item(root, NULL, BTRFS_ORPHAN_OBJECTID,
  1119. offset, BTRFS_ORPHAN_ITEM_KEY, NULL);
  1120. if (ret > 0)
  1121. ret = btrfs_insert_orphan_item(trans, root, offset);
  1122. return ret;
  1123. }
  1124. static int count_inode_extrefs(struct btrfs_root *root,
  1125. struct inode *inode, struct btrfs_path *path)
  1126. {
  1127. int ret = 0;
  1128. int name_len;
  1129. unsigned int nlink = 0;
  1130. u32 item_size;
  1131. u32 cur_offset = 0;
  1132. u64 inode_objectid = btrfs_ino(inode);
  1133. u64 offset = 0;
  1134. unsigned long ptr;
  1135. struct btrfs_inode_extref *extref;
  1136. struct extent_buffer *leaf;
  1137. while (1) {
  1138. ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
  1139. &extref, &offset);
  1140. if (ret)
  1141. break;
  1142. leaf = path->nodes[0];
  1143. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  1144. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  1145. while (cur_offset < item_size) {
  1146. extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
  1147. name_len = btrfs_inode_extref_name_len(leaf, extref);
  1148. nlink++;
  1149. cur_offset += name_len + sizeof(*extref);
  1150. }
  1151. offset++;
  1152. btrfs_release_path(path);
  1153. }
  1154. btrfs_release_path(path);
  1155. if (ret < 0)
  1156. return ret;
  1157. return nlink;
  1158. }
  1159. static int count_inode_refs(struct btrfs_root *root,
  1160. struct inode *inode, struct btrfs_path *path)
  1161. {
  1162. int ret;
  1163. struct btrfs_key key;
  1164. unsigned int nlink = 0;
  1165. unsigned long ptr;
  1166. unsigned long ptr_end;
  1167. int name_len;
  1168. u64 ino = btrfs_ino(inode);
  1169. key.objectid = ino;
  1170. key.type = BTRFS_INODE_REF_KEY;
  1171. key.offset = (u64)-1;
  1172. while (1) {
  1173. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1174. if (ret < 0)
  1175. break;
  1176. if (ret > 0) {
  1177. if (path->slots[0] == 0)
  1178. break;
  1179. path->slots[0]--;
  1180. }
  1181. process_slot:
  1182. btrfs_item_key_to_cpu(path->nodes[0], &key,
  1183. path->slots[0]);
  1184. if (key.objectid != ino ||
  1185. key.type != BTRFS_INODE_REF_KEY)
  1186. break;
  1187. ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
  1188. ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
  1189. path->slots[0]);
  1190. while (ptr < ptr_end) {
  1191. struct btrfs_inode_ref *ref;
  1192. ref = (struct btrfs_inode_ref *)ptr;
  1193. name_len = btrfs_inode_ref_name_len(path->nodes[0],
  1194. ref);
  1195. ptr = (unsigned long)(ref + 1) + name_len;
  1196. nlink++;
  1197. }
  1198. if (key.offset == 0)
  1199. break;
  1200. if (path->slots[0] > 0) {
  1201. path->slots[0]--;
  1202. goto process_slot;
  1203. }
  1204. key.offset--;
  1205. btrfs_release_path(path);
  1206. }
  1207. btrfs_release_path(path);
  1208. return nlink;
  1209. }
  1210. /*
  1211. * There are a few corners where the link count of the file can't
  1212. * be properly maintained during replay. So, instead of adding
  1213. * lots of complexity to the log code, we just scan the backrefs
  1214. * for any file that has been through replay.
  1215. *
  1216. * The scan will update the link count on the inode to reflect the
  1217. * number of back refs found. If it goes down to zero, the iput
  1218. * will free the inode.
  1219. */
  1220. static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
  1221. struct btrfs_root *root,
  1222. struct inode *inode)
  1223. {
  1224. struct btrfs_path *path;
  1225. int ret;
  1226. u64 nlink = 0;
  1227. u64 ino = btrfs_ino(inode);
  1228. path = btrfs_alloc_path();
  1229. if (!path)
  1230. return -ENOMEM;
  1231. ret = count_inode_refs(root, inode, path);
  1232. if (ret < 0)
  1233. goto out;
  1234. nlink = ret;
  1235. ret = count_inode_extrefs(root, inode, path);
  1236. if (ret == -ENOENT)
  1237. ret = 0;
  1238. if (ret < 0)
  1239. goto out;
  1240. nlink += ret;
  1241. ret = 0;
  1242. if (nlink != inode->i_nlink) {
  1243. set_nlink(inode, nlink);
  1244. btrfs_update_inode(trans, root, inode);
  1245. }
  1246. BTRFS_I(inode)->index_cnt = (u64)-1;
  1247. if (inode->i_nlink == 0) {
  1248. if (S_ISDIR(inode->i_mode)) {
  1249. ret = replay_dir_deletes(trans, root, NULL, path,
  1250. ino, 1);
  1251. if (ret)
  1252. goto out;
  1253. }
  1254. ret = insert_orphan_item(trans, root, ino);
  1255. }
  1256. out:
  1257. btrfs_free_path(path);
  1258. return ret;
  1259. }
  1260. static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
  1261. struct btrfs_root *root,
  1262. struct btrfs_path *path)
  1263. {
  1264. int ret;
  1265. struct btrfs_key key;
  1266. struct inode *inode;
  1267. key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
  1268. key.type = BTRFS_ORPHAN_ITEM_KEY;
  1269. key.offset = (u64)-1;
  1270. while (1) {
  1271. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1272. if (ret < 0)
  1273. break;
  1274. if (ret == 1) {
  1275. if (path->slots[0] == 0)
  1276. break;
  1277. path->slots[0]--;
  1278. }
  1279. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1280. if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
  1281. key.type != BTRFS_ORPHAN_ITEM_KEY)
  1282. break;
  1283. ret = btrfs_del_item(trans, root, path);
  1284. if (ret)
  1285. goto out;
  1286. btrfs_release_path(path);
  1287. inode = read_one_inode(root, key.offset);
  1288. if (!inode)
  1289. return -EIO;
  1290. ret = fixup_inode_link_count(trans, root, inode);
  1291. iput(inode);
  1292. if (ret)
  1293. goto out;
  1294. /*
  1295. * fixup on a directory may create new entries,
  1296. * make sure we always look for the highset possible
  1297. * offset
  1298. */
  1299. key.offset = (u64)-1;
  1300. }
  1301. ret = 0;
  1302. out:
  1303. btrfs_release_path(path);
  1304. return ret;
  1305. }
  1306. /*
  1307. * record a given inode in the fixup dir so we can check its link
  1308. * count when replay is done. The link count is incremented here
  1309. * so the inode won't go away until we check it
  1310. */
  1311. static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
  1312. struct btrfs_root *root,
  1313. struct btrfs_path *path,
  1314. u64 objectid)
  1315. {
  1316. struct btrfs_key key;
  1317. int ret = 0;
  1318. struct inode *inode;
  1319. inode = read_one_inode(root, objectid);
  1320. if (!inode)
  1321. return -EIO;
  1322. key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
  1323. btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
  1324. key.offset = objectid;
  1325. ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
  1326. btrfs_release_path(path);
  1327. if (ret == 0) {
  1328. if (!inode->i_nlink)
  1329. set_nlink(inode, 1);
  1330. else
  1331. inc_nlink(inode);
  1332. ret = btrfs_update_inode(trans, root, inode);
  1333. } else if (ret == -EEXIST) {
  1334. ret = 0;
  1335. } else {
  1336. BUG(); /* Logic Error */
  1337. }
  1338. iput(inode);
  1339. return ret;
  1340. }
  1341. /*
  1342. * when replaying the log for a directory, we only insert names
  1343. * for inodes that actually exist. This means an fsync on a directory
  1344. * does not implicitly fsync all the new files in it
  1345. */
  1346. static noinline int insert_one_name(struct btrfs_trans_handle *trans,
  1347. struct btrfs_root *root,
  1348. struct btrfs_path *path,
  1349. u64 dirid, u64 index,
  1350. char *name, int name_len, u8 type,
  1351. struct btrfs_key *location)
  1352. {
  1353. struct inode *inode;
  1354. struct inode *dir;
  1355. int ret;
  1356. inode = read_one_inode(root, location->objectid);
  1357. if (!inode)
  1358. return -ENOENT;
  1359. dir = read_one_inode(root, dirid);
  1360. if (!dir) {
  1361. iput(inode);
  1362. return -EIO;
  1363. }
  1364. ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
  1365. /* FIXME, put inode into FIXUP list */
  1366. iput(inode);
  1367. iput(dir);
  1368. return ret;
  1369. }
  1370. /*
  1371. * take a single entry in a log directory item and replay it into
  1372. * the subvolume.
  1373. *
  1374. * if a conflicting item exists in the subdirectory already,
  1375. * the inode it points to is unlinked and put into the link count
  1376. * fix up tree.
  1377. *
  1378. * If a name from the log points to a file or directory that does
  1379. * not exist in the FS, it is skipped. fsyncs on directories
  1380. * do not force down inodes inside that directory, just changes to the
  1381. * names or unlinks in a directory.
  1382. */
  1383. static noinline int replay_one_name(struct btrfs_trans_handle *trans,
  1384. struct btrfs_root *root,
  1385. struct btrfs_path *path,
  1386. struct extent_buffer *eb,
  1387. struct btrfs_dir_item *di,
  1388. struct btrfs_key *key)
  1389. {
  1390. char *name;
  1391. int name_len;
  1392. struct btrfs_dir_item *dst_di;
  1393. struct btrfs_key found_key;
  1394. struct btrfs_key log_key;
  1395. struct inode *dir;
  1396. u8 log_type;
  1397. int exists;
  1398. int ret = 0;
  1399. bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
  1400. dir = read_one_inode(root, key->objectid);
  1401. if (!dir)
  1402. return -EIO;
  1403. name_len = btrfs_dir_name_len(eb, di);
  1404. name = kmalloc(name_len, GFP_NOFS);
  1405. if (!name) {
  1406. ret = -ENOMEM;
  1407. goto out;
  1408. }
  1409. log_type = btrfs_dir_type(eb, di);
  1410. read_extent_buffer(eb, name, (unsigned long)(di + 1),
  1411. name_len);
  1412. btrfs_dir_item_key_to_cpu(eb, di, &log_key);
  1413. exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
  1414. if (exists == 0)
  1415. exists = 1;
  1416. else
  1417. exists = 0;
  1418. btrfs_release_path(path);
  1419. if (key->type == BTRFS_DIR_ITEM_KEY) {
  1420. dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
  1421. name, name_len, 1);
  1422. } else if (key->type == BTRFS_DIR_INDEX_KEY) {
  1423. dst_di = btrfs_lookup_dir_index_item(trans, root, path,
  1424. key->objectid,
  1425. key->offset, name,
  1426. name_len, 1);
  1427. } else {
  1428. /* Corruption */
  1429. ret = -EINVAL;
  1430. goto out;
  1431. }
  1432. if (IS_ERR_OR_NULL(dst_di)) {
  1433. /* we need a sequence number to insert, so we only
  1434. * do inserts for the BTRFS_DIR_INDEX_KEY types
  1435. */
  1436. if (key->type != BTRFS_DIR_INDEX_KEY)
  1437. goto out;
  1438. goto insert;
  1439. }
  1440. btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
  1441. /* the existing item matches the logged item */
  1442. if (found_key.objectid == log_key.objectid &&
  1443. found_key.type == log_key.type &&
  1444. found_key.offset == log_key.offset &&
  1445. btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
  1446. goto out;
  1447. }
  1448. /*
  1449. * don't drop the conflicting directory entry if the inode
  1450. * for the new entry doesn't exist
  1451. */
  1452. if (!exists)
  1453. goto out;
  1454. ret = drop_one_dir_item(trans, root, path, dir, dst_di);
  1455. if (ret)
  1456. goto out;
  1457. if (key->type == BTRFS_DIR_INDEX_KEY)
  1458. goto insert;
  1459. out:
  1460. btrfs_release_path(path);
  1461. if (!ret && update_size) {
  1462. btrfs_i_size_write(dir, dir->i_size + name_len * 2);
  1463. ret = btrfs_update_inode(trans, root, dir);
  1464. }
  1465. kfree(name);
  1466. iput(dir);
  1467. return ret;
  1468. insert:
  1469. btrfs_release_path(path);
  1470. ret = insert_one_name(trans, root, path, key->objectid, key->offset,
  1471. name, name_len, log_type, &log_key);
  1472. if (ret && ret != -ENOENT)
  1473. goto out;
  1474. update_size = false;
  1475. ret = 0;
  1476. goto out;
  1477. }
  1478. /*
  1479. * find all the names in a directory item and reconcile them into
  1480. * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
  1481. * one name in a directory item, but the same code gets used for
  1482. * both directory index types
  1483. */
  1484. static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
  1485. struct btrfs_root *root,
  1486. struct btrfs_path *path,
  1487. struct extent_buffer *eb, int slot,
  1488. struct btrfs_key *key)
  1489. {
  1490. int ret;
  1491. u32 item_size = btrfs_item_size_nr(eb, slot);
  1492. struct btrfs_dir_item *di;
  1493. int name_len;
  1494. unsigned long ptr;
  1495. unsigned long ptr_end;
  1496. ptr = btrfs_item_ptr_offset(eb, slot);
  1497. ptr_end = ptr + item_size;
  1498. while (ptr < ptr_end) {
  1499. di = (struct btrfs_dir_item *)ptr;
  1500. if (verify_dir_item(root, eb, di))
  1501. return -EIO;
  1502. name_len = btrfs_dir_name_len(eb, di);
  1503. ret = replay_one_name(trans, root, path, eb, di, key);
  1504. if (ret)
  1505. return ret;
  1506. ptr = (unsigned long)(di + 1);
  1507. ptr += name_len;
  1508. }
  1509. return 0;
  1510. }
  1511. /*
  1512. * directory replay has two parts. There are the standard directory
  1513. * items in the log copied from the subvolume, and range items
  1514. * created in the log while the subvolume was logged.
  1515. *
  1516. * The range items tell us which parts of the key space the log
  1517. * is authoritative for. During replay, if a key in the subvolume
  1518. * directory is in a logged range item, but not actually in the log
  1519. * that means it was deleted from the directory before the fsync
  1520. * and should be removed.
  1521. */
  1522. static noinline int find_dir_range(struct btrfs_root *root,
  1523. struct btrfs_path *path,
  1524. u64 dirid, int key_type,
  1525. u64 *start_ret, u64 *end_ret)
  1526. {
  1527. struct btrfs_key key;
  1528. u64 found_end;
  1529. struct btrfs_dir_log_item *item;
  1530. int ret;
  1531. int nritems;
  1532. if (*start_ret == (u64)-1)
  1533. return 1;
  1534. key.objectid = dirid;
  1535. key.type = key_type;
  1536. key.offset = *start_ret;
  1537. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1538. if (ret < 0)
  1539. goto out;
  1540. if (ret > 0) {
  1541. if (path->slots[0] == 0)
  1542. goto out;
  1543. path->slots[0]--;
  1544. }
  1545. if (ret != 0)
  1546. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1547. if (key.type != key_type || key.objectid != dirid) {
  1548. ret = 1;
  1549. goto next;
  1550. }
  1551. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1552. struct btrfs_dir_log_item);
  1553. found_end = btrfs_dir_log_end(path->nodes[0], item);
  1554. if (*start_ret >= key.offset && *start_ret <= found_end) {
  1555. ret = 0;
  1556. *start_ret = key.offset;
  1557. *end_ret = found_end;
  1558. goto out;
  1559. }
  1560. ret = 1;
  1561. next:
  1562. /* check the next slot in the tree to see if it is a valid item */
  1563. nritems = btrfs_header_nritems(path->nodes[0]);
  1564. if (path->slots[0] >= nritems) {
  1565. ret = btrfs_next_leaf(root, path);
  1566. if (ret)
  1567. goto out;
  1568. } else {
  1569. path->slots[0]++;
  1570. }
  1571. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1572. if (key.type != key_type || key.objectid != dirid) {
  1573. ret = 1;
  1574. goto out;
  1575. }
  1576. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1577. struct btrfs_dir_log_item);
  1578. found_end = btrfs_dir_log_end(path->nodes[0], item);
  1579. *start_ret = key.offset;
  1580. *end_ret = found_end;
  1581. ret = 0;
  1582. out:
  1583. btrfs_release_path(path);
  1584. return ret;
  1585. }
  1586. /*
  1587. * this looks for a given directory item in the log. If the directory
  1588. * item is not in the log, the item is removed and the inode it points
  1589. * to is unlinked
  1590. */
  1591. static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
  1592. struct btrfs_root *root,
  1593. struct btrfs_root *log,
  1594. struct btrfs_path *path,
  1595. struct btrfs_path *log_path,
  1596. struct inode *dir,
  1597. struct btrfs_key *dir_key)
  1598. {
  1599. int ret;
  1600. struct extent_buffer *eb;
  1601. int slot;
  1602. u32 item_size;
  1603. struct btrfs_dir_item *di;
  1604. struct btrfs_dir_item *log_di;
  1605. int name_len;
  1606. unsigned long ptr;
  1607. unsigned long ptr_end;
  1608. char *name;
  1609. struct inode *inode;
  1610. struct btrfs_key location;
  1611. again:
  1612. eb = path->nodes[0];
  1613. slot = path->slots[0];
  1614. item_size = btrfs_item_size_nr(eb, slot);
  1615. ptr = btrfs_item_ptr_offset(eb, slot);
  1616. ptr_end = ptr + item_size;
  1617. while (ptr < ptr_end) {
  1618. di = (struct btrfs_dir_item *)ptr;
  1619. if (verify_dir_item(root, eb, di)) {
  1620. ret = -EIO;
  1621. goto out;
  1622. }
  1623. name_len = btrfs_dir_name_len(eb, di);
  1624. name = kmalloc(name_len, GFP_NOFS);
  1625. if (!name) {
  1626. ret = -ENOMEM;
  1627. goto out;
  1628. }
  1629. read_extent_buffer(eb, name, (unsigned long)(di + 1),
  1630. name_len);
  1631. log_di = NULL;
  1632. if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
  1633. log_di = btrfs_lookup_dir_item(trans, log, log_path,
  1634. dir_key->objectid,
  1635. name, name_len, 0);
  1636. } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
  1637. log_di = btrfs_lookup_dir_index_item(trans, log,
  1638. log_path,
  1639. dir_key->objectid,
  1640. dir_key->offset,
  1641. name, name_len, 0);
  1642. }
  1643. if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) {
  1644. btrfs_dir_item_key_to_cpu(eb, di, &location);
  1645. btrfs_release_path(path);
  1646. btrfs_release_path(log_path);
  1647. inode = read_one_inode(root, location.objectid);
  1648. if (!inode) {
  1649. kfree(name);
  1650. return -EIO;
  1651. }
  1652. ret = link_to_fixup_dir(trans, root,
  1653. path, location.objectid);
  1654. if (ret) {
  1655. kfree(name);
  1656. iput(inode);
  1657. goto out;
  1658. }
  1659. inc_nlink(inode);
  1660. ret = btrfs_unlink_inode(trans, root, dir, inode,
  1661. name, name_len);
  1662. if (!ret)
  1663. ret = btrfs_run_delayed_items(trans, root);
  1664. kfree(name);
  1665. iput(inode);
  1666. if (ret)
  1667. goto out;
  1668. /* there might still be more names under this key
  1669. * check and repeat if required
  1670. */
  1671. ret = btrfs_search_slot(NULL, root, dir_key, path,
  1672. 0, 0);
  1673. if (ret == 0)
  1674. goto again;
  1675. ret = 0;
  1676. goto out;
  1677. } else if (IS_ERR(log_di)) {
  1678. kfree(name);
  1679. return PTR_ERR(log_di);
  1680. }
  1681. btrfs_release_path(log_path);
  1682. kfree(name);
  1683. ptr = (unsigned long)(di + 1);
  1684. ptr += name_len;
  1685. }
  1686. ret = 0;
  1687. out:
  1688. btrfs_release_path(path);
  1689. btrfs_release_path(log_path);
  1690. return ret;
  1691. }
  1692. /*
  1693. * deletion replay happens before we copy any new directory items
  1694. * out of the log or out of backreferences from inodes. It
  1695. * scans the log to find ranges of keys that log is authoritative for,
  1696. * and then scans the directory to find items in those ranges that are
  1697. * not present in the log.
  1698. *
  1699. * Anything we don't find in the log is unlinked and removed from the
  1700. * directory.
  1701. */
  1702. static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
  1703. struct btrfs_root *root,
  1704. struct btrfs_root *log,
  1705. struct btrfs_path *path,
  1706. u64 dirid, int del_all)
  1707. {
  1708. u64 range_start;
  1709. u64 range_end;
  1710. int key_type = BTRFS_DIR_LOG_ITEM_KEY;
  1711. int ret = 0;
  1712. struct btrfs_key dir_key;
  1713. struct btrfs_key found_key;
  1714. struct btrfs_path *log_path;
  1715. struct inode *dir;
  1716. dir_key.objectid = dirid;
  1717. dir_key.type = BTRFS_DIR_ITEM_KEY;
  1718. log_path = btrfs_alloc_path();
  1719. if (!log_path)
  1720. return -ENOMEM;
  1721. dir = read_one_inode(root, dirid);
  1722. /* it isn't an error if the inode isn't there, that can happen
  1723. * because we replay the deletes before we copy in the inode item
  1724. * from the log
  1725. */
  1726. if (!dir) {
  1727. btrfs_free_path(log_path);
  1728. return 0;
  1729. }
  1730. again:
  1731. range_start = 0;
  1732. range_end = 0;
  1733. while (1) {
  1734. if (del_all)
  1735. range_end = (u64)-1;
  1736. else {
  1737. ret = find_dir_range(log, path, dirid, key_type,
  1738. &range_start, &range_end);
  1739. if (ret != 0)
  1740. break;
  1741. }
  1742. dir_key.offset = range_start;
  1743. while (1) {
  1744. int nritems;
  1745. ret = btrfs_search_slot(NULL, root, &dir_key, path,
  1746. 0, 0);
  1747. if (ret < 0)
  1748. goto out;
  1749. nritems = btrfs_header_nritems(path->nodes[0]);
  1750. if (path->slots[0] >= nritems) {
  1751. ret = btrfs_next_leaf(root, path);
  1752. if (ret)
  1753. break;
  1754. }
  1755. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1756. path->slots[0]);
  1757. if (found_key.objectid != dirid ||
  1758. found_key.type != dir_key.type)
  1759. goto next_type;
  1760. if (found_key.offset > range_end)
  1761. break;
  1762. ret = check_item_in_log(trans, root, log, path,
  1763. log_path, dir,
  1764. &found_key);
  1765. if (ret)
  1766. goto out;
  1767. if (found_key.offset == (u64)-1)
  1768. break;
  1769. dir_key.offset = found_key.offset + 1;
  1770. }
  1771. btrfs_release_path(path);
  1772. if (range_end == (u64)-1)
  1773. break;
  1774. range_start = range_end + 1;
  1775. }
  1776. next_type:
  1777. ret = 0;
  1778. if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
  1779. key_type = BTRFS_DIR_LOG_INDEX_KEY;
  1780. dir_key.type = BTRFS_DIR_INDEX_KEY;
  1781. btrfs_release_path(path);
  1782. goto again;
  1783. }
  1784. out:
  1785. btrfs_release_path(path);
  1786. btrfs_free_path(log_path);
  1787. iput(dir);
  1788. return ret;
  1789. }
  1790. /*
  1791. * the process_func used to replay items from the log tree. This
  1792. * gets called in two different stages. The first stage just looks
  1793. * for inodes and makes sure they are all copied into the subvolume.
  1794. *
  1795. * The second stage copies all the other item types from the log into
  1796. * the subvolume. The two stage approach is slower, but gets rid of
  1797. * lots of complexity around inodes referencing other inodes that exist
  1798. * only in the log (references come from either directory items or inode
  1799. * back refs).
  1800. */
  1801. static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
  1802. struct walk_control *wc, u64 gen)
  1803. {
  1804. int nritems;
  1805. struct btrfs_path *path;
  1806. struct btrfs_root *root = wc->replay_dest;
  1807. struct btrfs_key key;
  1808. int level;
  1809. int i;
  1810. int ret;
  1811. ret = btrfs_read_buffer(eb, gen);
  1812. if (ret)
  1813. return ret;
  1814. level = btrfs_header_level(eb);
  1815. if (level != 0)
  1816. return 0;
  1817. path = btrfs_alloc_path();
  1818. if (!path)
  1819. return -ENOMEM;
  1820. nritems = btrfs_header_nritems(eb);
  1821. for (i = 0; i < nritems; i++) {
  1822. btrfs_item_key_to_cpu(eb, &key, i);
  1823. /* inode keys are done during the first stage */
  1824. if (key.type == BTRFS_INODE_ITEM_KEY &&
  1825. wc->stage == LOG_WALK_REPLAY_INODES) {
  1826. struct btrfs_inode_item *inode_item;
  1827. u32 mode;
  1828. inode_item = btrfs_item_ptr(eb, i,
  1829. struct btrfs_inode_item);
  1830. mode = btrfs_inode_mode(eb, inode_item);
  1831. if (S_ISDIR(mode)) {
  1832. ret = replay_dir_deletes(wc->trans,
  1833. root, log, path, key.objectid, 0);
  1834. if (ret)
  1835. break;
  1836. }
  1837. ret = overwrite_item(wc->trans, root, path,
  1838. eb, i, &key);
  1839. if (ret)
  1840. break;
  1841. /* for regular files, make sure corresponding
  1842. * orhpan item exist. extents past the new EOF
  1843. * will be truncated later by orphan cleanup.
  1844. */
  1845. if (S_ISREG(mode)) {
  1846. ret = insert_orphan_item(wc->trans, root,
  1847. key.objectid);
  1848. if (ret)
  1849. break;
  1850. }
  1851. ret = link_to_fixup_dir(wc->trans, root,
  1852. path, key.objectid);
  1853. if (ret)
  1854. break;
  1855. }
  1856. if (key.type == BTRFS_DIR_INDEX_KEY &&
  1857. wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
  1858. ret = replay_one_dir_item(wc->trans, root, path,
  1859. eb, i, &key);
  1860. if (ret)
  1861. break;
  1862. }
  1863. if (wc->stage < LOG_WALK_REPLAY_ALL)
  1864. continue;
  1865. /* these keys are simply copied */
  1866. if (key.type == BTRFS_XATTR_ITEM_KEY) {
  1867. ret = overwrite_item(wc->trans, root, path,
  1868. eb, i, &key);
  1869. if (ret)
  1870. break;
  1871. } else if (key.type == BTRFS_INODE_REF_KEY ||
  1872. key.type == BTRFS_INODE_EXTREF_KEY) {
  1873. ret = add_inode_ref(wc->trans, root, log, path,
  1874. eb, i, &key);
  1875. if (ret && ret != -ENOENT)
  1876. break;
  1877. ret = 0;
  1878. } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
  1879. ret = replay_one_extent(wc->trans, root, path,
  1880. eb, i, &key);
  1881. if (ret)
  1882. break;
  1883. } else if (key.type == BTRFS_DIR_ITEM_KEY) {
  1884. ret = replay_one_dir_item(wc->trans, root, path,
  1885. eb, i, &key);
  1886. if (ret)
  1887. break;
  1888. }
  1889. }
  1890. btrfs_free_path(path);
  1891. return ret;
  1892. }
  1893. static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
  1894. struct btrfs_root *root,
  1895. struct btrfs_path *path, int *level,
  1896. struct walk_control *wc)
  1897. {
  1898. u64 root_owner;
  1899. u64 bytenr;
  1900. u64 ptr_gen;
  1901. struct extent_buffer *next;
  1902. struct extent_buffer *cur;
  1903. struct extent_buffer *parent;
  1904. u32 blocksize;
  1905. int ret = 0;
  1906. WARN_ON(*level < 0);
  1907. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1908. while (*level > 0) {
  1909. WARN_ON(*level < 0);
  1910. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1911. cur = path->nodes[*level];
  1912. WARN_ON(btrfs_header_level(cur) != *level);
  1913. if (path->slots[*level] >=
  1914. btrfs_header_nritems(cur))
  1915. break;
  1916. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  1917. ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
  1918. blocksize = btrfs_level_size(root, *level - 1);
  1919. parent = path->nodes[*level];
  1920. root_owner = btrfs_header_owner(parent);
  1921. next = btrfs_find_create_tree_block(root, bytenr, blocksize);
  1922. if (!next)
  1923. return -ENOMEM;
  1924. if (*level == 1) {
  1925. ret = wc->process_func(root, next, wc, ptr_gen);
  1926. if (ret) {
  1927. free_extent_buffer(next);
  1928. return ret;
  1929. }
  1930. path->slots[*level]++;
  1931. if (wc->free) {
  1932. ret = btrfs_read_buffer(next, ptr_gen);
  1933. if (ret) {
  1934. free_extent_buffer(next);
  1935. return ret;
  1936. }
  1937. if (trans) {
  1938. btrfs_tree_lock(next);
  1939. btrfs_set_lock_blocking(next);
  1940. clean_tree_block(trans, root, next);
  1941. btrfs_wait_tree_block_writeback(next);
  1942. btrfs_tree_unlock(next);
  1943. }
  1944. WARN_ON(root_owner !=
  1945. BTRFS_TREE_LOG_OBJECTID);
  1946. ret = btrfs_free_and_pin_reserved_extent(root,
  1947. bytenr, blocksize);
  1948. if (ret) {
  1949. free_extent_buffer(next);
  1950. return ret;
  1951. }
  1952. }
  1953. free_extent_buffer(next);
  1954. continue;
  1955. }
  1956. ret = btrfs_read_buffer(next, ptr_gen);
  1957. if (ret) {
  1958. free_extent_buffer(next);
  1959. return ret;
  1960. }
  1961. WARN_ON(*level <= 0);
  1962. if (path->nodes[*level-1])
  1963. free_extent_buffer(path->nodes[*level-1]);
  1964. path->nodes[*level-1] = next;
  1965. *level = btrfs_header_level(next);
  1966. path->slots[*level] = 0;
  1967. cond_resched();
  1968. }
  1969. WARN_ON(*level < 0);
  1970. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1971. path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
  1972. cond_resched();
  1973. return 0;
  1974. }
  1975. static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
  1976. struct btrfs_root *root,
  1977. struct btrfs_path *path, int *level,
  1978. struct walk_control *wc)
  1979. {
  1980. u64 root_owner;
  1981. int i;
  1982. int slot;
  1983. int ret;
  1984. for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
  1985. slot = path->slots[i];
  1986. if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
  1987. path->slots[i]++;
  1988. *level = i;
  1989. WARN_ON(*level == 0);
  1990. return 0;
  1991. } else {
  1992. struct extent_buffer *parent;
  1993. if (path->nodes[*level] == root->node)
  1994. parent = path->nodes[*level];
  1995. else
  1996. parent = path->nodes[*level + 1];
  1997. root_owner = btrfs_header_owner(parent);
  1998. ret = wc->process_func(root, path->nodes[*level], wc,
  1999. btrfs_header_generation(path->nodes[*level]));
  2000. if (ret)
  2001. return ret;
  2002. if (wc->free) {
  2003. struct extent_buffer *next;
  2004. next = path->nodes[*level];
  2005. if (trans) {
  2006. btrfs_tree_lock(next);
  2007. btrfs_set_lock_blocking(next);
  2008. clean_tree_block(trans, root, next);
  2009. btrfs_wait_tree_block_writeback(next);
  2010. btrfs_tree_unlock(next);
  2011. }
  2012. WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
  2013. ret = btrfs_free_and_pin_reserved_extent(root,
  2014. path->nodes[*level]->start,
  2015. path->nodes[*level]->len);
  2016. if (ret)
  2017. return ret;
  2018. }
  2019. free_extent_buffer(path->nodes[*level]);
  2020. path->nodes[*level] = NULL;
  2021. *level = i + 1;
  2022. }
  2023. }
  2024. return 1;
  2025. }
  2026. /*
  2027. * drop the reference count on the tree rooted at 'snap'. This traverses
  2028. * the tree freeing any blocks that have a ref count of zero after being
  2029. * decremented.
  2030. */
  2031. static int walk_log_tree(struct btrfs_trans_handle *trans,
  2032. struct btrfs_root *log, struct walk_control *wc)
  2033. {
  2034. int ret = 0;
  2035. int wret;
  2036. int level;
  2037. struct btrfs_path *path;
  2038. int orig_level;
  2039. path = btrfs_alloc_path();
  2040. if (!path)
  2041. return -ENOMEM;
  2042. level = btrfs_header_level(log->node);
  2043. orig_level = level;
  2044. path->nodes[level] = log->node;
  2045. extent_buffer_get(log->node);
  2046. path->slots[level] = 0;
  2047. while (1) {
  2048. wret = walk_down_log_tree(trans, log, path, &level, wc);
  2049. if (wret > 0)
  2050. break;
  2051. if (wret < 0) {
  2052. ret = wret;
  2053. goto out;
  2054. }
  2055. wret = walk_up_log_tree(trans, log, path, &level, wc);
  2056. if (wret > 0)
  2057. break;
  2058. if (wret < 0) {
  2059. ret = wret;
  2060. goto out;
  2061. }
  2062. }
  2063. /* was the root node processed? if not, catch it here */
  2064. if (path->nodes[orig_level]) {
  2065. ret = wc->process_func(log, path->nodes[orig_level], wc,
  2066. btrfs_header_generation(path->nodes[orig_level]));
  2067. if (ret)
  2068. goto out;
  2069. if (wc->free) {
  2070. struct extent_buffer *next;
  2071. next = path->nodes[orig_level];
  2072. if (trans) {
  2073. btrfs_tree_lock(next);
  2074. btrfs_set_lock_blocking(next);
  2075. clean_tree_block(trans, log, next);
  2076. btrfs_wait_tree_block_writeback(next);
  2077. btrfs_tree_unlock(next);
  2078. }
  2079. WARN_ON(log->root_key.objectid !=
  2080. BTRFS_TREE_LOG_OBJECTID);
  2081. ret = btrfs_free_and_pin_reserved_extent(log, next->start,
  2082. next->len);
  2083. if (ret)
  2084. goto out;
  2085. }
  2086. }
  2087. out:
  2088. btrfs_free_path(path);
  2089. return ret;
  2090. }
  2091. /*
  2092. * helper function to update the item for a given subvolumes log root
  2093. * in the tree of log roots
  2094. */
  2095. static int update_log_root(struct btrfs_trans_handle *trans,
  2096. struct btrfs_root *log)
  2097. {
  2098. int ret;
  2099. if (log->log_transid == 1) {
  2100. /* insert root item on the first sync */
  2101. ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
  2102. &log->root_key, &log->root_item);
  2103. } else {
  2104. ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
  2105. &log->root_key, &log->root_item);
  2106. }
  2107. return ret;
  2108. }
  2109. static void wait_log_commit(struct btrfs_trans_handle *trans,
  2110. struct btrfs_root *root, int transid)
  2111. {
  2112. DEFINE_WAIT(wait);
  2113. int index = transid % 2;
  2114. /*
  2115. * we only allow two pending log transactions at a time,
  2116. * so we know that if ours is more than 2 older than the
  2117. * current transaction, we're done
  2118. */
  2119. do {
  2120. prepare_to_wait(&root->log_commit_wait[index],
  2121. &wait, TASK_UNINTERRUPTIBLE);
  2122. mutex_unlock(&root->log_mutex);
  2123. if (root->log_transid_committed < transid &&
  2124. atomic_read(&root->log_commit[index]))
  2125. schedule();
  2126. finish_wait(&root->log_commit_wait[index], &wait);
  2127. mutex_lock(&root->log_mutex);
  2128. } while (root->log_transid_committed < transid &&
  2129. atomic_read(&root->log_commit[index]));
  2130. }
  2131. static void wait_for_writer(struct btrfs_trans_handle *trans,
  2132. struct btrfs_root *root)
  2133. {
  2134. DEFINE_WAIT(wait);
  2135. while (atomic_read(&root->log_writers)) {
  2136. prepare_to_wait(&root->log_writer_wait,
  2137. &wait, TASK_UNINTERRUPTIBLE);
  2138. mutex_unlock(&root->log_mutex);
  2139. if (atomic_read(&root->log_writers))
  2140. schedule();
  2141. mutex_lock(&root->log_mutex);
  2142. finish_wait(&root->log_writer_wait, &wait);
  2143. }
  2144. }
  2145. static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
  2146. struct btrfs_log_ctx *ctx)
  2147. {
  2148. if (!ctx)
  2149. return;
  2150. mutex_lock(&root->log_mutex);
  2151. list_del_init(&ctx->list);
  2152. mutex_unlock(&root->log_mutex);
  2153. }
  2154. /*
  2155. * Invoked in log mutex context, or be sure there is no other task which
  2156. * can access the list.
  2157. */
  2158. static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
  2159. int index, int error)
  2160. {
  2161. struct btrfs_log_ctx *ctx;
  2162. if (!error) {
  2163. INIT_LIST_HEAD(&root->log_ctxs[index]);
  2164. return;
  2165. }
  2166. list_for_each_entry(ctx, &root->log_ctxs[index], list)
  2167. ctx->log_ret = error;
  2168. INIT_LIST_HEAD(&root->log_ctxs[index]);
  2169. }
  2170. /*
  2171. * btrfs_sync_log does sends a given tree log down to the disk and
  2172. * updates the super blocks to record it. When this call is done,
  2173. * you know that any inodes previously logged are safely on disk only
  2174. * if it returns 0.
  2175. *
  2176. * Any other return value means you need to call btrfs_commit_transaction.
  2177. * Some of the edge cases for fsyncing directories that have had unlinks
  2178. * or renames done in the past mean that sometimes the only safe
  2179. * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
  2180. * that has happened.
  2181. */
  2182. int btrfs_sync_log(struct btrfs_trans_handle *trans,
  2183. struct btrfs_root *root, struct btrfs_log_ctx *ctx)
  2184. {
  2185. int index1;
  2186. int index2;
  2187. int mark;
  2188. int ret;
  2189. struct btrfs_root *log = root->log_root;
  2190. struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
  2191. int log_transid = 0;
  2192. struct btrfs_log_ctx root_log_ctx;
  2193. struct blk_plug plug;
  2194. mutex_lock(&root->log_mutex);
  2195. log_transid = ctx->log_transid;
  2196. if (root->log_transid_committed >= log_transid) {
  2197. mutex_unlock(&root->log_mutex);
  2198. return ctx->log_ret;
  2199. }
  2200. index1 = log_transid % 2;
  2201. if (atomic_read(&root->log_commit[index1])) {
  2202. wait_log_commit(trans, root, log_transid);
  2203. mutex_unlock(&root->log_mutex);
  2204. return ctx->log_ret;
  2205. }
  2206. ASSERT(log_transid == root->log_transid);
  2207. atomic_set(&root->log_commit[index1], 1);
  2208. /* wait for previous tree log sync to complete */
  2209. if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
  2210. wait_log_commit(trans, root, log_transid - 1);
  2211. while (1) {
  2212. int batch = atomic_read(&root->log_batch);
  2213. /* when we're on an ssd, just kick the log commit out */
  2214. if (!btrfs_test_opt(root, SSD) && root->log_multiple_pids) {
  2215. mutex_unlock(&root->log_mutex);
  2216. schedule_timeout_uninterruptible(1);
  2217. mutex_lock(&root->log_mutex);
  2218. }
  2219. wait_for_writer(trans, root);
  2220. if (batch == atomic_read(&root->log_batch))
  2221. break;
  2222. }
  2223. /* bail out if we need to do a full commit */
  2224. if (ACCESS_ONCE(root->fs_info->last_trans_log_full_commit) ==
  2225. trans->transid) {
  2226. ret = -EAGAIN;
  2227. btrfs_free_logged_extents(log, log_transid);
  2228. mutex_unlock(&root->log_mutex);
  2229. goto out;
  2230. }
  2231. if (log_transid % 2 == 0)
  2232. mark = EXTENT_DIRTY;
  2233. else
  2234. mark = EXTENT_NEW;
  2235. /* we start IO on all the marked extents here, but we don't actually
  2236. * wait for them until later.
  2237. */
  2238. blk_start_plug(&plug);
  2239. ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
  2240. if (ret) {
  2241. blk_finish_plug(&plug);
  2242. btrfs_abort_transaction(trans, root, ret);
  2243. btrfs_free_logged_extents(log, log_transid);
  2244. mutex_unlock(&root->log_mutex);
  2245. goto out;
  2246. }
  2247. btrfs_set_root_node(&log->root_item, log->node);
  2248. root->log_transid++;
  2249. log->log_transid = root->log_transid;
  2250. root->log_start_pid = 0;
  2251. /*
  2252. * IO has been started, blocks of the log tree have WRITTEN flag set
  2253. * in their headers. new modifications of the log will be written to
  2254. * new positions. so it's safe to allow log writers to go in.
  2255. */
  2256. mutex_unlock(&root->log_mutex);
  2257. btrfs_init_log_ctx(&root_log_ctx);
  2258. mutex_lock(&log_root_tree->log_mutex);
  2259. atomic_inc(&log_root_tree->log_batch);
  2260. atomic_inc(&log_root_tree->log_writers);
  2261. index2 = log_root_tree->log_transid % 2;
  2262. list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
  2263. root_log_ctx.log_transid = log_root_tree->log_transid;
  2264. mutex_unlock(&log_root_tree->log_mutex);
  2265. ret = update_log_root(trans, log);
  2266. mutex_lock(&log_root_tree->log_mutex);
  2267. if (atomic_dec_and_test(&log_root_tree->log_writers)) {
  2268. smp_mb();
  2269. if (waitqueue_active(&log_root_tree->log_writer_wait))
  2270. wake_up(&log_root_tree->log_writer_wait);
  2271. }
  2272. if (ret) {
  2273. if (!list_empty(&root_log_ctx.list))
  2274. list_del_init(&root_log_ctx.list);
  2275. blk_finish_plug(&plug);
  2276. if (ret != -ENOSPC) {
  2277. btrfs_abort_transaction(trans, root, ret);
  2278. mutex_unlock(&log_root_tree->log_mutex);
  2279. goto out;
  2280. }
  2281. ACCESS_ONCE(root->fs_info->last_trans_log_full_commit) =
  2282. trans->transid;
  2283. btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
  2284. btrfs_free_logged_extents(log, log_transid);
  2285. mutex_unlock(&log_root_tree->log_mutex);
  2286. ret = -EAGAIN;
  2287. goto out;
  2288. }
  2289. if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
  2290. mutex_unlock(&log_root_tree->log_mutex);
  2291. ret = root_log_ctx.log_ret;
  2292. goto out;
  2293. }
  2294. index2 = root_log_ctx.log_transid % 2;
  2295. if (atomic_read(&log_root_tree->log_commit[index2])) {
  2296. blk_finish_plug(&plug);
  2297. btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
  2298. wait_log_commit(trans, log_root_tree,
  2299. root_log_ctx.log_transid);
  2300. btrfs_free_logged_extents(log, log_transid);
  2301. mutex_unlock(&log_root_tree->log_mutex);
  2302. ret = root_log_ctx.log_ret;
  2303. goto out;
  2304. }
  2305. ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
  2306. atomic_set(&log_root_tree->log_commit[index2], 1);
  2307. if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
  2308. wait_log_commit(trans, log_root_tree,
  2309. root_log_ctx.log_transid - 1);
  2310. }
  2311. wait_for_writer(trans, log_root_tree);
  2312. /*
  2313. * now that we've moved on to the tree of log tree roots,
  2314. * check the full commit flag again
  2315. */
  2316. if (ACCESS_ONCE(root->fs_info->last_trans_log_full_commit) ==
  2317. trans->transid) {
  2318. blk_finish_plug(&plug);
  2319. btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
  2320. btrfs_free_logged_extents(log, log_transid);
  2321. mutex_unlock(&log_root_tree->log_mutex);
  2322. ret = -EAGAIN;
  2323. goto out_wake_log_root;
  2324. }
  2325. ret = btrfs_write_marked_extents(log_root_tree,
  2326. &log_root_tree->dirty_log_pages,
  2327. EXTENT_DIRTY | EXTENT_NEW);
  2328. blk_finish_plug(&plug);
  2329. if (ret) {
  2330. btrfs_abort_transaction(trans, root, ret);
  2331. btrfs_free_logged_extents(log, log_transid);
  2332. mutex_unlock(&log_root_tree->log_mutex);
  2333. goto out_wake_log_root;
  2334. }
  2335. btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
  2336. btrfs_wait_marked_extents(log_root_tree,
  2337. &log_root_tree->dirty_log_pages,
  2338. EXTENT_NEW | EXTENT_DIRTY);
  2339. btrfs_wait_logged_extents(log, log_transid);
  2340. btrfs_set_super_log_root(root->fs_info->super_for_commit,
  2341. log_root_tree->node->start);
  2342. btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
  2343. btrfs_header_level(log_root_tree->node));
  2344. log_root_tree->log_transid++;
  2345. mutex_unlock(&log_root_tree->log_mutex);
  2346. /*
  2347. * nobody else is going to jump in and write the the ctree
  2348. * super here because the log_commit atomic below is protecting
  2349. * us. We must be called with a transaction handle pinning
  2350. * the running transaction open, so a full commit can't hop
  2351. * in and cause problems either.
  2352. */
  2353. ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
  2354. if (ret) {
  2355. btrfs_abort_transaction(trans, root, ret);
  2356. goto out_wake_log_root;
  2357. }
  2358. mutex_lock(&root->log_mutex);
  2359. if (root->last_log_commit < log_transid)
  2360. root->last_log_commit = log_transid;
  2361. mutex_unlock(&root->log_mutex);
  2362. out_wake_log_root:
  2363. /*
  2364. * We needn't get log_mutex here because we are sure all
  2365. * the other tasks are blocked.
  2366. */
  2367. btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
  2368. mutex_lock(&log_root_tree->log_mutex);
  2369. log_root_tree->log_transid_committed++;
  2370. atomic_set(&log_root_tree->log_commit[index2], 0);
  2371. mutex_unlock(&log_root_tree->log_mutex);
  2372. if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
  2373. wake_up(&log_root_tree->log_commit_wait[index2]);
  2374. out:
  2375. /* See above. */
  2376. btrfs_remove_all_log_ctxs(root, index1, ret);
  2377. mutex_lock(&root->log_mutex);
  2378. root->log_transid_committed++;
  2379. atomic_set(&root->log_commit[index1], 0);
  2380. mutex_unlock(&root->log_mutex);
  2381. if (waitqueue_active(&root->log_commit_wait[index1]))
  2382. wake_up(&root->log_commit_wait[index1]);
  2383. return ret;
  2384. }
  2385. static void free_log_tree(struct btrfs_trans_handle *trans,
  2386. struct btrfs_root *log)
  2387. {
  2388. int ret;
  2389. u64 start;
  2390. u64 end;
  2391. struct walk_control wc = {
  2392. .free = 1,
  2393. .process_func = process_one_buffer
  2394. };
  2395. ret = walk_log_tree(trans, log, &wc);
  2396. /* I don't think this can happen but just in case */
  2397. if (ret)
  2398. btrfs_abort_transaction(trans, log, ret);
  2399. while (1) {
  2400. ret = find_first_extent_bit(&log->dirty_log_pages,
  2401. 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
  2402. NULL);
  2403. if (ret)
  2404. break;
  2405. clear_extent_bits(&log->dirty_log_pages, start, end,
  2406. EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
  2407. }
  2408. /*
  2409. * We may have short-circuited the log tree with the full commit logic
  2410. * and left ordered extents on our list, so clear these out to keep us
  2411. * from leaking inodes and memory.
  2412. */
  2413. btrfs_free_logged_extents(log, 0);
  2414. btrfs_free_logged_extents(log, 1);
  2415. free_extent_buffer(log->node);
  2416. kfree(log);
  2417. }
  2418. /*
  2419. * free all the extents used by the tree log. This should be called
  2420. * at commit time of the full transaction
  2421. */
  2422. int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
  2423. {
  2424. if (root->log_root) {
  2425. free_log_tree(trans, root->log_root);
  2426. root->log_root = NULL;
  2427. }
  2428. return 0;
  2429. }
  2430. int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
  2431. struct btrfs_fs_info *fs_info)
  2432. {
  2433. if (fs_info->log_root_tree) {
  2434. free_log_tree(trans, fs_info->log_root_tree);
  2435. fs_info->log_root_tree = NULL;
  2436. }
  2437. return 0;
  2438. }
  2439. /*
  2440. * If both a file and directory are logged, and unlinks or renames are
  2441. * mixed in, we have a few interesting corners:
  2442. *
  2443. * create file X in dir Y
  2444. * link file X to X.link in dir Y
  2445. * fsync file X
  2446. * unlink file X but leave X.link
  2447. * fsync dir Y
  2448. *
  2449. * After a crash we would expect only X.link to exist. But file X
  2450. * didn't get fsync'd again so the log has back refs for X and X.link.
  2451. *
  2452. * We solve this by removing directory entries and inode backrefs from the
  2453. * log when a file that was logged in the current transaction is
  2454. * unlinked. Any later fsync will include the updated log entries, and
  2455. * we'll be able to reconstruct the proper directory items from backrefs.
  2456. *
  2457. * This optimizations allows us to avoid relogging the entire inode
  2458. * or the entire directory.
  2459. */
  2460. int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
  2461. struct btrfs_root *root,
  2462. const char *name, int name_len,
  2463. struct inode *dir, u64 index)
  2464. {
  2465. struct btrfs_root *log;
  2466. struct btrfs_dir_item *di;
  2467. struct btrfs_path *path;
  2468. int ret;
  2469. int err = 0;
  2470. int bytes_del = 0;
  2471. u64 dir_ino = btrfs_ino(dir);
  2472. if (BTRFS_I(dir)->logged_trans < trans->transid)
  2473. return 0;
  2474. ret = join_running_log_trans(root);
  2475. if (ret)
  2476. return 0;
  2477. mutex_lock(&BTRFS_I(dir)->log_mutex);
  2478. log = root->log_root;
  2479. path = btrfs_alloc_path();
  2480. if (!path) {
  2481. err = -ENOMEM;
  2482. goto out_unlock;
  2483. }
  2484. di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
  2485. name, name_len, -1);
  2486. if (IS_ERR(di)) {
  2487. err = PTR_ERR(di);
  2488. goto fail;
  2489. }
  2490. if (di) {
  2491. ret = btrfs_delete_one_dir_name(trans, log, path, di);
  2492. bytes_del += name_len;
  2493. if (ret) {
  2494. err = ret;
  2495. goto fail;
  2496. }
  2497. }
  2498. btrfs_release_path(path);
  2499. di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
  2500. index, name, name_len, -1);
  2501. if (IS_ERR(di)) {
  2502. err = PTR_ERR(di);
  2503. goto fail;
  2504. }
  2505. if (di) {
  2506. ret = btrfs_delete_one_dir_name(trans, log, path, di);
  2507. bytes_del += name_len;
  2508. if (ret) {
  2509. err = ret;
  2510. goto fail;
  2511. }
  2512. }
  2513. /* update the directory size in the log to reflect the names
  2514. * we have removed
  2515. */
  2516. if (bytes_del) {
  2517. struct btrfs_key key;
  2518. key.objectid = dir_ino;
  2519. key.offset = 0;
  2520. key.type = BTRFS_INODE_ITEM_KEY;
  2521. btrfs_release_path(path);
  2522. ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
  2523. if (ret < 0) {
  2524. err = ret;
  2525. goto fail;
  2526. }
  2527. if (ret == 0) {
  2528. struct btrfs_inode_item *item;
  2529. u64 i_size;
  2530. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2531. struct btrfs_inode_item);
  2532. i_size = btrfs_inode_size(path->nodes[0], item);
  2533. if (i_size > bytes_del)
  2534. i_size -= bytes_del;
  2535. else
  2536. i_size = 0;
  2537. btrfs_set_inode_size(path->nodes[0], item, i_size);
  2538. btrfs_mark_buffer_dirty(path->nodes[0]);
  2539. } else
  2540. ret = 0;
  2541. btrfs_release_path(path);
  2542. }
  2543. fail:
  2544. btrfs_free_path(path);
  2545. out_unlock:
  2546. mutex_unlock(&BTRFS_I(dir)->log_mutex);
  2547. if (ret == -ENOSPC) {
  2548. root->fs_info->last_trans_log_full_commit = trans->transid;
  2549. ret = 0;
  2550. } else if (ret < 0)
  2551. btrfs_abort_transaction(trans, root, ret);
  2552. btrfs_end_log_trans(root);
  2553. return err;
  2554. }
  2555. /* see comments for btrfs_del_dir_entries_in_log */
  2556. int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
  2557. struct btrfs_root *root,
  2558. const char *name, int name_len,
  2559. struct inode *inode, u64 dirid)
  2560. {
  2561. struct btrfs_root *log;
  2562. u64 index;
  2563. int ret;
  2564. if (BTRFS_I(inode)->logged_trans < trans->transid)
  2565. return 0;
  2566. ret = join_running_log_trans(root);
  2567. if (ret)
  2568. return 0;
  2569. log = root->log_root;
  2570. mutex_lock(&BTRFS_I(inode)->log_mutex);
  2571. ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
  2572. dirid, &index);
  2573. mutex_unlock(&BTRFS_I(inode)->log_mutex);
  2574. if (ret == -ENOSPC) {
  2575. root->fs_info->last_trans_log_full_commit = trans->transid;
  2576. ret = 0;
  2577. } else if (ret < 0 && ret != -ENOENT)
  2578. btrfs_abort_transaction(trans, root, ret);
  2579. btrfs_end_log_trans(root);
  2580. return ret;
  2581. }
  2582. /*
  2583. * creates a range item in the log for 'dirid'. first_offset and
  2584. * last_offset tell us which parts of the key space the log should
  2585. * be considered authoritative for.
  2586. */
  2587. static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
  2588. struct btrfs_root *log,
  2589. struct btrfs_path *path,
  2590. int key_type, u64 dirid,
  2591. u64 first_offset, u64 last_offset)
  2592. {
  2593. int ret;
  2594. struct btrfs_key key;
  2595. struct btrfs_dir_log_item *item;
  2596. key.objectid = dirid;
  2597. key.offset = first_offset;
  2598. if (key_type == BTRFS_DIR_ITEM_KEY)
  2599. key.type = BTRFS_DIR_LOG_ITEM_KEY;
  2600. else
  2601. key.type = BTRFS_DIR_LOG_INDEX_KEY;
  2602. ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
  2603. if (ret)
  2604. return ret;
  2605. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2606. struct btrfs_dir_log_item);
  2607. btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
  2608. btrfs_mark_buffer_dirty(path->nodes[0]);
  2609. btrfs_release_path(path);
  2610. return 0;
  2611. }
  2612. /*
  2613. * log all the items included in the current transaction for a given
  2614. * directory. This also creates the range items in the log tree required
  2615. * to replay anything deleted before the fsync
  2616. */
  2617. static noinline int log_dir_items(struct btrfs_trans_handle *trans,
  2618. struct btrfs_root *root, struct inode *inode,
  2619. struct btrfs_path *path,
  2620. struct btrfs_path *dst_path, int key_type,
  2621. u64 min_offset, u64 *last_offset_ret)
  2622. {
  2623. struct btrfs_key min_key;
  2624. struct btrfs_root *log = root->log_root;
  2625. struct extent_buffer *src;
  2626. int err = 0;
  2627. int ret;
  2628. int i;
  2629. int nritems;
  2630. u64 first_offset = min_offset;
  2631. u64 last_offset = (u64)-1;
  2632. u64 ino = btrfs_ino(inode);
  2633. log = root->log_root;
  2634. min_key.objectid = ino;
  2635. min_key.type = key_type;
  2636. min_key.offset = min_offset;
  2637. path->keep_locks = 1;
  2638. ret = btrfs_search_forward(root, &min_key, path, trans->transid);
  2639. /*
  2640. * we didn't find anything from this transaction, see if there
  2641. * is anything at all
  2642. */
  2643. if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
  2644. min_key.objectid = ino;
  2645. min_key.type = key_type;
  2646. min_key.offset = (u64)-1;
  2647. btrfs_release_path(path);
  2648. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  2649. if (ret < 0) {
  2650. btrfs_release_path(path);
  2651. return ret;
  2652. }
  2653. ret = btrfs_previous_item(root, path, ino, key_type);
  2654. /* if ret == 0 there are items for this type,
  2655. * create a range to tell us the last key of this type.
  2656. * otherwise, there are no items in this directory after
  2657. * *min_offset, and we create a range to indicate that.
  2658. */
  2659. if (ret == 0) {
  2660. struct btrfs_key tmp;
  2661. btrfs_item_key_to_cpu(path->nodes[0], &tmp,
  2662. path->slots[0]);
  2663. if (key_type == tmp.type)
  2664. first_offset = max(min_offset, tmp.offset) + 1;
  2665. }
  2666. goto done;
  2667. }
  2668. /* go backward to find any previous key */
  2669. ret = btrfs_previous_item(root, path, ino, key_type);
  2670. if (ret == 0) {
  2671. struct btrfs_key tmp;
  2672. btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
  2673. if (key_type == tmp.type) {
  2674. first_offset = tmp.offset;
  2675. ret = overwrite_item(trans, log, dst_path,
  2676. path->nodes[0], path->slots[0],
  2677. &tmp);
  2678. if (ret) {
  2679. err = ret;
  2680. goto done;
  2681. }
  2682. }
  2683. }
  2684. btrfs_release_path(path);
  2685. /* find the first key from this transaction again */
  2686. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  2687. if (WARN_ON(ret != 0))
  2688. goto done;
  2689. /*
  2690. * we have a block from this transaction, log every item in it
  2691. * from our directory
  2692. */
  2693. while (1) {
  2694. struct btrfs_key tmp;
  2695. src = path->nodes[0];
  2696. nritems = btrfs_header_nritems(src);
  2697. for (i = path->slots[0]; i < nritems; i++) {
  2698. btrfs_item_key_to_cpu(src, &min_key, i);
  2699. if (min_key.objectid != ino || min_key.type != key_type)
  2700. goto done;
  2701. ret = overwrite_item(trans, log, dst_path, src, i,
  2702. &min_key);
  2703. if (ret) {
  2704. err = ret;
  2705. goto done;
  2706. }
  2707. }
  2708. path->slots[0] = nritems;
  2709. /*
  2710. * look ahead to the next item and see if it is also
  2711. * from this directory and from this transaction
  2712. */
  2713. ret = btrfs_next_leaf(root, path);
  2714. if (ret == 1) {
  2715. last_offset = (u64)-1;
  2716. goto done;
  2717. }
  2718. btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
  2719. if (tmp.objectid != ino || tmp.type != key_type) {
  2720. last_offset = (u64)-1;
  2721. goto done;
  2722. }
  2723. if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
  2724. ret = overwrite_item(trans, log, dst_path,
  2725. path->nodes[0], path->slots[0],
  2726. &tmp);
  2727. if (ret)
  2728. err = ret;
  2729. else
  2730. last_offset = tmp.offset;
  2731. goto done;
  2732. }
  2733. }
  2734. done:
  2735. btrfs_release_path(path);
  2736. btrfs_release_path(dst_path);
  2737. if (err == 0) {
  2738. *last_offset_ret = last_offset;
  2739. /*
  2740. * insert the log range keys to indicate where the log
  2741. * is valid
  2742. */
  2743. ret = insert_dir_log_key(trans, log, path, key_type,
  2744. ino, first_offset, last_offset);
  2745. if (ret)
  2746. err = ret;
  2747. }
  2748. return err;
  2749. }
  2750. /*
  2751. * logging directories is very similar to logging inodes, We find all the items
  2752. * from the current transaction and write them to the log.
  2753. *
  2754. * The recovery code scans the directory in the subvolume, and if it finds a
  2755. * key in the range logged that is not present in the log tree, then it means
  2756. * that dir entry was unlinked during the transaction.
  2757. *
  2758. * In order for that scan to work, we must include one key smaller than
  2759. * the smallest logged by this transaction and one key larger than the largest
  2760. * key logged by this transaction.
  2761. */
  2762. static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
  2763. struct btrfs_root *root, struct inode *inode,
  2764. struct btrfs_path *path,
  2765. struct btrfs_path *dst_path)
  2766. {
  2767. u64 min_key;
  2768. u64 max_key;
  2769. int ret;
  2770. int key_type = BTRFS_DIR_ITEM_KEY;
  2771. again:
  2772. min_key = 0;
  2773. max_key = 0;
  2774. while (1) {
  2775. ret = log_dir_items(trans, root, inode, path,
  2776. dst_path, key_type, min_key,
  2777. &max_key);
  2778. if (ret)
  2779. return ret;
  2780. if (max_key == (u64)-1)
  2781. break;
  2782. min_key = max_key + 1;
  2783. }
  2784. if (key_type == BTRFS_DIR_ITEM_KEY) {
  2785. key_type = BTRFS_DIR_INDEX_KEY;
  2786. goto again;
  2787. }
  2788. return 0;
  2789. }
  2790. /*
  2791. * a helper function to drop items from the log before we relog an
  2792. * inode. max_key_type indicates the highest item type to remove.
  2793. * This cannot be run for file data extents because it does not
  2794. * free the extents they point to.
  2795. */
  2796. static int drop_objectid_items(struct btrfs_trans_handle *trans,
  2797. struct btrfs_root *log,
  2798. struct btrfs_path *path,
  2799. u64 objectid, int max_key_type)
  2800. {
  2801. int ret;
  2802. struct btrfs_key key;
  2803. struct btrfs_key found_key;
  2804. int start_slot;
  2805. key.objectid = objectid;
  2806. key.type = max_key_type;
  2807. key.offset = (u64)-1;
  2808. while (1) {
  2809. ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
  2810. BUG_ON(ret == 0); /* Logic error */
  2811. if (ret < 0)
  2812. break;
  2813. if (path->slots[0] == 0)
  2814. break;
  2815. path->slots[0]--;
  2816. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  2817. path->slots[0]);
  2818. if (found_key.objectid != objectid)
  2819. break;
  2820. found_key.offset = 0;
  2821. found_key.type = 0;
  2822. ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
  2823. &start_slot);
  2824. ret = btrfs_del_items(trans, log, path, start_slot,
  2825. path->slots[0] - start_slot + 1);
  2826. /*
  2827. * If start slot isn't 0 then we don't need to re-search, we've
  2828. * found the last guy with the objectid in this tree.
  2829. */
  2830. if (ret || start_slot != 0)
  2831. break;
  2832. btrfs_release_path(path);
  2833. }
  2834. btrfs_release_path(path);
  2835. if (ret > 0)
  2836. ret = 0;
  2837. return ret;
  2838. }
  2839. static void fill_inode_item(struct btrfs_trans_handle *trans,
  2840. struct extent_buffer *leaf,
  2841. struct btrfs_inode_item *item,
  2842. struct inode *inode, int log_inode_only)
  2843. {
  2844. struct btrfs_map_token token;
  2845. btrfs_init_map_token(&token);
  2846. if (log_inode_only) {
  2847. /* set the generation to zero so the recover code
  2848. * can tell the difference between an logging
  2849. * just to say 'this inode exists' and a logging
  2850. * to say 'update this inode with these values'
  2851. */
  2852. btrfs_set_token_inode_generation(leaf, item, 0, &token);
  2853. btrfs_set_token_inode_size(leaf, item, 0, &token);
  2854. } else {
  2855. btrfs_set_token_inode_generation(leaf, item,
  2856. BTRFS_I(inode)->generation,
  2857. &token);
  2858. btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
  2859. }
  2860. btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
  2861. btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
  2862. btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
  2863. btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
  2864. btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
  2865. inode->i_atime.tv_sec, &token);
  2866. btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
  2867. inode->i_atime.tv_nsec, &token);
  2868. btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
  2869. inode->i_mtime.tv_sec, &token);
  2870. btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
  2871. inode->i_mtime.tv_nsec, &token);
  2872. btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
  2873. inode->i_ctime.tv_sec, &token);
  2874. btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
  2875. inode->i_ctime.tv_nsec, &token);
  2876. btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
  2877. &token);
  2878. btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
  2879. btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
  2880. btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
  2881. btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
  2882. btrfs_set_token_inode_block_group(leaf, item, 0, &token);
  2883. }
  2884. static int log_inode_item(struct btrfs_trans_handle *trans,
  2885. struct btrfs_root *log, struct btrfs_path *path,
  2886. struct inode *inode)
  2887. {
  2888. struct btrfs_inode_item *inode_item;
  2889. int ret;
  2890. ret = btrfs_insert_empty_item(trans, log, path,
  2891. &BTRFS_I(inode)->location,
  2892. sizeof(*inode_item));
  2893. if (ret && ret != -EEXIST)
  2894. return ret;
  2895. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2896. struct btrfs_inode_item);
  2897. fill_inode_item(trans, path->nodes[0], inode_item, inode, 0);
  2898. btrfs_release_path(path);
  2899. return 0;
  2900. }
  2901. static noinline int copy_items(struct btrfs_trans_handle *trans,
  2902. struct inode *inode,
  2903. struct btrfs_path *dst_path,
  2904. struct btrfs_path *src_path, u64 *last_extent,
  2905. int start_slot, int nr, int inode_only)
  2906. {
  2907. unsigned long src_offset;
  2908. unsigned long dst_offset;
  2909. struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
  2910. struct btrfs_file_extent_item *extent;
  2911. struct btrfs_inode_item *inode_item;
  2912. struct extent_buffer *src = src_path->nodes[0];
  2913. struct btrfs_key first_key, last_key, key;
  2914. int ret;
  2915. struct btrfs_key *ins_keys;
  2916. u32 *ins_sizes;
  2917. char *ins_data;
  2918. int i;
  2919. struct list_head ordered_sums;
  2920. int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  2921. bool has_extents = false;
  2922. bool need_find_last_extent = (*last_extent == 0);
  2923. bool done = false;
  2924. INIT_LIST_HEAD(&ordered_sums);
  2925. ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
  2926. nr * sizeof(u32), GFP_NOFS);
  2927. if (!ins_data)
  2928. return -ENOMEM;
  2929. first_key.objectid = (u64)-1;
  2930. ins_sizes = (u32 *)ins_data;
  2931. ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
  2932. for (i = 0; i < nr; i++) {
  2933. ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
  2934. btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
  2935. }
  2936. ret = btrfs_insert_empty_items(trans, log, dst_path,
  2937. ins_keys, ins_sizes, nr);
  2938. if (ret) {
  2939. kfree(ins_data);
  2940. return ret;
  2941. }
  2942. for (i = 0; i < nr; i++, dst_path->slots[0]++) {
  2943. dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
  2944. dst_path->slots[0]);
  2945. src_offset = btrfs_item_ptr_offset(src, start_slot + i);
  2946. if ((i == (nr - 1)))
  2947. last_key = ins_keys[i];
  2948. if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
  2949. inode_item = btrfs_item_ptr(dst_path->nodes[0],
  2950. dst_path->slots[0],
  2951. struct btrfs_inode_item);
  2952. fill_inode_item(trans, dst_path->nodes[0], inode_item,
  2953. inode, inode_only == LOG_INODE_EXISTS);
  2954. } else {
  2955. copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
  2956. src_offset, ins_sizes[i]);
  2957. }
  2958. /*
  2959. * We set need_find_last_extent here in case we know we were
  2960. * processing other items and then walk into the first extent in
  2961. * the inode. If we don't hit an extent then nothing changes,
  2962. * we'll do the last search the next time around.
  2963. */
  2964. if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
  2965. has_extents = true;
  2966. if (need_find_last_extent &&
  2967. first_key.objectid == (u64)-1)
  2968. first_key = ins_keys[i];
  2969. } else {
  2970. need_find_last_extent = false;
  2971. }
  2972. /* take a reference on file data extents so that truncates
  2973. * or deletes of this inode don't have to relog the inode
  2974. * again
  2975. */
  2976. if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY &&
  2977. !skip_csum) {
  2978. int found_type;
  2979. extent = btrfs_item_ptr(src, start_slot + i,
  2980. struct btrfs_file_extent_item);
  2981. if (btrfs_file_extent_generation(src, extent) < trans->transid)
  2982. continue;
  2983. found_type = btrfs_file_extent_type(src, extent);
  2984. if (found_type == BTRFS_FILE_EXTENT_REG) {
  2985. u64 ds, dl, cs, cl;
  2986. ds = btrfs_file_extent_disk_bytenr(src,
  2987. extent);
  2988. /* ds == 0 is a hole */
  2989. if (ds == 0)
  2990. continue;
  2991. dl = btrfs_file_extent_disk_num_bytes(src,
  2992. extent);
  2993. cs = btrfs_file_extent_offset(src, extent);
  2994. cl = btrfs_file_extent_num_bytes(src,
  2995. extent);
  2996. if (btrfs_file_extent_compression(src,
  2997. extent)) {
  2998. cs = 0;
  2999. cl = dl;
  3000. }
  3001. ret = btrfs_lookup_csums_range(
  3002. log->fs_info->csum_root,
  3003. ds + cs, ds + cs + cl - 1,
  3004. &ordered_sums, 0);
  3005. if (ret) {
  3006. btrfs_release_path(dst_path);
  3007. kfree(ins_data);
  3008. return ret;
  3009. }
  3010. }
  3011. }
  3012. }
  3013. btrfs_mark_buffer_dirty(dst_path->nodes[0]);
  3014. btrfs_release_path(dst_path);
  3015. kfree(ins_data);
  3016. /*
  3017. * we have to do this after the loop above to avoid changing the
  3018. * log tree while trying to change the log tree.
  3019. */
  3020. ret = 0;
  3021. while (!list_empty(&ordered_sums)) {
  3022. struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
  3023. struct btrfs_ordered_sum,
  3024. list);
  3025. if (!ret)
  3026. ret = btrfs_csum_file_blocks(trans, log, sums);
  3027. list_del(&sums->list);
  3028. kfree(sums);
  3029. }
  3030. if (!has_extents)
  3031. return ret;
  3032. /*
  3033. * Because we use btrfs_search_forward we could skip leaves that were
  3034. * not modified and then assume *last_extent is valid when it really
  3035. * isn't. So back up to the previous leaf and read the end of the last
  3036. * extent before we go and fill in holes.
  3037. */
  3038. if (need_find_last_extent) {
  3039. u64 len;
  3040. ret = btrfs_prev_leaf(BTRFS_I(inode)->root, src_path);
  3041. if (ret < 0)
  3042. return ret;
  3043. if (ret)
  3044. goto fill_holes;
  3045. if (src_path->slots[0])
  3046. src_path->slots[0]--;
  3047. src = src_path->nodes[0];
  3048. btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
  3049. if (key.objectid != btrfs_ino(inode) ||
  3050. key.type != BTRFS_EXTENT_DATA_KEY)
  3051. goto fill_holes;
  3052. extent = btrfs_item_ptr(src, src_path->slots[0],
  3053. struct btrfs_file_extent_item);
  3054. if (btrfs_file_extent_type(src, extent) ==
  3055. BTRFS_FILE_EXTENT_INLINE) {
  3056. len = btrfs_file_extent_inline_len(src,
  3057. src_path->slots[0],
  3058. extent);
  3059. *last_extent = ALIGN(key.offset + len,
  3060. log->sectorsize);
  3061. } else {
  3062. len = btrfs_file_extent_num_bytes(src, extent);
  3063. *last_extent = key.offset + len;
  3064. }
  3065. }
  3066. fill_holes:
  3067. /* So we did prev_leaf, now we need to move to the next leaf, but a few
  3068. * things could have happened
  3069. *
  3070. * 1) A merge could have happened, so we could currently be on a leaf
  3071. * that holds what we were copying in the first place.
  3072. * 2) A split could have happened, and now not all of the items we want
  3073. * are on the same leaf.
  3074. *
  3075. * So we need to adjust how we search for holes, we need to drop the
  3076. * path and re-search for the first extent key we found, and then walk
  3077. * forward until we hit the last one we copied.
  3078. */
  3079. if (need_find_last_extent) {
  3080. /* btrfs_prev_leaf could return 1 without releasing the path */
  3081. btrfs_release_path(src_path);
  3082. ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &first_key,
  3083. src_path, 0, 0);
  3084. if (ret < 0)
  3085. return ret;
  3086. ASSERT(ret == 0);
  3087. src = src_path->nodes[0];
  3088. i = src_path->slots[0];
  3089. } else {
  3090. i = start_slot;
  3091. }
  3092. /*
  3093. * Ok so here we need to go through and fill in any holes we may have
  3094. * to make sure that holes are punched for those areas in case they had
  3095. * extents previously.
  3096. */
  3097. while (!done) {
  3098. u64 offset, len;
  3099. u64 extent_end;
  3100. if (i >= btrfs_header_nritems(src_path->nodes[0])) {
  3101. ret = btrfs_next_leaf(BTRFS_I(inode)->root, src_path);
  3102. if (ret < 0)
  3103. return ret;
  3104. ASSERT(ret == 0);
  3105. src = src_path->nodes[0];
  3106. i = 0;
  3107. }
  3108. btrfs_item_key_to_cpu(src, &key, i);
  3109. if (!btrfs_comp_cpu_keys(&key, &last_key))
  3110. done = true;
  3111. if (key.objectid != btrfs_ino(inode) ||
  3112. key.type != BTRFS_EXTENT_DATA_KEY) {
  3113. i++;
  3114. continue;
  3115. }
  3116. extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
  3117. if (btrfs_file_extent_type(src, extent) ==
  3118. BTRFS_FILE_EXTENT_INLINE) {
  3119. len = btrfs_file_extent_inline_len(src, i, extent);
  3120. extent_end = ALIGN(key.offset + len, log->sectorsize);
  3121. } else {
  3122. len = btrfs_file_extent_num_bytes(src, extent);
  3123. extent_end = key.offset + len;
  3124. }
  3125. i++;
  3126. if (*last_extent == key.offset) {
  3127. *last_extent = extent_end;
  3128. continue;
  3129. }
  3130. offset = *last_extent;
  3131. len = key.offset - *last_extent;
  3132. ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
  3133. offset, 0, 0, len, 0, len, 0,
  3134. 0, 0);
  3135. if (ret)
  3136. break;
  3137. *last_extent = offset + len;
  3138. }
  3139. /*
  3140. * Need to let the callers know we dropped the path so they should
  3141. * re-search.
  3142. */
  3143. if (!ret && need_find_last_extent)
  3144. ret = 1;
  3145. return ret;
  3146. }
  3147. static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
  3148. {
  3149. struct extent_map *em1, *em2;
  3150. em1 = list_entry(a, struct extent_map, list);
  3151. em2 = list_entry(b, struct extent_map, list);
  3152. if (em1->start < em2->start)
  3153. return -1;
  3154. else if (em1->start > em2->start)
  3155. return 1;
  3156. return 0;
  3157. }
  3158. static int log_one_extent(struct btrfs_trans_handle *trans,
  3159. struct inode *inode, struct btrfs_root *root,
  3160. struct extent_map *em, struct btrfs_path *path,
  3161. struct list_head *logged_list)
  3162. {
  3163. struct btrfs_root *log = root->log_root;
  3164. struct btrfs_file_extent_item *fi;
  3165. struct extent_buffer *leaf;
  3166. struct btrfs_ordered_extent *ordered;
  3167. struct list_head ordered_sums;
  3168. struct btrfs_map_token token;
  3169. struct btrfs_key key;
  3170. u64 mod_start = em->mod_start;
  3171. u64 mod_len = em->mod_len;
  3172. u64 csum_offset;
  3173. u64 csum_len;
  3174. u64 extent_offset = em->start - em->orig_start;
  3175. u64 block_len;
  3176. int ret;
  3177. bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  3178. int extent_inserted = 0;
  3179. INIT_LIST_HEAD(&ordered_sums);
  3180. btrfs_init_map_token(&token);
  3181. ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
  3182. em->start + em->len, NULL, 0, 1,
  3183. sizeof(*fi), &extent_inserted);
  3184. if (ret)
  3185. return ret;
  3186. if (!extent_inserted) {
  3187. key.objectid = btrfs_ino(inode);
  3188. key.type = BTRFS_EXTENT_DATA_KEY;
  3189. key.offset = em->start;
  3190. ret = btrfs_insert_empty_item(trans, log, path, &key,
  3191. sizeof(*fi));
  3192. if (ret)
  3193. return ret;
  3194. }
  3195. leaf = path->nodes[0];
  3196. fi = btrfs_item_ptr(leaf, path->slots[0],
  3197. struct btrfs_file_extent_item);
  3198. btrfs_set_token_file_extent_generation(leaf, fi, em->generation,
  3199. &token);
  3200. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
  3201. skip_csum = true;
  3202. btrfs_set_token_file_extent_type(leaf, fi,
  3203. BTRFS_FILE_EXTENT_PREALLOC,
  3204. &token);
  3205. } else {
  3206. btrfs_set_token_file_extent_type(leaf, fi,
  3207. BTRFS_FILE_EXTENT_REG,
  3208. &token);
  3209. if (em->block_start == EXTENT_MAP_HOLE)
  3210. skip_csum = true;
  3211. }
  3212. block_len = max(em->block_len, em->orig_block_len);
  3213. if (em->compress_type != BTRFS_COMPRESS_NONE) {
  3214. btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
  3215. em->block_start,
  3216. &token);
  3217. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
  3218. &token);
  3219. } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
  3220. btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
  3221. em->block_start -
  3222. extent_offset, &token);
  3223. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
  3224. &token);
  3225. } else {
  3226. btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
  3227. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
  3228. &token);
  3229. }
  3230. btrfs_set_token_file_extent_offset(leaf, fi,
  3231. em->start - em->orig_start,
  3232. &token);
  3233. btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
  3234. btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
  3235. btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
  3236. &token);
  3237. btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
  3238. btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
  3239. btrfs_mark_buffer_dirty(leaf);
  3240. btrfs_release_path(path);
  3241. if (ret) {
  3242. return ret;
  3243. }
  3244. if (skip_csum)
  3245. return 0;
  3246. /*
  3247. * First check and see if our csums are on our outstanding ordered
  3248. * extents.
  3249. */
  3250. list_for_each_entry(ordered, logged_list, log_list) {
  3251. struct btrfs_ordered_sum *sum;
  3252. if (!mod_len)
  3253. break;
  3254. if (ordered->file_offset + ordered->len <= mod_start ||
  3255. mod_start + mod_len <= ordered->file_offset)
  3256. continue;
  3257. /*
  3258. * We are going to copy all the csums on this ordered extent, so
  3259. * go ahead and adjust mod_start and mod_len in case this
  3260. * ordered extent has already been logged.
  3261. */
  3262. if (ordered->file_offset > mod_start) {
  3263. if (ordered->file_offset + ordered->len >=
  3264. mod_start + mod_len)
  3265. mod_len = ordered->file_offset - mod_start;
  3266. /*
  3267. * If we have this case
  3268. *
  3269. * |--------- logged extent ---------|
  3270. * |----- ordered extent ----|
  3271. *
  3272. * Just don't mess with mod_start and mod_len, we'll
  3273. * just end up logging more csums than we need and it
  3274. * will be ok.
  3275. */
  3276. } else {
  3277. if (ordered->file_offset + ordered->len <
  3278. mod_start + mod_len) {
  3279. mod_len = (mod_start + mod_len) -
  3280. (ordered->file_offset + ordered->len);
  3281. mod_start = ordered->file_offset +
  3282. ordered->len;
  3283. } else {
  3284. mod_len = 0;
  3285. }
  3286. }
  3287. /*
  3288. * To keep us from looping for the above case of an ordered
  3289. * extent that falls inside of the logged extent.
  3290. */
  3291. if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
  3292. &ordered->flags))
  3293. continue;
  3294. if (ordered->csum_bytes_left) {
  3295. btrfs_start_ordered_extent(inode, ordered, 0);
  3296. wait_event(ordered->wait,
  3297. ordered->csum_bytes_left == 0);
  3298. }
  3299. list_for_each_entry(sum, &ordered->list, list) {
  3300. ret = btrfs_csum_file_blocks(trans, log, sum);
  3301. if (ret)
  3302. goto unlocked;
  3303. }
  3304. }
  3305. unlocked:
  3306. if (!mod_len || ret)
  3307. return ret;
  3308. if (em->compress_type) {
  3309. csum_offset = 0;
  3310. csum_len = block_len;
  3311. } else {
  3312. csum_offset = mod_start - em->start;
  3313. csum_len = mod_len;
  3314. }
  3315. /* block start is already adjusted for the file extent offset. */
  3316. ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
  3317. em->block_start + csum_offset,
  3318. em->block_start + csum_offset +
  3319. csum_len - 1, &ordered_sums, 0);
  3320. if (ret)
  3321. return ret;
  3322. while (!list_empty(&ordered_sums)) {
  3323. struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
  3324. struct btrfs_ordered_sum,
  3325. list);
  3326. if (!ret)
  3327. ret = btrfs_csum_file_blocks(trans, log, sums);
  3328. list_del(&sums->list);
  3329. kfree(sums);
  3330. }
  3331. return ret;
  3332. }
  3333. static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
  3334. struct btrfs_root *root,
  3335. struct inode *inode,
  3336. struct btrfs_path *path,
  3337. struct list_head *logged_list)
  3338. {
  3339. struct extent_map *em, *n;
  3340. struct list_head extents;
  3341. struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
  3342. u64 test_gen;
  3343. int ret = 0;
  3344. int num = 0;
  3345. INIT_LIST_HEAD(&extents);
  3346. write_lock(&tree->lock);
  3347. test_gen = root->fs_info->last_trans_committed;
  3348. list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
  3349. list_del_init(&em->list);
  3350. /*
  3351. * Just an arbitrary number, this can be really CPU intensive
  3352. * once we start getting a lot of extents, and really once we
  3353. * have a bunch of extents we just want to commit since it will
  3354. * be faster.
  3355. */
  3356. if (++num > 32768) {
  3357. list_del_init(&tree->modified_extents);
  3358. ret = -EFBIG;
  3359. goto process;
  3360. }
  3361. if (em->generation <= test_gen)
  3362. continue;
  3363. /* Need a ref to keep it from getting evicted from cache */
  3364. atomic_inc(&em->refs);
  3365. set_bit(EXTENT_FLAG_LOGGING, &em->flags);
  3366. list_add_tail(&em->list, &extents);
  3367. num++;
  3368. }
  3369. list_sort(NULL, &extents, extent_cmp);
  3370. process:
  3371. while (!list_empty(&extents)) {
  3372. em = list_entry(extents.next, struct extent_map, list);
  3373. list_del_init(&em->list);
  3374. /*
  3375. * If we had an error we just need to delete everybody from our
  3376. * private list.
  3377. */
  3378. if (ret) {
  3379. clear_em_logging(tree, em);
  3380. free_extent_map(em);
  3381. continue;
  3382. }
  3383. write_unlock(&tree->lock);
  3384. ret = log_one_extent(trans, inode, root, em, path, logged_list);
  3385. write_lock(&tree->lock);
  3386. clear_em_logging(tree, em);
  3387. free_extent_map(em);
  3388. }
  3389. WARN_ON(!list_empty(&extents));
  3390. write_unlock(&tree->lock);
  3391. btrfs_release_path(path);
  3392. return ret;
  3393. }
  3394. /* log a single inode in the tree log.
  3395. * At least one parent directory for this inode must exist in the tree
  3396. * or be logged already.
  3397. *
  3398. * Any items from this inode changed by the current transaction are copied
  3399. * to the log tree. An extra reference is taken on any extents in this
  3400. * file, allowing us to avoid a whole pile of corner cases around logging
  3401. * blocks that have been removed from the tree.
  3402. *
  3403. * See LOG_INODE_ALL and related defines for a description of what inode_only
  3404. * does.
  3405. *
  3406. * This handles both files and directories.
  3407. */
  3408. static int btrfs_log_inode(struct btrfs_trans_handle *trans,
  3409. struct btrfs_root *root, struct inode *inode,
  3410. int inode_only)
  3411. {
  3412. struct btrfs_path *path;
  3413. struct btrfs_path *dst_path;
  3414. struct btrfs_key min_key;
  3415. struct btrfs_key max_key;
  3416. struct btrfs_root *log = root->log_root;
  3417. struct extent_buffer *src = NULL;
  3418. LIST_HEAD(logged_list);
  3419. u64 last_extent = 0;
  3420. int err = 0;
  3421. int ret;
  3422. int nritems;
  3423. int ins_start_slot = 0;
  3424. int ins_nr;
  3425. bool fast_search = false;
  3426. u64 ino = btrfs_ino(inode);
  3427. path = btrfs_alloc_path();
  3428. if (!path)
  3429. return -ENOMEM;
  3430. dst_path = btrfs_alloc_path();
  3431. if (!dst_path) {
  3432. btrfs_free_path(path);
  3433. return -ENOMEM;
  3434. }
  3435. min_key.objectid = ino;
  3436. min_key.type = BTRFS_INODE_ITEM_KEY;
  3437. min_key.offset = 0;
  3438. max_key.objectid = ino;
  3439. /* today the code can only do partial logging of directories */
  3440. if (S_ISDIR(inode->i_mode) ||
  3441. (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  3442. &BTRFS_I(inode)->runtime_flags) &&
  3443. inode_only == LOG_INODE_EXISTS))
  3444. max_key.type = BTRFS_XATTR_ITEM_KEY;
  3445. else
  3446. max_key.type = (u8)-1;
  3447. max_key.offset = (u64)-1;
  3448. /* Only run delayed items if we are a dir or a new file */
  3449. if (S_ISDIR(inode->i_mode) ||
  3450. BTRFS_I(inode)->generation > root->fs_info->last_trans_committed) {
  3451. ret = btrfs_commit_inode_delayed_items(trans, inode);
  3452. if (ret) {
  3453. btrfs_free_path(path);
  3454. btrfs_free_path(dst_path);
  3455. return ret;
  3456. }
  3457. }
  3458. mutex_lock(&BTRFS_I(inode)->log_mutex);
  3459. btrfs_get_logged_extents(inode, &logged_list);
  3460. /*
  3461. * a brute force approach to making sure we get the most uptodate
  3462. * copies of everything.
  3463. */
  3464. if (S_ISDIR(inode->i_mode)) {
  3465. int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
  3466. if (inode_only == LOG_INODE_EXISTS)
  3467. max_key_type = BTRFS_XATTR_ITEM_KEY;
  3468. ret = drop_objectid_items(trans, log, path, ino, max_key_type);
  3469. } else {
  3470. if (test_and_clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  3471. &BTRFS_I(inode)->runtime_flags)) {
  3472. clear_bit(BTRFS_INODE_COPY_EVERYTHING,
  3473. &BTRFS_I(inode)->runtime_flags);
  3474. ret = btrfs_truncate_inode_items(trans, log,
  3475. inode, 0, 0);
  3476. } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
  3477. &BTRFS_I(inode)->runtime_flags) ||
  3478. inode_only == LOG_INODE_EXISTS) {
  3479. if (inode_only == LOG_INODE_ALL)
  3480. fast_search = true;
  3481. max_key.type = BTRFS_XATTR_ITEM_KEY;
  3482. ret = drop_objectid_items(trans, log, path, ino,
  3483. max_key.type);
  3484. } else {
  3485. if (inode_only == LOG_INODE_ALL)
  3486. fast_search = true;
  3487. ret = log_inode_item(trans, log, dst_path, inode);
  3488. if (ret) {
  3489. err = ret;
  3490. goto out_unlock;
  3491. }
  3492. goto log_extents;
  3493. }
  3494. }
  3495. if (ret) {
  3496. err = ret;
  3497. goto out_unlock;
  3498. }
  3499. path->keep_locks = 1;
  3500. while (1) {
  3501. ins_nr = 0;
  3502. ret = btrfs_search_forward(root, &min_key,
  3503. path, trans->transid);
  3504. if (ret != 0)
  3505. break;
  3506. again:
  3507. /* note, ins_nr might be > 0 here, cleanup outside the loop */
  3508. if (min_key.objectid != ino)
  3509. break;
  3510. if (min_key.type > max_key.type)
  3511. break;
  3512. src = path->nodes[0];
  3513. if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
  3514. ins_nr++;
  3515. goto next_slot;
  3516. } else if (!ins_nr) {
  3517. ins_start_slot = path->slots[0];
  3518. ins_nr = 1;
  3519. goto next_slot;
  3520. }
  3521. ret = copy_items(trans, inode, dst_path, path, &last_extent,
  3522. ins_start_slot, ins_nr, inode_only);
  3523. if (ret < 0) {
  3524. err = ret;
  3525. goto out_unlock;
  3526. } if (ret) {
  3527. ins_nr = 0;
  3528. btrfs_release_path(path);
  3529. continue;
  3530. }
  3531. ins_nr = 1;
  3532. ins_start_slot = path->slots[0];
  3533. next_slot:
  3534. nritems = btrfs_header_nritems(path->nodes[0]);
  3535. path->slots[0]++;
  3536. if (path->slots[0] < nritems) {
  3537. btrfs_item_key_to_cpu(path->nodes[0], &min_key,
  3538. path->slots[0]);
  3539. goto again;
  3540. }
  3541. if (ins_nr) {
  3542. ret = copy_items(trans, inode, dst_path, path,
  3543. &last_extent, ins_start_slot,
  3544. ins_nr, inode_only);
  3545. if (ret < 0) {
  3546. err = ret;
  3547. goto out_unlock;
  3548. }
  3549. ret = 0;
  3550. ins_nr = 0;
  3551. }
  3552. btrfs_release_path(path);
  3553. if (min_key.offset < (u64)-1) {
  3554. min_key.offset++;
  3555. } else if (min_key.type < max_key.type) {
  3556. min_key.type++;
  3557. min_key.offset = 0;
  3558. } else {
  3559. break;
  3560. }
  3561. }
  3562. if (ins_nr) {
  3563. ret = copy_items(trans, inode, dst_path, path, &last_extent,
  3564. ins_start_slot, ins_nr, inode_only);
  3565. if (ret < 0) {
  3566. err = ret;
  3567. goto out_unlock;
  3568. }
  3569. ret = 0;
  3570. ins_nr = 0;
  3571. }
  3572. log_extents:
  3573. btrfs_release_path(path);
  3574. btrfs_release_path(dst_path);
  3575. if (fast_search) {
  3576. ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
  3577. &logged_list);
  3578. if (ret) {
  3579. err = ret;
  3580. goto out_unlock;
  3581. }
  3582. } else if (inode_only == LOG_INODE_ALL) {
  3583. struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
  3584. struct extent_map *em, *n;
  3585. write_lock(&tree->lock);
  3586. list_for_each_entry_safe(em, n, &tree->modified_extents, list)
  3587. list_del_init(&em->list);
  3588. write_unlock(&tree->lock);
  3589. }
  3590. if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
  3591. ret = log_directory_changes(trans, root, inode, path, dst_path);
  3592. if (ret) {
  3593. err = ret;
  3594. goto out_unlock;
  3595. }
  3596. }
  3597. BTRFS_I(inode)->logged_trans = trans->transid;
  3598. BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
  3599. out_unlock:
  3600. if (unlikely(err))
  3601. btrfs_put_logged_extents(&logged_list);
  3602. else
  3603. btrfs_submit_logged_extents(&logged_list, log);
  3604. mutex_unlock(&BTRFS_I(inode)->log_mutex);
  3605. btrfs_free_path(path);
  3606. btrfs_free_path(dst_path);
  3607. return err;
  3608. }
  3609. /*
  3610. * follow the dentry parent pointers up the chain and see if any
  3611. * of the directories in it require a full commit before they can
  3612. * be logged. Returns zero if nothing special needs to be done or 1 if
  3613. * a full commit is required.
  3614. */
  3615. static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
  3616. struct inode *inode,
  3617. struct dentry *parent,
  3618. struct super_block *sb,
  3619. u64 last_committed)
  3620. {
  3621. int ret = 0;
  3622. struct btrfs_root *root;
  3623. struct dentry *old_parent = NULL;
  3624. struct inode *orig_inode = inode;
  3625. /*
  3626. * for regular files, if its inode is already on disk, we don't
  3627. * have to worry about the parents at all. This is because
  3628. * we can use the last_unlink_trans field to record renames
  3629. * and other fun in this file.
  3630. */
  3631. if (S_ISREG(inode->i_mode) &&
  3632. BTRFS_I(inode)->generation <= last_committed &&
  3633. BTRFS_I(inode)->last_unlink_trans <= last_committed)
  3634. goto out;
  3635. if (!S_ISDIR(inode->i_mode)) {
  3636. if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
  3637. goto out;
  3638. inode = parent->d_inode;
  3639. }
  3640. while (1) {
  3641. /*
  3642. * If we are logging a directory then we start with our inode,
  3643. * not our parents inode, so we need to skipp setting the
  3644. * logged_trans so that further down in the log code we don't
  3645. * think this inode has already been logged.
  3646. */
  3647. if (inode != orig_inode)
  3648. BTRFS_I(inode)->logged_trans = trans->transid;
  3649. smp_mb();
  3650. if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
  3651. root = BTRFS_I(inode)->root;
  3652. /*
  3653. * make sure any commits to the log are forced
  3654. * to be full commits
  3655. */
  3656. root->fs_info->last_trans_log_full_commit =
  3657. trans->transid;
  3658. ret = 1;
  3659. break;
  3660. }
  3661. if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
  3662. break;
  3663. if (IS_ROOT(parent))
  3664. break;
  3665. parent = dget_parent(parent);
  3666. dput(old_parent);
  3667. old_parent = parent;
  3668. inode = parent->d_inode;
  3669. }
  3670. dput(old_parent);
  3671. out:
  3672. return ret;
  3673. }
  3674. /*
  3675. * helper function around btrfs_log_inode to make sure newly created
  3676. * parent directories also end up in the log. A minimal inode and backref
  3677. * only logging is done of any parent directories that are older than
  3678. * the last committed transaction
  3679. */
  3680. static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
  3681. struct btrfs_root *root, struct inode *inode,
  3682. struct dentry *parent, int exists_only,
  3683. struct btrfs_log_ctx *ctx)
  3684. {
  3685. int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
  3686. struct super_block *sb;
  3687. struct dentry *old_parent = NULL;
  3688. int ret = 0;
  3689. u64 last_committed = root->fs_info->last_trans_committed;
  3690. sb = inode->i_sb;
  3691. if (btrfs_test_opt(root, NOTREELOG)) {
  3692. ret = 1;
  3693. goto end_no_trans;
  3694. }
  3695. if (root->fs_info->last_trans_log_full_commit >
  3696. root->fs_info->last_trans_committed) {
  3697. ret = 1;
  3698. goto end_no_trans;
  3699. }
  3700. if (root != BTRFS_I(inode)->root ||
  3701. btrfs_root_refs(&root->root_item) == 0) {
  3702. ret = 1;
  3703. goto end_no_trans;
  3704. }
  3705. ret = check_parent_dirs_for_sync(trans, inode, parent,
  3706. sb, last_committed);
  3707. if (ret)
  3708. goto end_no_trans;
  3709. if (btrfs_inode_in_log(inode, trans->transid)) {
  3710. ret = BTRFS_NO_LOG_SYNC;
  3711. goto end_no_trans;
  3712. }
  3713. ret = start_log_trans(trans, root, ctx);
  3714. if (ret)
  3715. goto end_no_trans;
  3716. ret = btrfs_log_inode(trans, root, inode, inode_only);
  3717. if (ret)
  3718. goto end_trans;
  3719. /*
  3720. * for regular files, if its inode is already on disk, we don't
  3721. * have to worry about the parents at all. This is because
  3722. * we can use the last_unlink_trans field to record renames
  3723. * and other fun in this file.
  3724. */
  3725. if (S_ISREG(inode->i_mode) &&
  3726. BTRFS_I(inode)->generation <= last_committed &&
  3727. BTRFS_I(inode)->last_unlink_trans <= last_committed) {
  3728. ret = 0;
  3729. goto end_trans;
  3730. }
  3731. inode_only = LOG_INODE_EXISTS;
  3732. while (1) {
  3733. if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
  3734. break;
  3735. inode = parent->d_inode;
  3736. if (root != BTRFS_I(inode)->root)
  3737. break;
  3738. if (BTRFS_I(inode)->generation >
  3739. root->fs_info->last_trans_committed) {
  3740. ret = btrfs_log_inode(trans, root, inode, inode_only);
  3741. if (ret)
  3742. goto end_trans;
  3743. }
  3744. if (IS_ROOT(parent))
  3745. break;
  3746. parent = dget_parent(parent);
  3747. dput(old_parent);
  3748. old_parent = parent;
  3749. }
  3750. ret = 0;
  3751. end_trans:
  3752. dput(old_parent);
  3753. if (ret < 0) {
  3754. root->fs_info->last_trans_log_full_commit = trans->transid;
  3755. ret = 1;
  3756. }
  3757. if (ret)
  3758. btrfs_remove_log_ctx(root, ctx);
  3759. btrfs_end_log_trans(root);
  3760. end_no_trans:
  3761. return ret;
  3762. }
  3763. /*
  3764. * it is not safe to log dentry if the chunk root has added new
  3765. * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
  3766. * If this returns 1, you must commit the transaction to safely get your
  3767. * data on disk.
  3768. */
  3769. int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
  3770. struct btrfs_root *root, struct dentry *dentry,
  3771. struct btrfs_log_ctx *ctx)
  3772. {
  3773. struct dentry *parent = dget_parent(dentry);
  3774. int ret;
  3775. ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent,
  3776. 0, ctx);
  3777. dput(parent);
  3778. return ret;
  3779. }
  3780. /*
  3781. * should be called during mount to recover any replay any log trees
  3782. * from the FS
  3783. */
  3784. int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
  3785. {
  3786. int ret;
  3787. struct btrfs_path *path;
  3788. struct btrfs_trans_handle *trans;
  3789. struct btrfs_key key;
  3790. struct btrfs_key found_key;
  3791. struct btrfs_key tmp_key;
  3792. struct btrfs_root *log;
  3793. struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
  3794. struct walk_control wc = {
  3795. .process_func = process_one_buffer,
  3796. .stage = 0,
  3797. };
  3798. path = btrfs_alloc_path();
  3799. if (!path)
  3800. return -ENOMEM;
  3801. fs_info->log_root_recovering = 1;
  3802. trans = btrfs_start_transaction(fs_info->tree_root, 0);
  3803. if (IS_ERR(trans)) {
  3804. ret = PTR_ERR(trans);
  3805. goto error;
  3806. }
  3807. wc.trans = trans;
  3808. wc.pin = 1;
  3809. ret = walk_log_tree(trans, log_root_tree, &wc);
  3810. if (ret) {
  3811. btrfs_error(fs_info, ret, "Failed to pin buffers while "
  3812. "recovering log root tree.");
  3813. goto error;
  3814. }
  3815. again:
  3816. key.objectid = BTRFS_TREE_LOG_OBJECTID;
  3817. key.offset = (u64)-1;
  3818. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  3819. while (1) {
  3820. ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
  3821. if (ret < 0) {
  3822. btrfs_error(fs_info, ret,
  3823. "Couldn't find tree log root.");
  3824. goto error;
  3825. }
  3826. if (ret > 0) {
  3827. if (path->slots[0] == 0)
  3828. break;
  3829. path->slots[0]--;
  3830. }
  3831. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  3832. path->slots[0]);
  3833. btrfs_release_path(path);
  3834. if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  3835. break;
  3836. log = btrfs_read_fs_root(log_root_tree, &found_key);
  3837. if (IS_ERR(log)) {
  3838. ret = PTR_ERR(log);
  3839. btrfs_error(fs_info, ret,
  3840. "Couldn't read tree log root.");
  3841. goto error;
  3842. }
  3843. tmp_key.objectid = found_key.offset;
  3844. tmp_key.type = BTRFS_ROOT_ITEM_KEY;
  3845. tmp_key.offset = (u64)-1;
  3846. wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
  3847. if (IS_ERR(wc.replay_dest)) {
  3848. ret = PTR_ERR(wc.replay_dest);
  3849. free_extent_buffer(log->node);
  3850. free_extent_buffer(log->commit_root);
  3851. kfree(log);
  3852. btrfs_error(fs_info, ret, "Couldn't read target root "
  3853. "for tree log recovery.");
  3854. goto error;
  3855. }
  3856. wc.replay_dest->log_root = log;
  3857. btrfs_record_root_in_trans(trans, wc.replay_dest);
  3858. ret = walk_log_tree(trans, log, &wc);
  3859. if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
  3860. ret = fixup_inode_link_counts(trans, wc.replay_dest,
  3861. path);
  3862. }
  3863. key.offset = found_key.offset - 1;
  3864. wc.replay_dest->log_root = NULL;
  3865. free_extent_buffer(log->node);
  3866. free_extent_buffer(log->commit_root);
  3867. kfree(log);
  3868. if (ret)
  3869. goto error;
  3870. if (found_key.offset == 0)
  3871. break;
  3872. }
  3873. btrfs_release_path(path);
  3874. /* step one is to pin it all, step two is to replay just inodes */
  3875. if (wc.pin) {
  3876. wc.pin = 0;
  3877. wc.process_func = replay_one_buffer;
  3878. wc.stage = LOG_WALK_REPLAY_INODES;
  3879. goto again;
  3880. }
  3881. /* step three is to replay everything */
  3882. if (wc.stage < LOG_WALK_REPLAY_ALL) {
  3883. wc.stage++;
  3884. goto again;
  3885. }
  3886. btrfs_free_path(path);
  3887. /* step 4: commit the transaction, which also unpins the blocks */
  3888. ret = btrfs_commit_transaction(trans, fs_info->tree_root);
  3889. if (ret)
  3890. return ret;
  3891. free_extent_buffer(log_root_tree->node);
  3892. log_root_tree->log_root = NULL;
  3893. fs_info->log_root_recovering = 0;
  3894. kfree(log_root_tree);
  3895. return 0;
  3896. error:
  3897. if (wc.trans)
  3898. btrfs_end_transaction(wc.trans, fs_info->tree_root);
  3899. btrfs_free_path(path);
  3900. return ret;
  3901. }
  3902. /*
  3903. * there are some corner cases where we want to force a full
  3904. * commit instead of allowing a directory to be logged.
  3905. *
  3906. * They revolve around files there were unlinked from the directory, and
  3907. * this function updates the parent directory so that a full commit is
  3908. * properly done if it is fsync'd later after the unlinks are done.
  3909. */
  3910. void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
  3911. struct inode *dir, struct inode *inode,
  3912. int for_rename)
  3913. {
  3914. /*
  3915. * when we're logging a file, if it hasn't been renamed
  3916. * or unlinked, and its inode is fully committed on disk,
  3917. * we don't have to worry about walking up the directory chain
  3918. * to log its parents.
  3919. *
  3920. * So, we use the last_unlink_trans field to put this transid
  3921. * into the file. When the file is logged we check it and
  3922. * don't log the parents if the file is fully on disk.
  3923. */
  3924. if (S_ISREG(inode->i_mode))
  3925. BTRFS_I(inode)->last_unlink_trans = trans->transid;
  3926. /*
  3927. * if this directory was already logged any new
  3928. * names for this file/dir will get recorded
  3929. */
  3930. smp_mb();
  3931. if (BTRFS_I(dir)->logged_trans == trans->transid)
  3932. return;
  3933. /*
  3934. * if the inode we're about to unlink was logged,
  3935. * the log will be properly updated for any new names
  3936. */
  3937. if (BTRFS_I(inode)->logged_trans == trans->transid)
  3938. return;
  3939. /*
  3940. * when renaming files across directories, if the directory
  3941. * there we're unlinking from gets fsync'd later on, there's
  3942. * no way to find the destination directory later and fsync it
  3943. * properly. So, we have to be conservative and force commits
  3944. * so the new name gets discovered.
  3945. */
  3946. if (for_rename)
  3947. goto record;
  3948. /* we can safely do the unlink without any special recording */
  3949. return;
  3950. record:
  3951. BTRFS_I(dir)->last_unlink_trans = trans->transid;
  3952. }
  3953. /*
  3954. * Call this after adding a new name for a file and it will properly
  3955. * update the log to reflect the new name.
  3956. *
  3957. * It will return zero if all goes well, and it will return 1 if a
  3958. * full transaction commit is required.
  3959. */
  3960. int btrfs_log_new_name(struct btrfs_trans_handle *trans,
  3961. struct inode *inode, struct inode *old_dir,
  3962. struct dentry *parent)
  3963. {
  3964. struct btrfs_root * root = BTRFS_I(inode)->root;
  3965. /*
  3966. * this will force the logging code to walk the dentry chain
  3967. * up for the file
  3968. */
  3969. if (S_ISREG(inode->i_mode))
  3970. BTRFS_I(inode)->last_unlink_trans = trans->transid;
  3971. /*
  3972. * if this inode hasn't been logged and directory we're renaming it
  3973. * from hasn't been logged, we don't need to log it
  3974. */
  3975. if (BTRFS_I(inode)->logged_trans <=
  3976. root->fs_info->last_trans_committed &&
  3977. (!old_dir || BTRFS_I(old_dir)->logged_trans <=
  3978. root->fs_info->last_trans_committed))
  3979. return 0;
  3980. return btrfs_log_inode_parent(trans, root, inode, parent, 1, NULL);
  3981. }