tree-log.c 83 KB

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