tree-log.c 120 KB

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