tree-log.c 100 KB

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