volumes.c 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/sched.h>
  19. #include <linux/bio.h>
  20. #include <linux/slab.h>
  21. #include <linux/buffer_head.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/random.h>
  24. #include <linux/iocontext.h>
  25. #include <linux/capability.h>
  26. #include <asm/div64.h>
  27. #include "compat.h"
  28. #include "ctree.h"
  29. #include "extent_map.h"
  30. #include "disk-io.h"
  31. #include "transaction.h"
  32. #include "print-tree.h"
  33. #include "volumes.h"
  34. #include "async-thread.h"
  35. static int init_first_rw_device(struct btrfs_trans_handle *trans,
  36. struct btrfs_root *root,
  37. struct btrfs_device *device);
  38. static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
  39. static DEFINE_MUTEX(uuid_mutex);
  40. static LIST_HEAD(fs_uuids);
  41. static void lock_chunks(struct btrfs_root *root)
  42. {
  43. mutex_lock(&root->fs_info->chunk_mutex);
  44. }
  45. static void unlock_chunks(struct btrfs_root *root)
  46. {
  47. mutex_unlock(&root->fs_info->chunk_mutex);
  48. }
  49. static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
  50. {
  51. struct btrfs_device *device;
  52. WARN_ON(fs_devices->opened);
  53. while (!list_empty(&fs_devices->devices)) {
  54. device = list_entry(fs_devices->devices.next,
  55. struct btrfs_device, dev_list);
  56. list_del(&device->dev_list);
  57. kfree(device->name);
  58. kfree(device);
  59. }
  60. kfree(fs_devices);
  61. }
  62. int btrfs_cleanup_fs_uuids(void)
  63. {
  64. struct btrfs_fs_devices *fs_devices;
  65. while (!list_empty(&fs_uuids)) {
  66. fs_devices = list_entry(fs_uuids.next,
  67. struct btrfs_fs_devices, list);
  68. list_del(&fs_devices->list);
  69. free_fs_devices(fs_devices);
  70. }
  71. return 0;
  72. }
  73. static noinline struct btrfs_device *__find_device(struct list_head *head,
  74. u64 devid, u8 *uuid)
  75. {
  76. struct btrfs_device *dev;
  77. list_for_each_entry(dev, head, dev_list) {
  78. if (dev->devid == devid &&
  79. (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
  80. return dev;
  81. }
  82. }
  83. return NULL;
  84. }
  85. static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
  86. {
  87. struct btrfs_fs_devices *fs_devices;
  88. list_for_each_entry(fs_devices, &fs_uuids, list) {
  89. if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
  90. return fs_devices;
  91. }
  92. return NULL;
  93. }
  94. static void requeue_list(struct btrfs_pending_bios *pending_bios,
  95. struct bio *head, struct bio *tail)
  96. {
  97. struct bio *old_head;
  98. old_head = pending_bios->head;
  99. pending_bios->head = head;
  100. if (pending_bios->tail)
  101. tail->bi_next = old_head;
  102. else
  103. pending_bios->tail = tail;
  104. }
  105. /*
  106. * we try to collect pending bios for a device so we don't get a large
  107. * number of procs sending bios down to the same device. This greatly
  108. * improves the schedulers ability to collect and merge the bios.
  109. *
  110. * But, it also turns into a long list of bios to process and that is sure
  111. * to eventually make the worker thread block. The solution here is to
  112. * make some progress and then put this work struct back at the end of
  113. * the list if the block device is congested. This way, multiple devices
  114. * can make progress from a single worker thread.
  115. */
  116. static noinline int run_scheduled_bios(struct btrfs_device *device)
  117. {
  118. struct bio *pending;
  119. struct backing_dev_info *bdi;
  120. struct btrfs_fs_info *fs_info;
  121. struct btrfs_pending_bios *pending_bios;
  122. struct bio *tail;
  123. struct bio *cur;
  124. int again = 0;
  125. unsigned long num_run;
  126. unsigned long batch_run = 0;
  127. unsigned long limit;
  128. unsigned long last_waited = 0;
  129. int force_reg = 0;
  130. int sync_pending = 0;
  131. struct blk_plug plug;
  132. /*
  133. * this function runs all the bios we've collected for
  134. * a particular device. We don't want to wander off to
  135. * another device without first sending all of these down.
  136. * So, setup a plug here and finish it off before we return
  137. */
  138. blk_start_plug(&plug);
  139. bdi = blk_get_backing_dev_info(device->bdev);
  140. fs_info = device->dev_root->fs_info;
  141. limit = btrfs_async_submit_limit(fs_info);
  142. limit = limit * 2 / 3;
  143. loop:
  144. spin_lock(&device->io_lock);
  145. loop_lock:
  146. num_run = 0;
  147. /* take all the bios off the list at once and process them
  148. * later on (without the lock held). But, remember the
  149. * tail and other pointers so the bios can be properly reinserted
  150. * into the list if we hit congestion
  151. */
  152. if (!force_reg && device->pending_sync_bios.head) {
  153. pending_bios = &device->pending_sync_bios;
  154. force_reg = 1;
  155. } else {
  156. pending_bios = &device->pending_bios;
  157. force_reg = 0;
  158. }
  159. pending = pending_bios->head;
  160. tail = pending_bios->tail;
  161. WARN_ON(pending && !tail);
  162. /*
  163. * if pending was null this time around, no bios need processing
  164. * at all and we can stop. Otherwise it'll loop back up again
  165. * and do an additional check so no bios are missed.
  166. *
  167. * device->running_pending is used to synchronize with the
  168. * schedule_bio code.
  169. */
  170. if (device->pending_sync_bios.head == NULL &&
  171. device->pending_bios.head == NULL) {
  172. again = 0;
  173. device->running_pending = 0;
  174. } else {
  175. again = 1;
  176. device->running_pending = 1;
  177. }
  178. pending_bios->head = NULL;
  179. pending_bios->tail = NULL;
  180. spin_unlock(&device->io_lock);
  181. while (pending) {
  182. rmb();
  183. /* we want to work on both lists, but do more bios on the
  184. * sync list than the regular list
  185. */
  186. if ((num_run > 32 &&
  187. pending_bios != &device->pending_sync_bios &&
  188. device->pending_sync_bios.head) ||
  189. (num_run > 64 && pending_bios == &device->pending_sync_bios &&
  190. device->pending_bios.head)) {
  191. spin_lock(&device->io_lock);
  192. requeue_list(pending_bios, pending, tail);
  193. goto loop_lock;
  194. }
  195. cur = pending;
  196. pending = pending->bi_next;
  197. cur->bi_next = NULL;
  198. atomic_dec(&fs_info->nr_async_bios);
  199. if (atomic_read(&fs_info->nr_async_bios) < limit &&
  200. waitqueue_active(&fs_info->async_submit_wait))
  201. wake_up(&fs_info->async_submit_wait);
  202. BUG_ON(atomic_read(&cur->bi_cnt) == 0);
  203. /*
  204. * if we're doing the sync list, record that our
  205. * plug has some sync requests on it
  206. *
  207. * If we're doing the regular list and there are
  208. * sync requests sitting around, unplug before
  209. * we add more
  210. */
  211. if (pending_bios == &device->pending_sync_bios) {
  212. sync_pending = 1;
  213. } else if (sync_pending) {
  214. blk_finish_plug(&plug);
  215. blk_start_plug(&plug);
  216. sync_pending = 0;
  217. }
  218. submit_bio(cur->bi_rw, cur);
  219. num_run++;
  220. batch_run++;
  221. if (need_resched())
  222. cond_resched();
  223. /*
  224. * we made progress, there is more work to do and the bdi
  225. * is now congested. Back off and let other work structs
  226. * run instead
  227. */
  228. if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
  229. fs_info->fs_devices->open_devices > 1) {
  230. struct io_context *ioc;
  231. ioc = current->io_context;
  232. /*
  233. * the main goal here is that we don't want to
  234. * block if we're going to be able to submit
  235. * more requests without blocking.
  236. *
  237. * This code does two great things, it pokes into
  238. * the elevator code from a filesystem _and_
  239. * it makes assumptions about how batching works.
  240. */
  241. if (ioc && ioc->nr_batch_requests > 0 &&
  242. time_before(jiffies, ioc->last_waited + HZ/50UL) &&
  243. (last_waited == 0 ||
  244. ioc->last_waited == last_waited)) {
  245. /*
  246. * we want to go through our batch of
  247. * requests and stop. So, we copy out
  248. * the ioc->last_waited time and test
  249. * against it before looping
  250. */
  251. last_waited = ioc->last_waited;
  252. if (need_resched())
  253. cond_resched();
  254. continue;
  255. }
  256. spin_lock(&device->io_lock);
  257. requeue_list(pending_bios, pending, tail);
  258. device->running_pending = 1;
  259. spin_unlock(&device->io_lock);
  260. btrfs_requeue_work(&device->work);
  261. goto done;
  262. }
  263. }
  264. cond_resched();
  265. if (again)
  266. goto loop;
  267. spin_lock(&device->io_lock);
  268. if (device->pending_bios.head || device->pending_sync_bios.head)
  269. goto loop_lock;
  270. spin_unlock(&device->io_lock);
  271. done:
  272. blk_finish_plug(&plug);
  273. return 0;
  274. }
  275. static void pending_bios_fn(struct btrfs_work *work)
  276. {
  277. struct btrfs_device *device;
  278. device = container_of(work, struct btrfs_device, work);
  279. run_scheduled_bios(device);
  280. }
  281. static noinline int device_list_add(const char *path,
  282. struct btrfs_super_block *disk_super,
  283. u64 devid, struct btrfs_fs_devices **fs_devices_ret)
  284. {
  285. struct btrfs_device *device;
  286. struct btrfs_fs_devices *fs_devices;
  287. u64 found_transid = btrfs_super_generation(disk_super);
  288. char *name;
  289. fs_devices = find_fsid(disk_super->fsid);
  290. if (!fs_devices) {
  291. fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
  292. if (!fs_devices)
  293. return -ENOMEM;
  294. INIT_LIST_HEAD(&fs_devices->devices);
  295. INIT_LIST_HEAD(&fs_devices->alloc_list);
  296. list_add(&fs_devices->list, &fs_uuids);
  297. memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
  298. fs_devices->latest_devid = devid;
  299. fs_devices->latest_trans = found_transid;
  300. mutex_init(&fs_devices->device_list_mutex);
  301. device = NULL;
  302. } else {
  303. device = __find_device(&fs_devices->devices, devid,
  304. disk_super->dev_item.uuid);
  305. }
  306. if (!device) {
  307. if (fs_devices->opened)
  308. return -EBUSY;
  309. device = kzalloc(sizeof(*device), GFP_NOFS);
  310. if (!device) {
  311. /* we can safely leave the fs_devices entry around */
  312. return -ENOMEM;
  313. }
  314. device->devid = devid;
  315. device->work.func = pending_bios_fn;
  316. memcpy(device->uuid, disk_super->dev_item.uuid,
  317. BTRFS_UUID_SIZE);
  318. spin_lock_init(&device->io_lock);
  319. device->name = kstrdup(path, GFP_NOFS);
  320. if (!device->name) {
  321. kfree(device);
  322. return -ENOMEM;
  323. }
  324. INIT_LIST_HEAD(&device->dev_alloc_list);
  325. mutex_lock(&fs_devices->device_list_mutex);
  326. list_add_rcu(&device->dev_list, &fs_devices->devices);
  327. mutex_unlock(&fs_devices->device_list_mutex);
  328. device->fs_devices = fs_devices;
  329. fs_devices->num_devices++;
  330. } else if (!device->name || strcmp(device->name, path)) {
  331. name = kstrdup(path, GFP_NOFS);
  332. if (!name)
  333. return -ENOMEM;
  334. kfree(device->name);
  335. device->name = name;
  336. if (device->missing) {
  337. fs_devices->missing_devices--;
  338. device->missing = 0;
  339. }
  340. }
  341. if (found_transid > fs_devices->latest_trans) {
  342. fs_devices->latest_devid = devid;
  343. fs_devices->latest_trans = found_transid;
  344. }
  345. *fs_devices_ret = fs_devices;
  346. return 0;
  347. }
  348. static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
  349. {
  350. struct btrfs_fs_devices *fs_devices;
  351. struct btrfs_device *device;
  352. struct btrfs_device *orig_dev;
  353. fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
  354. if (!fs_devices)
  355. return ERR_PTR(-ENOMEM);
  356. INIT_LIST_HEAD(&fs_devices->devices);
  357. INIT_LIST_HEAD(&fs_devices->alloc_list);
  358. INIT_LIST_HEAD(&fs_devices->list);
  359. mutex_init(&fs_devices->device_list_mutex);
  360. fs_devices->latest_devid = orig->latest_devid;
  361. fs_devices->latest_trans = orig->latest_trans;
  362. memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
  363. /* We have held the volume lock, it is safe to get the devices. */
  364. list_for_each_entry(orig_dev, &orig->devices, dev_list) {
  365. device = kzalloc(sizeof(*device), GFP_NOFS);
  366. if (!device)
  367. goto error;
  368. device->name = kstrdup(orig_dev->name, GFP_NOFS);
  369. if (!device->name) {
  370. kfree(device);
  371. goto error;
  372. }
  373. device->devid = orig_dev->devid;
  374. device->work.func = pending_bios_fn;
  375. memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
  376. spin_lock_init(&device->io_lock);
  377. INIT_LIST_HEAD(&device->dev_list);
  378. INIT_LIST_HEAD(&device->dev_alloc_list);
  379. list_add(&device->dev_list, &fs_devices->devices);
  380. device->fs_devices = fs_devices;
  381. fs_devices->num_devices++;
  382. }
  383. return fs_devices;
  384. error:
  385. free_fs_devices(fs_devices);
  386. return ERR_PTR(-ENOMEM);
  387. }
  388. int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
  389. {
  390. struct btrfs_device *device, *next;
  391. mutex_lock(&uuid_mutex);
  392. again:
  393. /* This is the initialized path, it is safe to release the devices. */
  394. list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
  395. if (device->in_fs_metadata)
  396. continue;
  397. if (device->bdev) {
  398. blkdev_put(device->bdev, device->mode);
  399. device->bdev = NULL;
  400. fs_devices->open_devices--;
  401. }
  402. if (device->writeable) {
  403. list_del_init(&device->dev_alloc_list);
  404. device->writeable = 0;
  405. fs_devices->rw_devices--;
  406. }
  407. list_del_init(&device->dev_list);
  408. fs_devices->num_devices--;
  409. kfree(device->name);
  410. kfree(device);
  411. }
  412. if (fs_devices->seed) {
  413. fs_devices = fs_devices->seed;
  414. goto again;
  415. }
  416. mutex_unlock(&uuid_mutex);
  417. return 0;
  418. }
  419. static void __free_device(struct work_struct *work)
  420. {
  421. struct btrfs_device *device;
  422. device = container_of(work, struct btrfs_device, rcu_work);
  423. if (device->bdev)
  424. blkdev_put(device->bdev, device->mode);
  425. kfree(device->name);
  426. kfree(device);
  427. }
  428. static void free_device(struct rcu_head *head)
  429. {
  430. struct btrfs_device *device;
  431. device = container_of(head, struct btrfs_device, rcu);
  432. INIT_WORK(&device->rcu_work, __free_device);
  433. schedule_work(&device->rcu_work);
  434. }
  435. static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
  436. {
  437. struct btrfs_device *device;
  438. if (--fs_devices->opened > 0)
  439. return 0;
  440. mutex_lock(&fs_devices->device_list_mutex);
  441. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  442. struct btrfs_device *new_device;
  443. if (device->bdev)
  444. fs_devices->open_devices--;
  445. if (device->writeable) {
  446. list_del_init(&device->dev_alloc_list);
  447. fs_devices->rw_devices--;
  448. }
  449. if (device->can_discard)
  450. fs_devices->num_can_discard--;
  451. new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
  452. BUG_ON(!new_device);
  453. memcpy(new_device, device, sizeof(*new_device));
  454. new_device->name = kstrdup(device->name, GFP_NOFS);
  455. BUG_ON(device->name && !new_device->name);
  456. new_device->bdev = NULL;
  457. new_device->writeable = 0;
  458. new_device->in_fs_metadata = 0;
  459. new_device->can_discard = 0;
  460. list_replace_rcu(&device->dev_list, &new_device->dev_list);
  461. call_rcu(&device->rcu, free_device);
  462. }
  463. mutex_unlock(&fs_devices->device_list_mutex);
  464. WARN_ON(fs_devices->open_devices);
  465. WARN_ON(fs_devices->rw_devices);
  466. fs_devices->opened = 0;
  467. fs_devices->seeding = 0;
  468. return 0;
  469. }
  470. int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
  471. {
  472. struct btrfs_fs_devices *seed_devices = NULL;
  473. int ret;
  474. mutex_lock(&uuid_mutex);
  475. ret = __btrfs_close_devices(fs_devices);
  476. if (!fs_devices->opened) {
  477. seed_devices = fs_devices->seed;
  478. fs_devices->seed = NULL;
  479. }
  480. mutex_unlock(&uuid_mutex);
  481. while (seed_devices) {
  482. fs_devices = seed_devices;
  483. seed_devices = fs_devices->seed;
  484. __btrfs_close_devices(fs_devices);
  485. free_fs_devices(fs_devices);
  486. }
  487. return ret;
  488. }
  489. static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
  490. fmode_t flags, void *holder)
  491. {
  492. struct request_queue *q;
  493. struct block_device *bdev;
  494. struct list_head *head = &fs_devices->devices;
  495. struct btrfs_device *device;
  496. struct block_device *latest_bdev = NULL;
  497. struct buffer_head *bh;
  498. struct btrfs_super_block *disk_super;
  499. u64 latest_devid = 0;
  500. u64 latest_transid = 0;
  501. u64 devid;
  502. int seeding = 1;
  503. int ret = 0;
  504. flags |= FMODE_EXCL;
  505. list_for_each_entry(device, head, dev_list) {
  506. if (device->bdev)
  507. continue;
  508. if (!device->name)
  509. continue;
  510. bdev = blkdev_get_by_path(device->name, flags, holder);
  511. if (IS_ERR(bdev)) {
  512. printk(KERN_INFO "open %s failed\n", device->name);
  513. goto error;
  514. }
  515. set_blocksize(bdev, 4096);
  516. bh = btrfs_read_dev_super(bdev);
  517. if (!bh) {
  518. ret = -EINVAL;
  519. goto error_close;
  520. }
  521. disk_super = (struct btrfs_super_block *)bh->b_data;
  522. devid = btrfs_stack_device_id(&disk_super->dev_item);
  523. if (devid != device->devid)
  524. goto error_brelse;
  525. if (memcmp(device->uuid, disk_super->dev_item.uuid,
  526. BTRFS_UUID_SIZE))
  527. goto error_brelse;
  528. device->generation = btrfs_super_generation(disk_super);
  529. if (!latest_transid || device->generation > latest_transid) {
  530. latest_devid = devid;
  531. latest_transid = device->generation;
  532. latest_bdev = bdev;
  533. }
  534. if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
  535. device->writeable = 0;
  536. } else {
  537. device->writeable = !bdev_read_only(bdev);
  538. seeding = 0;
  539. }
  540. q = bdev_get_queue(bdev);
  541. if (blk_queue_discard(q)) {
  542. device->can_discard = 1;
  543. fs_devices->num_can_discard++;
  544. }
  545. device->bdev = bdev;
  546. device->in_fs_metadata = 0;
  547. device->mode = flags;
  548. if (!blk_queue_nonrot(bdev_get_queue(bdev)))
  549. fs_devices->rotating = 1;
  550. fs_devices->open_devices++;
  551. if (device->writeable) {
  552. fs_devices->rw_devices++;
  553. list_add(&device->dev_alloc_list,
  554. &fs_devices->alloc_list);
  555. }
  556. brelse(bh);
  557. continue;
  558. error_brelse:
  559. brelse(bh);
  560. error_close:
  561. blkdev_put(bdev, flags);
  562. error:
  563. continue;
  564. }
  565. if (fs_devices->open_devices == 0) {
  566. ret = -EIO;
  567. goto out;
  568. }
  569. fs_devices->seeding = seeding;
  570. fs_devices->opened = 1;
  571. fs_devices->latest_bdev = latest_bdev;
  572. fs_devices->latest_devid = latest_devid;
  573. fs_devices->latest_trans = latest_transid;
  574. fs_devices->total_rw_bytes = 0;
  575. out:
  576. return ret;
  577. }
  578. int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
  579. fmode_t flags, void *holder)
  580. {
  581. int ret;
  582. mutex_lock(&uuid_mutex);
  583. if (fs_devices->opened) {
  584. fs_devices->opened++;
  585. ret = 0;
  586. } else {
  587. ret = __btrfs_open_devices(fs_devices, flags, holder);
  588. }
  589. mutex_unlock(&uuid_mutex);
  590. return ret;
  591. }
  592. int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
  593. struct btrfs_fs_devices **fs_devices_ret)
  594. {
  595. struct btrfs_super_block *disk_super;
  596. struct block_device *bdev;
  597. struct buffer_head *bh;
  598. int ret;
  599. u64 devid;
  600. u64 transid;
  601. mutex_lock(&uuid_mutex);
  602. flags |= FMODE_EXCL;
  603. bdev = blkdev_get_by_path(path, flags, holder);
  604. if (IS_ERR(bdev)) {
  605. ret = PTR_ERR(bdev);
  606. goto error;
  607. }
  608. ret = set_blocksize(bdev, 4096);
  609. if (ret)
  610. goto error_close;
  611. bh = btrfs_read_dev_super(bdev);
  612. if (!bh) {
  613. ret = -EINVAL;
  614. goto error_close;
  615. }
  616. disk_super = (struct btrfs_super_block *)bh->b_data;
  617. devid = btrfs_stack_device_id(&disk_super->dev_item);
  618. transid = btrfs_super_generation(disk_super);
  619. if (disk_super->label[0])
  620. printk(KERN_INFO "device label %s ", disk_super->label);
  621. else
  622. printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
  623. printk(KERN_CONT "devid %llu transid %llu %s\n",
  624. (unsigned long long)devid, (unsigned long long)transid, path);
  625. ret = device_list_add(path, disk_super, devid, fs_devices_ret);
  626. brelse(bh);
  627. error_close:
  628. blkdev_put(bdev, flags);
  629. error:
  630. mutex_unlock(&uuid_mutex);
  631. return ret;
  632. }
  633. /* helper to account the used device space in the range */
  634. int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
  635. u64 end, u64 *length)
  636. {
  637. struct btrfs_key key;
  638. struct btrfs_root *root = device->dev_root;
  639. struct btrfs_dev_extent *dev_extent;
  640. struct btrfs_path *path;
  641. u64 extent_end;
  642. int ret;
  643. int slot;
  644. struct extent_buffer *l;
  645. *length = 0;
  646. if (start >= device->total_bytes)
  647. return 0;
  648. path = btrfs_alloc_path();
  649. if (!path)
  650. return -ENOMEM;
  651. path->reada = 2;
  652. key.objectid = device->devid;
  653. key.offset = start;
  654. key.type = BTRFS_DEV_EXTENT_KEY;
  655. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  656. if (ret < 0)
  657. goto out;
  658. if (ret > 0) {
  659. ret = btrfs_previous_item(root, path, key.objectid, key.type);
  660. if (ret < 0)
  661. goto out;
  662. }
  663. while (1) {
  664. l = path->nodes[0];
  665. slot = path->slots[0];
  666. if (slot >= btrfs_header_nritems(l)) {
  667. ret = btrfs_next_leaf(root, path);
  668. if (ret == 0)
  669. continue;
  670. if (ret < 0)
  671. goto out;
  672. break;
  673. }
  674. btrfs_item_key_to_cpu(l, &key, slot);
  675. if (key.objectid < device->devid)
  676. goto next;
  677. if (key.objectid > device->devid)
  678. break;
  679. if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
  680. goto next;
  681. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  682. extent_end = key.offset + btrfs_dev_extent_length(l,
  683. dev_extent);
  684. if (key.offset <= start && extent_end > end) {
  685. *length = end - start + 1;
  686. break;
  687. } else if (key.offset <= start && extent_end > start)
  688. *length += extent_end - start;
  689. else if (key.offset > start && extent_end <= end)
  690. *length += extent_end - key.offset;
  691. else if (key.offset > start && key.offset <= end) {
  692. *length += end - key.offset + 1;
  693. break;
  694. } else if (key.offset > end)
  695. break;
  696. next:
  697. path->slots[0]++;
  698. }
  699. ret = 0;
  700. out:
  701. btrfs_free_path(path);
  702. return ret;
  703. }
  704. /*
  705. * find_free_dev_extent - find free space in the specified device
  706. * @trans: transaction handler
  707. * @device: the device which we search the free space in
  708. * @num_bytes: the size of the free space that we need
  709. * @start: store the start of the free space.
  710. * @len: the size of the free space. that we find, or the size of the max
  711. * free space if we don't find suitable free space
  712. *
  713. * this uses a pretty simple search, the expectation is that it is
  714. * called very infrequently and that a given device has a small number
  715. * of extents
  716. *
  717. * @start is used to store the start of the free space if we find. But if we
  718. * don't find suitable free space, it will be used to store the start position
  719. * of the max free space.
  720. *
  721. * @len is used to store the size of the free space that we find.
  722. * But if we don't find suitable free space, it is used to store the size of
  723. * the max free space.
  724. */
  725. int find_free_dev_extent(struct btrfs_trans_handle *trans,
  726. struct btrfs_device *device, u64 num_bytes,
  727. u64 *start, u64 *len)
  728. {
  729. struct btrfs_key key;
  730. struct btrfs_root *root = device->dev_root;
  731. struct btrfs_dev_extent *dev_extent;
  732. struct btrfs_path *path;
  733. u64 hole_size;
  734. u64 max_hole_start;
  735. u64 max_hole_size;
  736. u64 extent_end;
  737. u64 search_start;
  738. u64 search_end = device->total_bytes;
  739. int ret;
  740. int slot;
  741. struct extent_buffer *l;
  742. /* FIXME use last free of some kind */
  743. /* we don't want to overwrite the superblock on the drive,
  744. * so we make sure to start at an offset of at least 1MB
  745. */
  746. search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
  747. max_hole_start = search_start;
  748. max_hole_size = 0;
  749. hole_size = 0;
  750. if (search_start >= search_end) {
  751. ret = -ENOSPC;
  752. goto error;
  753. }
  754. path = btrfs_alloc_path();
  755. if (!path) {
  756. ret = -ENOMEM;
  757. goto error;
  758. }
  759. path->reada = 2;
  760. key.objectid = device->devid;
  761. key.offset = search_start;
  762. key.type = BTRFS_DEV_EXTENT_KEY;
  763. ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
  764. if (ret < 0)
  765. goto out;
  766. if (ret > 0) {
  767. ret = btrfs_previous_item(root, path, key.objectid, key.type);
  768. if (ret < 0)
  769. goto out;
  770. }
  771. while (1) {
  772. l = path->nodes[0];
  773. slot = path->slots[0];
  774. if (slot >= btrfs_header_nritems(l)) {
  775. ret = btrfs_next_leaf(root, path);
  776. if (ret == 0)
  777. continue;
  778. if (ret < 0)
  779. goto out;
  780. break;
  781. }
  782. btrfs_item_key_to_cpu(l, &key, slot);
  783. if (key.objectid < device->devid)
  784. goto next;
  785. if (key.objectid > device->devid)
  786. break;
  787. if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
  788. goto next;
  789. if (key.offset > search_start) {
  790. hole_size = key.offset - search_start;
  791. if (hole_size > max_hole_size) {
  792. max_hole_start = search_start;
  793. max_hole_size = hole_size;
  794. }
  795. /*
  796. * If this free space is greater than which we need,
  797. * it must be the max free space that we have found
  798. * until now, so max_hole_start must point to the start
  799. * of this free space and the length of this free space
  800. * is stored in max_hole_size. Thus, we return
  801. * max_hole_start and max_hole_size and go back to the
  802. * caller.
  803. */
  804. if (hole_size >= num_bytes) {
  805. ret = 0;
  806. goto out;
  807. }
  808. }
  809. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  810. extent_end = key.offset + btrfs_dev_extent_length(l,
  811. dev_extent);
  812. if (extent_end > search_start)
  813. search_start = extent_end;
  814. next:
  815. path->slots[0]++;
  816. cond_resched();
  817. }
  818. /*
  819. * At this point, search_start should be the end of
  820. * allocated dev extents, and when shrinking the device,
  821. * search_end may be smaller than search_start.
  822. */
  823. if (search_end > search_start)
  824. hole_size = search_end - search_start;
  825. if (hole_size > max_hole_size) {
  826. max_hole_start = search_start;
  827. max_hole_size = hole_size;
  828. }
  829. /* See above. */
  830. if (hole_size < num_bytes)
  831. ret = -ENOSPC;
  832. else
  833. ret = 0;
  834. out:
  835. btrfs_free_path(path);
  836. error:
  837. *start = max_hole_start;
  838. if (len)
  839. *len = max_hole_size;
  840. return ret;
  841. }
  842. static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
  843. struct btrfs_device *device,
  844. u64 start)
  845. {
  846. int ret;
  847. struct btrfs_path *path;
  848. struct btrfs_root *root = device->dev_root;
  849. struct btrfs_key key;
  850. struct btrfs_key found_key;
  851. struct extent_buffer *leaf = NULL;
  852. struct btrfs_dev_extent *extent = NULL;
  853. path = btrfs_alloc_path();
  854. if (!path)
  855. return -ENOMEM;
  856. key.objectid = device->devid;
  857. key.offset = start;
  858. key.type = BTRFS_DEV_EXTENT_KEY;
  859. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  860. if (ret > 0) {
  861. ret = btrfs_previous_item(root, path, key.objectid,
  862. BTRFS_DEV_EXTENT_KEY);
  863. if (ret)
  864. goto out;
  865. leaf = path->nodes[0];
  866. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  867. extent = btrfs_item_ptr(leaf, path->slots[0],
  868. struct btrfs_dev_extent);
  869. BUG_ON(found_key.offset > start || found_key.offset +
  870. btrfs_dev_extent_length(leaf, extent) < start);
  871. } else if (ret == 0) {
  872. leaf = path->nodes[0];
  873. extent = btrfs_item_ptr(leaf, path->slots[0],
  874. struct btrfs_dev_extent);
  875. }
  876. BUG_ON(ret);
  877. if (device->bytes_used > 0)
  878. device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
  879. ret = btrfs_del_item(trans, root, path);
  880. out:
  881. btrfs_free_path(path);
  882. return ret;
  883. }
  884. int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
  885. struct btrfs_device *device,
  886. u64 chunk_tree, u64 chunk_objectid,
  887. u64 chunk_offset, u64 start, u64 num_bytes)
  888. {
  889. int ret;
  890. struct btrfs_path *path;
  891. struct btrfs_root *root = device->dev_root;
  892. struct btrfs_dev_extent *extent;
  893. struct extent_buffer *leaf;
  894. struct btrfs_key key;
  895. WARN_ON(!device->in_fs_metadata);
  896. path = btrfs_alloc_path();
  897. if (!path)
  898. return -ENOMEM;
  899. key.objectid = device->devid;
  900. key.offset = start;
  901. key.type = BTRFS_DEV_EXTENT_KEY;
  902. ret = btrfs_insert_empty_item(trans, root, path, &key,
  903. sizeof(*extent));
  904. BUG_ON(ret);
  905. leaf = path->nodes[0];
  906. extent = btrfs_item_ptr(leaf, path->slots[0],
  907. struct btrfs_dev_extent);
  908. btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
  909. btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
  910. btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
  911. write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
  912. (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
  913. BTRFS_UUID_SIZE);
  914. btrfs_set_dev_extent_length(leaf, extent, num_bytes);
  915. btrfs_mark_buffer_dirty(leaf);
  916. btrfs_free_path(path);
  917. return ret;
  918. }
  919. static noinline int find_next_chunk(struct btrfs_root *root,
  920. u64 objectid, u64 *offset)
  921. {
  922. struct btrfs_path *path;
  923. int ret;
  924. struct btrfs_key key;
  925. struct btrfs_chunk *chunk;
  926. struct btrfs_key found_key;
  927. path = btrfs_alloc_path();
  928. if (!path)
  929. return -ENOMEM;
  930. key.objectid = objectid;
  931. key.offset = (u64)-1;
  932. key.type = BTRFS_CHUNK_ITEM_KEY;
  933. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  934. if (ret < 0)
  935. goto error;
  936. BUG_ON(ret == 0);
  937. ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
  938. if (ret) {
  939. *offset = 0;
  940. } else {
  941. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  942. path->slots[0]);
  943. if (found_key.objectid != objectid)
  944. *offset = 0;
  945. else {
  946. chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
  947. struct btrfs_chunk);
  948. *offset = found_key.offset +
  949. btrfs_chunk_length(path->nodes[0], chunk);
  950. }
  951. }
  952. ret = 0;
  953. error:
  954. btrfs_free_path(path);
  955. return ret;
  956. }
  957. static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
  958. {
  959. int ret;
  960. struct btrfs_key key;
  961. struct btrfs_key found_key;
  962. struct btrfs_path *path;
  963. root = root->fs_info->chunk_root;
  964. path = btrfs_alloc_path();
  965. if (!path)
  966. return -ENOMEM;
  967. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  968. key.type = BTRFS_DEV_ITEM_KEY;
  969. key.offset = (u64)-1;
  970. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  971. if (ret < 0)
  972. goto error;
  973. BUG_ON(ret == 0);
  974. ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
  975. BTRFS_DEV_ITEM_KEY);
  976. if (ret) {
  977. *objectid = 1;
  978. } else {
  979. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  980. path->slots[0]);
  981. *objectid = found_key.offset + 1;
  982. }
  983. ret = 0;
  984. error:
  985. btrfs_free_path(path);
  986. return ret;
  987. }
  988. /*
  989. * the device information is stored in the chunk root
  990. * the btrfs_device struct should be fully filled in
  991. */
  992. int btrfs_add_device(struct btrfs_trans_handle *trans,
  993. struct btrfs_root *root,
  994. struct btrfs_device *device)
  995. {
  996. int ret;
  997. struct btrfs_path *path;
  998. struct btrfs_dev_item *dev_item;
  999. struct extent_buffer *leaf;
  1000. struct btrfs_key key;
  1001. unsigned long ptr;
  1002. root = root->fs_info->chunk_root;
  1003. path = btrfs_alloc_path();
  1004. if (!path)
  1005. return -ENOMEM;
  1006. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1007. key.type = BTRFS_DEV_ITEM_KEY;
  1008. key.offset = device->devid;
  1009. ret = btrfs_insert_empty_item(trans, root, path, &key,
  1010. sizeof(*dev_item));
  1011. if (ret)
  1012. goto out;
  1013. leaf = path->nodes[0];
  1014. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  1015. btrfs_set_device_id(leaf, dev_item, device->devid);
  1016. btrfs_set_device_generation(leaf, dev_item, 0);
  1017. btrfs_set_device_type(leaf, dev_item, device->type);
  1018. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  1019. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  1020. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  1021. btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
  1022. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  1023. btrfs_set_device_group(leaf, dev_item, 0);
  1024. btrfs_set_device_seek_speed(leaf, dev_item, 0);
  1025. btrfs_set_device_bandwidth(leaf, dev_item, 0);
  1026. btrfs_set_device_start_offset(leaf, dev_item, 0);
  1027. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  1028. write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  1029. ptr = (unsigned long)btrfs_device_fsid(dev_item);
  1030. write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
  1031. btrfs_mark_buffer_dirty(leaf);
  1032. ret = 0;
  1033. out:
  1034. btrfs_free_path(path);
  1035. return ret;
  1036. }
  1037. static int btrfs_rm_dev_item(struct btrfs_root *root,
  1038. struct btrfs_device *device)
  1039. {
  1040. int ret;
  1041. struct btrfs_path *path;
  1042. struct btrfs_key key;
  1043. struct btrfs_trans_handle *trans;
  1044. root = root->fs_info->chunk_root;
  1045. path = btrfs_alloc_path();
  1046. if (!path)
  1047. return -ENOMEM;
  1048. trans = btrfs_start_transaction(root, 0);
  1049. if (IS_ERR(trans)) {
  1050. btrfs_free_path(path);
  1051. return PTR_ERR(trans);
  1052. }
  1053. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1054. key.type = BTRFS_DEV_ITEM_KEY;
  1055. key.offset = device->devid;
  1056. lock_chunks(root);
  1057. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1058. if (ret < 0)
  1059. goto out;
  1060. if (ret > 0) {
  1061. ret = -ENOENT;
  1062. goto out;
  1063. }
  1064. ret = btrfs_del_item(trans, root, path);
  1065. if (ret)
  1066. goto out;
  1067. out:
  1068. btrfs_free_path(path);
  1069. unlock_chunks(root);
  1070. btrfs_commit_transaction(trans, root);
  1071. return ret;
  1072. }
  1073. int btrfs_rm_device(struct btrfs_root *root, char *device_path)
  1074. {
  1075. struct btrfs_device *device;
  1076. struct btrfs_device *next_device;
  1077. struct block_device *bdev;
  1078. struct buffer_head *bh = NULL;
  1079. struct btrfs_super_block *disk_super;
  1080. struct btrfs_fs_devices *cur_devices;
  1081. u64 all_avail;
  1082. u64 devid;
  1083. u64 num_devices;
  1084. u8 *dev_uuid;
  1085. int ret = 0;
  1086. bool clear_super = false;
  1087. mutex_lock(&uuid_mutex);
  1088. mutex_lock(&root->fs_info->volume_mutex);
  1089. all_avail = root->fs_info->avail_data_alloc_bits |
  1090. root->fs_info->avail_system_alloc_bits |
  1091. root->fs_info->avail_metadata_alloc_bits;
  1092. if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
  1093. root->fs_info->fs_devices->num_devices <= 4) {
  1094. printk(KERN_ERR "btrfs: unable to go below four devices "
  1095. "on raid10\n");
  1096. ret = -EINVAL;
  1097. goto out;
  1098. }
  1099. if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
  1100. root->fs_info->fs_devices->num_devices <= 2) {
  1101. printk(KERN_ERR "btrfs: unable to go below two "
  1102. "devices on raid1\n");
  1103. ret = -EINVAL;
  1104. goto out;
  1105. }
  1106. if (strcmp(device_path, "missing") == 0) {
  1107. struct list_head *devices;
  1108. struct btrfs_device *tmp;
  1109. device = NULL;
  1110. devices = &root->fs_info->fs_devices->devices;
  1111. /*
  1112. * It is safe to read the devices since the volume_mutex
  1113. * is held.
  1114. */
  1115. list_for_each_entry(tmp, devices, dev_list) {
  1116. if (tmp->in_fs_metadata && !tmp->bdev) {
  1117. device = tmp;
  1118. break;
  1119. }
  1120. }
  1121. bdev = NULL;
  1122. bh = NULL;
  1123. disk_super = NULL;
  1124. if (!device) {
  1125. printk(KERN_ERR "btrfs: no missing devices found to "
  1126. "remove\n");
  1127. goto out;
  1128. }
  1129. } else {
  1130. bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
  1131. root->fs_info->bdev_holder);
  1132. if (IS_ERR(bdev)) {
  1133. ret = PTR_ERR(bdev);
  1134. goto out;
  1135. }
  1136. set_blocksize(bdev, 4096);
  1137. bh = btrfs_read_dev_super(bdev);
  1138. if (!bh) {
  1139. ret = -EINVAL;
  1140. goto error_close;
  1141. }
  1142. disk_super = (struct btrfs_super_block *)bh->b_data;
  1143. devid = btrfs_stack_device_id(&disk_super->dev_item);
  1144. dev_uuid = disk_super->dev_item.uuid;
  1145. device = btrfs_find_device(root, devid, dev_uuid,
  1146. disk_super->fsid);
  1147. if (!device) {
  1148. ret = -ENOENT;
  1149. goto error_brelse;
  1150. }
  1151. }
  1152. if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
  1153. printk(KERN_ERR "btrfs: unable to remove the only writeable "
  1154. "device\n");
  1155. ret = -EINVAL;
  1156. goto error_brelse;
  1157. }
  1158. if (device->writeable) {
  1159. lock_chunks(root);
  1160. list_del_init(&device->dev_alloc_list);
  1161. unlock_chunks(root);
  1162. root->fs_info->fs_devices->rw_devices--;
  1163. clear_super = true;
  1164. }
  1165. ret = btrfs_shrink_device(device, 0);
  1166. if (ret)
  1167. goto error_undo;
  1168. ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
  1169. if (ret)
  1170. goto error_undo;
  1171. device->in_fs_metadata = 0;
  1172. btrfs_scrub_cancel_dev(root, device);
  1173. /*
  1174. * the device list mutex makes sure that we don't change
  1175. * the device list while someone else is writing out all
  1176. * the device supers.
  1177. */
  1178. cur_devices = device->fs_devices;
  1179. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1180. list_del_rcu(&device->dev_list);
  1181. device->fs_devices->num_devices--;
  1182. if (device->missing)
  1183. root->fs_info->fs_devices->missing_devices--;
  1184. next_device = list_entry(root->fs_info->fs_devices->devices.next,
  1185. struct btrfs_device, dev_list);
  1186. if (device->bdev == root->fs_info->sb->s_bdev)
  1187. root->fs_info->sb->s_bdev = next_device->bdev;
  1188. if (device->bdev == root->fs_info->fs_devices->latest_bdev)
  1189. root->fs_info->fs_devices->latest_bdev = next_device->bdev;
  1190. if (device->bdev)
  1191. device->fs_devices->open_devices--;
  1192. call_rcu(&device->rcu, free_device);
  1193. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1194. num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
  1195. btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
  1196. if (cur_devices->open_devices == 0) {
  1197. struct btrfs_fs_devices *fs_devices;
  1198. fs_devices = root->fs_info->fs_devices;
  1199. while (fs_devices) {
  1200. if (fs_devices->seed == cur_devices)
  1201. break;
  1202. fs_devices = fs_devices->seed;
  1203. }
  1204. fs_devices->seed = cur_devices->seed;
  1205. cur_devices->seed = NULL;
  1206. lock_chunks(root);
  1207. __btrfs_close_devices(cur_devices);
  1208. unlock_chunks(root);
  1209. free_fs_devices(cur_devices);
  1210. }
  1211. /*
  1212. * at this point, the device is zero sized. We want to
  1213. * remove it from the devices list and zero out the old super
  1214. */
  1215. if (clear_super) {
  1216. /* make sure this device isn't detected as part of
  1217. * the FS anymore
  1218. */
  1219. memset(&disk_super->magic, 0, sizeof(disk_super->magic));
  1220. set_buffer_dirty(bh);
  1221. sync_dirty_buffer(bh);
  1222. }
  1223. ret = 0;
  1224. error_brelse:
  1225. brelse(bh);
  1226. error_close:
  1227. if (bdev)
  1228. blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
  1229. out:
  1230. mutex_unlock(&root->fs_info->volume_mutex);
  1231. mutex_unlock(&uuid_mutex);
  1232. return ret;
  1233. error_undo:
  1234. if (device->writeable) {
  1235. lock_chunks(root);
  1236. list_add(&device->dev_alloc_list,
  1237. &root->fs_info->fs_devices->alloc_list);
  1238. unlock_chunks(root);
  1239. root->fs_info->fs_devices->rw_devices++;
  1240. }
  1241. goto error_brelse;
  1242. }
  1243. /*
  1244. * does all the dirty work required for changing file system's UUID.
  1245. */
  1246. static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
  1247. struct btrfs_root *root)
  1248. {
  1249. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  1250. struct btrfs_fs_devices *old_devices;
  1251. struct btrfs_fs_devices *seed_devices;
  1252. struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
  1253. struct btrfs_device *device;
  1254. u64 super_flags;
  1255. BUG_ON(!mutex_is_locked(&uuid_mutex));
  1256. if (!fs_devices->seeding)
  1257. return -EINVAL;
  1258. seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
  1259. if (!seed_devices)
  1260. return -ENOMEM;
  1261. old_devices = clone_fs_devices(fs_devices);
  1262. if (IS_ERR(old_devices)) {
  1263. kfree(seed_devices);
  1264. return PTR_ERR(old_devices);
  1265. }
  1266. list_add(&old_devices->list, &fs_uuids);
  1267. memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
  1268. seed_devices->opened = 1;
  1269. INIT_LIST_HEAD(&seed_devices->devices);
  1270. INIT_LIST_HEAD(&seed_devices->alloc_list);
  1271. mutex_init(&seed_devices->device_list_mutex);
  1272. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1273. list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
  1274. synchronize_rcu);
  1275. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1276. list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
  1277. list_for_each_entry(device, &seed_devices->devices, dev_list) {
  1278. device->fs_devices = seed_devices;
  1279. }
  1280. fs_devices->seeding = 0;
  1281. fs_devices->num_devices = 0;
  1282. fs_devices->open_devices = 0;
  1283. fs_devices->seed = seed_devices;
  1284. generate_random_uuid(fs_devices->fsid);
  1285. memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
  1286. memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
  1287. super_flags = btrfs_super_flags(disk_super) &
  1288. ~BTRFS_SUPER_FLAG_SEEDING;
  1289. btrfs_set_super_flags(disk_super, super_flags);
  1290. return 0;
  1291. }
  1292. /*
  1293. * strore the expected generation for seed devices in device items.
  1294. */
  1295. static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
  1296. struct btrfs_root *root)
  1297. {
  1298. struct btrfs_path *path;
  1299. struct extent_buffer *leaf;
  1300. struct btrfs_dev_item *dev_item;
  1301. struct btrfs_device *device;
  1302. struct btrfs_key key;
  1303. u8 fs_uuid[BTRFS_UUID_SIZE];
  1304. u8 dev_uuid[BTRFS_UUID_SIZE];
  1305. u64 devid;
  1306. int ret;
  1307. path = btrfs_alloc_path();
  1308. if (!path)
  1309. return -ENOMEM;
  1310. root = root->fs_info->chunk_root;
  1311. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1312. key.offset = 0;
  1313. key.type = BTRFS_DEV_ITEM_KEY;
  1314. while (1) {
  1315. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1316. if (ret < 0)
  1317. goto error;
  1318. leaf = path->nodes[0];
  1319. next_slot:
  1320. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  1321. ret = btrfs_next_leaf(root, path);
  1322. if (ret > 0)
  1323. break;
  1324. if (ret < 0)
  1325. goto error;
  1326. leaf = path->nodes[0];
  1327. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1328. btrfs_release_path(path);
  1329. continue;
  1330. }
  1331. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1332. if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
  1333. key.type != BTRFS_DEV_ITEM_KEY)
  1334. break;
  1335. dev_item = btrfs_item_ptr(leaf, path->slots[0],
  1336. struct btrfs_dev_item);
  1337. devid = btrfs_device_id(leaf, dev_item);
  1338. read_extent_buffer(leaf, dev_uuid,
  1339. (unsigned long)btrfs_device_uuid(dev_item),
  1340. BTRFS_UUID_SIZE);
  1341. read_extent_buffer(leaf, fs_uuid,
  1342. (unsigned long)btrfs_device_fsid(dev_item),
  1343. BTRFS_UUID_SIZE);
  1344. device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
  1345. BUG_ON(!device);
  1346. if (device->fs_devices->seeding) {
  1347. btrfs_set_device_generation(leaf, dev_item,
  1348. device->generation);
  1349. btrfs_mark_buffer_dirty(leaf);
  1350. }
  1351. path->slots[0]++;
  1352. goto next_slot;
  1353. }
  1354. ret = 0;
  1355. error:
  1356. btrfs_free_path(path);
  1357. return ret;
  1358. }
  1359. int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
  1360. {
  1361. struct request_queue *q;
  1362. struct btrfs_trans_handle *trans;
  1363. struct btrfs_device *device;
  1364. struct block_device *bdev;
  1365. struct list_head *devices;
  1366. struct super_block *sb = root->fs_info->sb;
  1367. u64 total_bytes;
  1368. int seeding_dev = 0;
  1369. int ret = 0;
  1370. if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
  1371. return -EINVAL;
  1372. bdev = blkdev_get_by_path(device_path, FMODE_EXCL,
  1373. root->fs_info->bdev_holder);
  1374. if (IS_ERR(bdev))
  1375. return PTR_ERR(bdev);
  1376. if (root->fs_info->fs_devices->seeding) {
  1377. seeding_dev = 1;
  1378. down_write(&sb->s_umount);
  1379. mutex_lock(&uuid_mutex);
  1380. }
  1381. filemap_write_and_wait(bdev->bd_inode->i_mapping);
  1382. mutex_lock(&root->fs_info->volume_mutex);
  1383. devices = &root->fs_info->fs_devices->devices;
  1384. /*
  1385. * we have the volume lock, so we don't need the extra
  1386. * device list mutex while reading the list here.
  1387. */
  1388. list_for_each_entry(device, devices, dev_list) {
  1389. if (device->bdev == bdev) {
  1390. ret = -EEXIST;
  1391. goto error;
  1392. }
  1393. }
  1394. device = kzalloc(sizeof(*device), GFP_NOFS);
  1395. if (!device) {
  1396. /* we can safely leave the fs_devices entry around */
  1397. ret = -ENOMEM;
  1398. goto error;
  1399. }
  1400. device->name = kstrdup(device_path, GFP_NOFS);
  1401. if (!device->name) {
  1402. kfree(device);
  1403. ret = -ENOMEM;
  1404. goto error;
  1405. }
  1406. ret = find_next_devid(root, &device->devid);
  1407. if (ret) {
  1408. kfree(device->name);
  1409. kfree(device);
  1410. goto error;
  1411. }
  1412. trans = btrfs_start_transaction(root, 0);
  1413. if (IS_ERR(trans)) {
  1414. kfree(device->name);
  1415. kfree(device);
  1416. ret = PTR_ERR(trans);
  1417. goto error;
  1418. }
  1419. lock_chunks(root);
  1420. q = bdev_get_queue(bdev);
  1421. if (blk_queue_discard(q))
  1422. device->can_discard = 1;
  1423. device->writeable = 1;
  1424. device->work.func = pending_bios_fn;
  1425. generate_random_uuid(device->uuid);
  1426. spin_lock_init(&device->io_lock);
  1427. device->generation = trans->transid;
  1428. device->io_width = root->sectorsize;
  1429. device->io_align = root->sectorsize;
  1430. device->sector_size = root->sectorsize;
  1431. device->total_bytes = i_size_read(bdev->bd_inode);
  1432. device->disk_total_bytes = device->total_bytes;
  1433. device->dev_root = root->fs_info->dev_root;
  1434. device->bdev = bdev;
  1435. device->in_fs_metadata = 1;
  1436. device->mode = FMODE_EXCL;
  1437. set_blocksize(device->bdev, 4096);
  1438. if (seeding_dev) {
  1439. sb->s_flags &= ~MS_RDONLY;
  1440. ret = btrfs_prepare_sprout(trans, root);
  1441. BUG_ON(ret);
  1442. }
  1443. device->fs_devices = root->fs_info->fs_devices;
  1444. /*
  1445. * we don't want write_supers to jump in here with our device
  1446. * half setup
  1447. */
  1448. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1449. list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
  1450. list_add(&device->dev_alloc_list,
  1451. &root->fs_info->fs_devices->alloc_list);
  1452. root->fs_info->fs_devices->num_devices++;
  1453. root->fs_info->fs_devices->open_devices++;
  1454. root->fs_info->fs_devices->rw_devices++;
  1455. if (device->can_discard)
  1456. root->fs_info->fs_devices->num_can_discard++;
  1457. root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
  1458. if (!blk_queue_nonrot(bdev_get_queue(bdev)))
  1459. root->fs_info->fs_devices->rotating = 1;
  1460. total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
  1461. btrfs_set_super_total_bytes(&root->fs_info->super_copy,
  1462. total_bytes + device->total_bytes);
  1463. total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
  1464. btrfs_set_super_num_devices(&root->fs_info->super_copy,
  1465. total_bytes + 1);
  1466. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1467. if (seeding_dev) {
  1468. ret = init_first_rw_device(trans, root, device);
  1469. BUG_ON(ret);
  1470. ret = btrfs_finish_sprout(trans, root);
  1471. BUG_ON(ret);
  1472. } else {
  1473. ret = btrfs_add_device(trans, root, device);
  1474. }
  1475. /*
  1476. * we've got more storage, clear any full flags on the space
  1477. * infos
  1478. */
  1479. btrfs_clear_space_info_full(root->fs_info);
  1480. unlock_chunks(root);
  1481. btrfs_commit_transaction(trans, root);
  1482. if (seeding_dev) {
  1483. mutex_unlock(&uuid_mutex);
  1484. up_write(&sb->s_umount);
  1485. ret = btrfs_relocate_sys_chunks(root);
  1486. BUG_ON(ret);
  1487. }
  1488. out:
  1489. mutex_unlock(&root->fs_info->volume_mutex);
  1490. return ret;
  1491. error:
  1492. blkdev_put(bdev, FMODE_EXCL);
  1493. if (seeding_dev) {
  1494. mutex_unlock(&uuid_mutex);
  1495. up_write(&sb->s_umount);
  1496. }
  1497. goto out;
  1498. }
  1499. static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
  1500. struct btrfs_device *device)
  1501. {
  1502. int ret;
  1503. struct btrfs_path *path;
  1504. struct btrfs_root *root;
  1505. struct btrfs_dev_item *dev_item;
  1506. struct extent_buffer *leaf;
  1507. struct btrfs_key key;
  1508. root = device->dev_root->fs_info->chunk_root;
  1509. path = btrfs_alloc_path();
  1510. if (!path)
  1511. return -ENOMEM;
  1512. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1513. key.type = BTRFS_DEV_ITEM_KEY;
  1514. key.offset = device->devid;
  1515. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1516. if (ret < 0)
  1517. goto out;
  1518. if (ret > 0) {
  1519. ret = -ENOENT;
  1520. goto out;
  1521. }
  1522. leaf = path->nodes[0];
  1523. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  1524. btrfs_set_device_id(leaf, dev_item, device->devid);
  1525. btrfs_set_device_type(leaf, dev_item, device->type);
  1526. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  1527. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  1528. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  1529. btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
  1530. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  1531. btrfs_mark_buffer_dirty(leaf);
  1532. out:
  1533. btrfs_free_path(path);
  1534. return ret;
  1535. }
  1536. static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
  1537. struct btrfs_device *device, u64 new_size)
  1538. {
  1539. struct btrfs_super_block *super_copy =
  1540. &device->dev_root->fs_info->super_copy;
  1541. u64 old_total = btrfs_super_total_bytes(super_copy);
  1542. u64 diff = new_size - device->total_bytes;
  1543. if (!device->writeable)
  1544. return -EACCES;
  1545. if (new_size <= device->total_bytes)
  1546. return -EINVAL;
  1547. btrfs_set_super_total_bytes(super_copy, old_total + diff);
  1548. device->fs_devices->total_rw_bytes += diff;
  1549. device->total_bytes = new_size;
  1550. device->disk_total_bytes = new_size;
  1551. btrfs_clear_space_info_full(device->dev_root->fs_info);
  1552. return btrfs_update_device(trans, device);
  1553. }
  1554. int btrfs_grow_device(struct btrfs_trans_handle *trans,
  1555. struct btrfs_device *device, u64 new_size)
  1556. {
  1557. int ret;
  1558. lock_chunks(device->dev_root);
  1559. ret = __btrfs_grow_device(trans, device, new_size);
  1560. unlock_chunks(device->dev_root);
  1561. return ret;
  1562. }
  1563. static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
  1564. struct btrfs_root *root,
  1565. u64 chunk_tree, u64 chunk_objectid,
  1566. u64 chunk_offset)
  1567. {
  1568. int ret;
  1569. struct btrfs_path *path;
  1570. struct btrfs_key key;
  1571. root = root->fs_info->chunk_root;
  1572. path = btrfs_alloc_path();
  1573. if (!path)
  1574. return -ENOMEM;
  1575. key.objectid = chunk_objectid;
  1576. key.offset = chunk_offset;
  1577. key.type = BTRFS_CHUNK_ITEM_KEY;
  1578. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1579. BUG_ON(ret);
  1580. ret = btrfs_del_item(trans, root, path);
  1581. btrfs_free_path(path);
  1582. return ret;
  1583. }
  1584. static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
  1585. chunk_offset)
  1586. {
  1587. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  1588. struct btrfs_disk_key *disk_key;
  1589. struct btrfs_chunk *chunk;
  1590. u8 *ptr;
  1591. int ret = 0;
  1592. u32 num_stripes;
  1593. u32 array_size;
  1594. u32 len = 0;
  1595. u32 cur;
  1596. struct btrfs_key key;
  1597. array_size = btrfs_super_sys_array_size(super_copy);
  1598. ptr = super_copy->sys_chunk_array;
  1599. cur = 0;
  1600. while (cur < array_size) {
  1601. disk_key = (struct btrfs_disk_key *)ptr;
  1602. btrfs_disk_key_to_cpu(&key, disk_key);
  1603. len = sizeof(*disk_key);
  1604. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  1605. chunk = (struct btrfs_chunk *)(ptr + len);
  1606. num_stripes = btrfs_stack_chunk_num_stripes(chunk);
  1607. len += btrfs_chunk_item_size(num_stripes);
  1608. } else {
  1609. ret = -EIO;
  1610. break;
  1611. }
  1612. if (key.objectid == chunk_objectid &&
  1613. key.offset == chunk_offset) {
  1614. memmove(ptr, ptr + len, array_size - (cur + len));
  1615. array_size -= len;
  1616. btrfs_set_super_sys_array_size(super_copy, array_size);
  1617. } else {
  1618. ptr += len;
  1619. cur += len;
  1620. }
  1621. }
  1622. return ret;
  1623. }
  1624. static int btrfs_relocate_chunk(struct btrfs_root *root,
  1625. u64 chunk_tree, u64 chunk_objectid,
  1626. u64 chunk_offset)
  1627. {
  1628. struct extent_map_tree *em_tree;
  1629. struct btrfs_root *extent_root;
  1630. struct btrfs_trans_handle *trans;
  1631. struct extent_map *em;
  1632. struct map_lookup *map;
  1633. int ret;
  1634. int i;
  1635. root = root->fs_info->chunk_root;
  1636. extent_root = root->fs_info->extent_root;
  1637. em_tree = &root->fs_info->mapping_tree.map_tree;
  1638. ret = btrfs_can_relocate(extent_root, chunk_offset);
  1639. if (ret)
  1640. return -ENOSPC;
  1641. /* step one, relocate all the extents inside this chunk */
  1642. ret = btrfs_relocate_block_group(extent_root, chunk_offset);
  1643. if (ret)
  1644. return ret;
  1645. trans = btrfs_start_transaction(root, 0);
  1646. BUG_ON(IS_ERR(trans));
  1647. lock_chunks(root);
  1648. /*
  1649. * step two, delete the device extents and the
  1650. * chunk tree entries
  1651. */
  1652. read_lock(&em_tree->lock);
  1653. em = lookup_extent_mapping(em_tree, chunk_offset, 1);
  1654. read_unlock(&em_tree->lock);
  1655. BUG_ON(em->start > chunk_offset ||
  1656. em->start + em->len < chunk_offset);
  1657. map = (struct map_lookup *)em->bdev;
  1658. for (i = 0; i < map->num_stripes; i++) {
  1659. ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
  1660. map->stripes[i].physical);
  1661. BUG_ON(ret);
  1662. if (map->stripes[i].dev) {
  1663. ret = btrfs_update_device(trans, map->stripes[i].dev);
  1664. BUG_ON(ret);
  1665. }
  1666. }
  1667. ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
  1668. chunk_offset);
  1669. BUG_ON(ret);
  1670. trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
  1671. if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1672. ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
  1673. BUG_ON(ret);
  1674. }
  1675. ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
  1676. BUG_ON(ret);
  1677. write_lock(&em_tree->lock);
  1678. remove_extent_mapping(em_tree, em);
  1679. write_unlock(&em_tree->lock);
  1680. kfree(map);
  1681. em->bdev = NULL;
  1682. /* once for the tree */
  1683. free_extent_map(em);
  1684. /* once for us */
  1685. free_extent_map(em);
  1686. unlock_chunks(root);
  1687. btrfs_end_transaction(trans, root);
  1688. return 0;
  1689. }
  1690. static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
  1691. {
  1692. struct btrfs_root *chunk_root = root->fs_info->chunk_root;
  1693. struct btrfs_path *path;
  1694. struct extent_buffer *leaf;
  1695. struct btrfs_chunk *chunk;
  1696. struct btrfs_key key;
  1697. struct btrfs_key found_key;
  1698. u64 chunk_tree = chunk_root->root_key.objectid;
  1699. u64 chunk_type;
  1700. bool retried = false;
  1701. int failed = 0;
  1702. int ret;
  1703. path = btrfs_alloc_path();
  1704. if (!path)
  1705. return -ENOMEM;
  1706. again:
  1707. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  1708. key.offset = (u64)-1;
  1709. key.type = BTRFS_CHUNK_ITEM_KEY;
  1710. while (1) {
  1711. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  1712. if (ret < 0)
  1713. goto error;
  1714. BUG_ON(ret == 0);
  1715. ret = btrfs_previous_item(chunk_root, path, key.objectid,
  1716. key.type);
  1717. if (ret < 0)
  1718. goto error;
  1719. if (ret > 0)
  1720. break;
  1721. leaf = path->nodes[0];
  1722. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1723. chunk = btrfs_item_ptr(leaf, path->slots[0],
  1724. struct btrfs_chunk);
  1725. chunk_type = btrfs_chunk_type(leaf, chunk);
  1726. btrfs_release_path(path);
  1727. if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1728. ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
  1729. found_key.objectid,
  1730. found_key.offset);
  1731. if (ret == -ENOSPC)
  1732. failed++;
  1733. else if (ret)
  1734. BUG();
  1735. }
  1736. if (found_key.offset == 0)
  1737. break;
  1738. key.offset = found_key.offset - 1;
  1739. }
  1740. ret = 0;
  1741. if (failed && !retried) {
  1742. failed = 0;
  1743. retried = true;
  1744. goto again;
  1745. } else if (failed && retried) {
  1746. WARN_ON(1);
  1747. ret = -ENOSPC;
  1748. }
  1749. error:
  1750. btrfs_free_path(path);
  1751. return ret;
  1752. }
  1753. static u64 div_factor(u64 num, int factor)
  1754. {
  1755. if (factor == 10)
  1756. return num;
  1757. num *= factor;
  1758. do_div(num, 10);
  1759. return num;
  1760. }
  1761. int btrfs_balance(struct btrfs_root *dev_root)
  1762. {
  1763. int ret;
  1764. struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
  1765. struct btrfs_device *device;
  1766. u64 old_size;
  1767. u64 size_to_free;
  1768. struct btrfs_path *path;
  1769. struct btrfs_key key;
  1770. struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
  1771. struct btrfs_trans_handle *trans;
  1772. struct btrfs_key found_key;
  1773. if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
  1774. return -EROFS;
  1775. if (!capable(CAP_SYS_ADMIN))
  1776. return -EPERM;
  1777. mutex_lock(&dev_root->fs_info->volume_mutex);
  1778. dev_root = dev_root->fs_info->dev_root;
  1779. /* step one make some room on all the devices */
  1780. list_for_each_entry(device, devices, dev_list) {
  1781. old_size = device->total_bytes;
  1782. size_to_free = div_factor(old_size, 1);
  1783. size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
  1784. if (!device->writeable ||
  1785. device->total_bytes - device->bytes_used > size_to_free)
  1786. continue;
  1787. ret = btrfs_shrink_device(device, old_size - size_to_free);
  1788. if (ret == -ENOSPC)
  1789. break;
  1790. BUG_ON(ret);
  1791. trans = btrfs_start_transaction(dev_root, 0);
  1792. BUG_ON(IS_ERR(trans));
  1793. ret = btrfs_grow_device(trans, device, old_size);
  1794. BUG_ON(ret);
  1795. btrfs_end_transaction(trans, dev_root);
  1796. }
  1797. /* step two, relocate all the chunks */
  1798. path = btrfs_alloc_path();
  1799. if (!path) {
  1800. ret = -ENOMEM;
  1801. goto error;
  1802. }
  1803. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  1804. key.offset = (u64)-1;
  1805. key.type = BTRFS_CHUNK_ITEM_KEY;
  1806. while (1) {
  1807. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  1808. if (ret < 0)
  1809. goto error;
  1810. /*
  1811. * this shouldn't happen, it means the last relocate
  1812. * failed
  1813. */
  1814. if (ret == 0)
  1815. break;
  1816. ret = btrfs_previous_item(chunk_root, path, 0,
  1817. BTRFS_CHUNK_ITEM_KEY);
  1818. if (ret)
  1819. break;
  1820. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1821. path->slots[0]);
  1822. if (found_key.objectid != key.objectid)
  1823. break;
  1824. /* chunk zero is special */
  1825. if (found_key.offset == 0)
  1826. break;
  1827. btrfs_release_path(path);
  1828. ret = btrfs_relocate_chunk(chunk_root,
  1829. chunk_root->root_key.objectid,
  1830. found_key.objectid,
  1831. found_key.offset);
  1832. if (ret && ret != -ENOSPC)
  1833. goto error;
  1834. key.offset = found_key.offset - 1;
  1835. }
  1836. ret = 0;
  1837. error:
  1838. btrfs_free_path(path);
  1839. mutex_unlock(&dev_root->fs_info->volume_mutex);
  1840. return ret;
  1841. }
  1842. /*
  1843. * shrinking a device means finding all of the device extents past
  1844. * the new size, and then following the back refs to the chunks.
  1845. * The chunk relocation code actually frees the device extent
  1846. */
  1847. int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
  1848. {
  1849. struct btrfs_trans_handle *trans;
  1850. struct btrfs_root *root = device->dev_root;
  1851. struct btrfs_dev_extent *dev_extent = NULL;
  1852. struct btrfs_path *path;
  1853. u64 length;
  1854. u64 chunk_tree;
  1855. u64 chunk_objectid;
  1856. u64 chunk_offset;
  1857. int ret;
  1858. int slot;
  1859. int failed = 0;
  1860. bool retried = false;
  1861. struct extent_buffer *l;
  1862. struct btrfs_key key;
  1863. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  1864. u64 old_total = btrfs_super_total_bytes(super_copy);
  1865. u64 old_size = device->total_bytes;
  1866. u64 diff = device->total_bytes - new_size;
  1867. if (new_size >= device->total_bytes)
  1868. return -EINVAL;
  1869. path = btrfs_alloc_path();
  1870. if (!path)
  1871. return -ENOMEM;
  1872. path->reada = 2;
  1873. lock_chunks(root);
  1874. device->total_bytes = new_size;
  1875. if (device->writeable)
  1876. device->fs_devices->total_rw_bytes -= diff;
  1877. unlock_chunks(root);
  1878. again:
  1879. key.objectid = device->devid;
  1880. key.offset = (u64)-1;
  1881. key.type = BTRFS_DEV_EXTENT_KEY;
  1882. while (1) {
  1883. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1884. if (ret < 0)
  1885. goto done;
  1886. ret = btrfs_previous_item(root, path, 0, key.type);
  1887. if (ret < 0)
  1888. goto done;
  1889. if (ret) {
  1890. ret = 0;
  1891. btrfs_release_path(path);
  1892. break;
  1893. }
  1894. l = path->nodes[0];
  1895. slot = path->slots[0];
  1896. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  1897. if (key.objectid != device->devid) {
  1898. btrfs_release_path(path);
  1899. break;
  1900. }
  1901. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  1902. length = btrfs_dev_extent_length(l, dev_extent);
  1903. if (key.offset + length <= new_size) {
  1904. btrfs_release_path(path);
  1905. break;
  1906. }
  1907. chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
  1908. chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
  1909. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  1910. btrfs_release_path(path);
  1911. ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
  1912. chunk_offset);
  1913. if (ret && ret != -ENOSPC)
  1914. goto done;
  1915. if (ret == -ENOSPC)
  1916. failed++;
  1917. key.offset -= 1;
  1918. }
  1919. if (failed && !retried) {
  1920. failed = 0;
  1921. retried = true;
  1922. goto again;
  1923. } else if (failed && retried) {
  1924. ret = -ENOSPC;
  1925. lock_chunks(root);
  1926. device->total_bytes = old_size;
  1927. if (device->writeable)
  1928. device->fs_devices->total_rw_bytes += diff;
  1929. unlock_chunks(root);
  1930. goto done;
  1931. }
  1932. /* Shrinking succeeded, else we would be at "done". */
  1933. trans = btrfs_start_transaction(root, 0);
  1934. if (IS_ERR(trans)) {
  1935. ret = PTR_ERR(trans);
  1936. goto done;
  1937. }
  1938. lock_chunks(root);
  1939. device->disk_total_bytes = new_size;
  1940. /* Now btrfs_update_device() will change the on-disk size. */
  1941. ret = btrfs_update_device(trans, device);
  1942. if (ret) {
  1943. unlock_chunks(root);
  1944. btrfs_end_transaction(trans, root);
  1945. goto done;
  1946. }
  1947. WARN_ON(diff > old_total);
  1948. btrfs_set_super_total_bytes(super_copy, old_total - diff);
  1949. unlock_chunks(root);
  1950. btrfs_end_transaction(trans, root);
  1951. done:
  1952. btrfs_free_path(path);
  1953. return ret;
  1954. }
  1955. static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
  1956. struct btrfs_root *root,
  1957. struct btrfs_key *key,
  1958. struct btrfs_chunk *chunk, int item_size)
  1959. {
  1960. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  1961. struct btrfs_disk_key disk_key;
  1962. u32 array_size;
  1963. u8 *ptr;
  1964. array_size = btrfs_super_sys_array_size(super_copy);
  1965. if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
  1966. return -EFBIG;
  1967. ptr = super_copy->sys_chunk_array + array_size;
  1968. btrfs_cpu_key_to_disk(&disk_key, key);
  1969. memcpy(ptr, &disk_key, sizeof(disk_key));
  1970. ptr += sizeof(disk_key);
  1971. memcpy(ptr, chunk, item_size);
  1972. item_size += sizeof(disk_key);
  1973. btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
  1974. return 0;
  1975. }
  1976. /*
  1977. * sort the devices in descending order by max_avail, total_avail
  1978. */
  1979. static int btrfs_cmp_device_info(const void *a, const void *b)
  1980. {
  1981. const struct btrfs_device_info *di_a = a;
  1982. const struct btrfs_device_info *di_b = b;
  1983. if (di_a->max_avail > di_b->max_avail)
  1984. return -1;
  1985. if (di_a->max_avail < di_b->max_avail)
  1986. return 1;
  1987. if (di_a->total_avail > di_b->total_avail)
  1988. return -1;
  1989. if (di_a->total_avail < di_b->total_avail)
  1990. return 1;
  1991. return 0;
  1992. }
  1993. static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  1994. struct btrfs_root *extent_root,
  1995. struct map_lookup **map_ret,
  1996. u64 *num_bytes_out, u64 *stripe_size_out,
  1997. u64 start, u64 type)
  1998. {
  1999. struct btrfs_fs_info *info = extent_root->fs_info;
  2000. struct btrfs_fs_devices *fs_devices = info->fs_devices;
  2001. struct list_head *cur;
  2002. struct map_lookup *map = NULL;
  2003. struct extent_map_tree *em_tree;
  2004. struct extent_map *em;
  2005. struct btrfs_device_info *devices_info = NULL;
  2006. u64 total_avail;
  2007. int num_stripes; /* total number of stripes to allocate */
  2008. int sub_stripes; /* sub_stripes info for map */
  2009. int dev_stripes; /* stripes per dev */
  2010. int devs_max; /* max devs to use */
  2011. int devs_min; /* min devs needed */
  2012. int devs_increment; /* ndevs has to be a multiple of this */
  2013. int ncopies; /* how many copies to data has */
  2014. int ret;
  2015. u64 max_stripe_size;
  2016. u64 max_chunk_size;
  2017. u64 stripe_size;
  2018. u64 num_bytes;
  2019. int ndevs;
  2020. int i;
  2021. int j;
  2022. if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
  2023. (type & BTRFS_BLOCK_GROUP_DUP)) {
  2024. WARN_ON(1);
  2025. type &= ~BTRFS_BLOCK_GROUP_DUP;
  2026. }
  2027. if (list_empty(&fs_devices->alloc_list))
  2028. return -ENOSPC;
  2029. sub_stripes = 1;
  2030. dev_stripes = 1;
  2031. devs_increment = 1;
  2032. ncopies = 1;
  2033. devs_max = 0; /* 0 == as many as possible */
  2034. devs_min = 1;
  2035. /*
  2036. * define the properties of each RAID type.
  2037. * FIXME: move this to a global table and use it in all RAID
  2038. * calculation code
  2039. */
  2040. if (type & (BTRFS_BLOCK_GROUP_DUP)) {
  2041. dev_stripes = 2;
  2042. ncopies = 2;
  2043. devs_max = 1;
  2044. } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
  2045. devs_min = 2;
  2046. } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
  2047. devs_increment = 2;
  2048. ncopies = 2;
  2049. devs_max = 2;
  2050. devs_min = 2;
  2051. } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
  2052. sub_stripes = 2;
  2053. devs_increment = 2;
  2054. ncopies = 2;
  2055. devs_min = 4;
  2056. } else {
  2057. devs_max = 1;
  2058. }
  2059. if (type & BTRFS_BLOCK_GROUP_DATA) {
  2060. max_stripe_size = 1024 * 1024 * 1024;
  2061. max_chunk_size = 10 * max_stripe_size;
  2062. } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
  2063. max_stripe_size = 256 * 1024 * 1024;
  2064. max_chunk_size = max_stripe_size;
  2065. } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
  2066. max_stripe_size = 8 * 1024 * 1024;
  2067. max_chunk_size = 2 * max_stripe_size;
  2068. } else {
  2069. printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
  2070. type);
  2071. BUG_ON(1);
  2072. }
  2073. /* we don't want a chunk larger than 10% of writeable space */
  2074. max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
  2075. max_chunk_size);
  2076. devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
  2077. GFP_NOFS);
  2078. if (!devices_info)
  2079. return -ENOMEM;
  2080. cur = fs_devices->alloc_list.next;
  2081. /*
  2082. * in the first pass through the devices list, we gather information
  2083. * about the available holes on each device.
  2084. */
  2085. ndevs = 0;
  2086. while (cur != &fs_devices->alloc_list) {
  2087. struct btrfs_device *device;
  2088. u64 max_avail;
  2089. u64 dev_offset;
  2090. device = list_entry(cur, struct btrfs_device, dev_alloc_list);
  2091. cur = cur->next;
  2092. if (!device->writeable) {
  2093. printk(KERN_ERR
  2094. "btrfs: read-only device in alloc_list\n");
  2095. WARN_ON(1);
  2096. continue;
  2097. }
  2098. if (!device->in_fs_metadata)
  2099. continue;
  2100. if (device->total_bytes > device->bytes_used)
  2101. total_avail = device->total_bytes - device->bytes_used;
  2102. else
  2103. total_avail = 0;
  2104. /* If there is no space on this device, skip it. */
  2105. if (total_avail == 0)
  2106. continue;
  2107. ret = find_free_dev_extent(trans, device,
  2108. max_stripe_size * dev_stripes,
  2109. &dev_offset, &max_avail);
  2110. if (ret && ret != -ENOSPC)
  2111. goto error;
  2112. if (ret == 0)
  2113. max_avail = max_stripe_size * dev_stripes;
  2114. if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
  2115. continue;
  2116. devices_info[ndevs].dev_offset = dev_offset;
  2117. devices_info[ndevs].max_avail = max_avail;
  2118. devices_info[ndevs].total_avail = total_avail;
  2119. devices_info[ndevs].dev = device;
  2120. ++ndevs;
  2121. }
  2122. /*
  2123. * now sort the devices by hole size / available space
  2124. */
  2125. sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
  2126. btrfs_cmp_device_info, NULL);
  2127. /* round down to number of usable stripes */
  2128. ndevs -= ndevs % devs_increment;
  2129. if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
  2130. ret = -ENOSPC;
  2131. goto error;
  2132. }
  2133. if (devs_max && ndevs > devs_max)
  2134. ndevs = devs_max;
  2135. /*
  2136. * the primary goal is to maximize the number of stripes, so use as many
  2137. * devices as possible, even if the stripes are not maximum sized.
  2138. */
  2139. stripe_size = devices_info[ndevs-1].max_avail;
  2140. num_stripes = ndevs * dev_stripes;
  2141. if (stripe_size * num_stripes > max_chunk_size * ncopies) {
  2142. stripe_size = max_chunk_size * ncopies;
  2143. do_div(stripe_size, num_stripes);
  2144. }
  2145. do_div(stripe_size, dev_stripes);
  2146. do_div(stripe_size, BTRFS_STRIPE_LEN);
  2147. stripe_size *= BTRFS_STRIPE_LEN;
  2148. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  2149. if (!map) {
  2150. ret = -ENOMEM;
  2151. goto error;
  2152. }
  2153. map->num_stripes = num_stripes;
  2154. for (i = 0; i < ndevs; ++i) {
  2155. for (j = 0; j < dev_stripes; ++j) {
  2156. int s = i * dev_stripes + j;
  2157. map->stripes[s].dev = devices_info[i].dev;
  2158. map->stripes[s].physical = devices_info[i].dev_offset +
  2159. j * stripe_size;
  2160. }
  2161. }
  2162. map->sector_size = extent_root->sectorsize;
  2163. map->stripe_len = BTRFS_STRIPE_LEN;
  2164. map->io_align = BTRFS_STRIPE_LEN;
  2165. map->io_width = BTRFS_STRIPE_LEN;
  2166. map->type = type;
  2167. map->sub_stripes = sub_stripes;
  2168. *map_ret = map;
  2169. num_bytes = stripe_size * (num_stripes / ncopies);
  2170. *stripe_size_out = stripe_size;
  2171. *num_bytes_out = num_bytes;
  2172. trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
  2173. em = alloc_extent_map();
  2174. if (!em) {
  2175. ret = -ENOMEM;
  2176. goto error;
  2177. }
  2178. em->bdev = (struct block_device *)map;
  2179. em->start = start;
  2180. em->len = num_bytes;
  2181. em->block_start = 0;
  2182. em->block_len = em->len;
  2183. em_tree = &extent_root->fs_info->mapping_tree.map_tree;
  2184. write_lock(&em_tree->lock);
  2185. ret = add_extent_mapping(em_tree, em);
  2186. write_unlock(&em_tree->lock);
  2187. BUG_ON(ret);
  2188. free_extent_map(em);
  2189. ret = btrfs_make_block_group(trans, extent_root, 0, type,
  2190. BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  2191. start, num_bytes);
  2192. BUG_ON(ret);
  2193. for (i = 0; i < map->num_stripes; ++i) {
  2194. struct btrfs_device *device;
  2195. u64 dev_offset;
  2196. device = map->stripes[i].dev;
  2197. dev_offset = map->stripes[i].physical;
  2198. ret = btrfs_alloc_dev_extent(trans, device,
  2199. info->chunk_root->root_key.objectid,
  2200. BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  2201. start, dev_offset, stripe_size);
  2202. BUG_ON(ret);
  2203. }
  2204. kfree(devices_info);
  2205. return 0;
  2206. error:
  2207. kfree(map);
  2208. kfree(devices_info);
  2209. return ret;
  2210. }
  2211. static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
  2212. struct btrfs_root *extent_root,
  2213. struct map_lookup *map, u64 chunk_offset,
  2214. u64 chunk_size, u64 stripe_size)
  2215. {
  2216. u64 dev_offset;
  2217. struct btrfs_key key;
  2218. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  2219. struct btrfs_device *device;
  2220. struct btrfs_chunk *chunk;
  2221. struct btrfs_stripe *stripe;
  2222. size_t item_size = btrfs_chunk_item_size(map->num_stripes);
  2223. int index = 0;
  2224. int ret;
  2225. chunk = kzalloc(item_size, GFP_NOFS);
  2226. if (!chunk)
  2227. return -ENOMEM;
  2228. index = 0;
  2229. while (index < map->num_stripes) {
  2230. device = map->stripes[index].dev;
  2231. device->bytes_used += stripe_size;
  2232. ret = btrfs_update_device(trans, device);
  2233. BUG_ON(ret);
  2234. index++;
  2235. }
  2236. index = 0;
  2237. stripe = &chunk->stripe;
  2238. while (index < map->num_stripes) {
  2239. device = map->stripes[index].dev;
  2240. dev_offset = map->stripes[index].physical;
  2241. btrfs_set_stack_stripe_devid(stripe, device->devid);
  2242. btrfs_set_stack_stripe_offset(stripe, dev_offset);
  2243. memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
  2244. stripe++;
  2245. index++;
  2246. }
  2247. btrfs_set_stack_chunk_length(chunk, chunk_size);
  2248. btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
  2249. btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
  2250. btrfs_set_stack_chunk_type(chunk, map->type);
  2251. btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
  2252. btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
  2253. btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
  2254. btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
  2255. btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
  2256. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  2257. key.type = BTRFS_CHUNK_ITEM_KEY;
  2258. key.offset = chunk_offset;
  2259. ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
  2260. BUG_ON(ret);
  2261. if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  2262. ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
  2263. item_size);
  2264. BUG_ON(ret);
  2265. }
  2266. kfree(chunk);
  2267. return 0;
  2268. }
  2269. /*
  2270. * Chunk allocation falls into two parts. The first part does works
  2271. * that make the new allocated chunk useable, but not do any operation
  2272. * that modifies the chunk tree. The second part does the works that
  2273. * require modifying the chunk tree. This division is important for the
  2274. * bootstrap process of adding storage to a seed btrfs.
  2275. */
  2276. int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  2277. struct btrfs_root *extent_root, u64 type)
  2278. {
  2279. u64 chunk_offset;
  2280. u64 chunk_size;
  2281. u64 stripe_size;
  2282. struct map_lookup *map;
  2283. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  2284. int ret;
  2285. ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  2286. &chunk_offset);
  2287. if (ret)
  2288. return ret;
  2289. ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
  2290. &stripe_size, chunk_offset, type);
  2291. if (ret)
  2292. return ret;
  2293. ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
  2294. chunk_size, stripe_size);
  2295. BUG_ON(ret);
  2296. return 0;
  2297. }
  2298. static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
  2299. struct btrfs_root *root,
  2300. struct btrfs_device *device)
  2301. {
  2302. u64 chunk_offset;
  2303. u64 sys_chunk_offset;
  2304. u64 chunk_size;
  2305. u64 sys_chunk_size;
  2306. u64 stripe_size;
  2307. u64 sys_stripe_size;
  2308. u64 alloc_profile;
  2309. struct map_lookup *map;
  2310. struct map_lookup *sys_map;
  2311. struct btrfs_fs_info *fs_info = root->fs_info;
  2312. struct btrfs_root *extent_root = fs_info->extent_root;
  2313. int ret;
  2314. ret = find_next_chunk(fs_info->chunk_root,
  2315. BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
  2316. if (ret)
  2317. return ret;
  2318. alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
  2319. (fs_info->metadata_alloc_profile &
  2320. fs_info->avail_metadata_alloc_bits);
  2321. alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
  2322. ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
  2323. &stripe_size, chunk_offset, alloc_profile);
  2324. BUG_ON(ret);
  2325. sys_chunk_offset = chunk_offset + chunk_size;
  2326. alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
  2327. (fs_info->system_alloc_profile &
  2328. fs_info->avail_system_alloc_bits);
  2329. alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
  2330. ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
  2331. &sys_chunk_size, &sys_stripe_size,
  2332. sys_chunk_offset, alloc_profile);
  2333. BUG_ON(ret);
  2334. ret = btrfs_add_device(trans, fs_info->chunk_root, device);
  2335. BUG_ON(ret);
  2336. /*
  2337. * Modifying chunk tree needs allocating new blocks from both
  2338. * system block group and metadata block group. So we only can
  2339. * do operations require modifying the chunk tree after both
  2340. * block groups were created.
  2341. */
  2342. ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
  2343. chunk_size, stripe_size);
  2344. BUG_ON(ret);
  2345. ret = __finish_chunk_alloc(trans, extent_root, sys_map,
  2346. sys_chunk_offset, sys_chunk_size,
  2347. sys_stripe_size);
  2348. BUG_ON(ret);
  2349. return 0;
  2350. }
  2351. int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
  2352. {
  2353. struct extent_map *em;
  2354. struct map_lookup *map;
  2355. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  2356. int readonly = 0;
  2357. int i;
  2358. read_lock(&map_tree->map_tree.lock);
  2359. em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
  2360. read_unlock(&map_tree->map_tree.lock);
  2361. if (!em)
  2362. return 1;
  2363. if (btrfs_test_opt(root, DEGRADED)) {
  2364. free_extent_map(em);
  2365. return 0;
  2366. }
  2367. map = (struct map_lookup *)em->bdev;
  2368. for (i = 0; i < map->num_stripes; i++) {
  2369. if (!map->stripes[i].dev->writeable) {
  2370. readonly = 1;
  2371. break;
  2372. }
  2373. }
  2374. free_extent_map(em);
  2375. return readonly;
  2376. }
  2377. void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
  2378. {
  2379. extent_map_tree_init(&tree->map_tree);
  2380. }
  2381. void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
  2382. {
  2383. struct extent_map *em;
  2384. while (1) {
  2385. write_lock(&tree->map_tree.lock);
  2386. em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
  2387. if (em)
  2388. remove_extent_mapping(&tree->map_tree, em);
  2389. write_unlock(&tree->map_tree.lock);
  2390. if (!em)
  2391. break;
  2392. kfree(em->bdev);
  2393. /* once for us */
  2394. free_extent_map(em);
  2395. /* once for the tree */
  2396. free_extent_map(em);
  2397. }
  2398. }
  2399. int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
  2400. {
  2401. struct extent_map *em;
  2402. struct map_lookup *map;
  2403. struct extent_map_tree *em_tree = &map_tree->map_tree;
  2404. int ret;
  2405. read_lock(&em_tree->lock);
  2406. em = lookup_extent_mapping(em_tree, logical, len);
  2407. read_unlock(&em_tree->lock);
  2408. BUG_ON(!em);
  2409. BUG_ON(em->start > logical || em->start + em->len < logical);
  2410. map = (struct map_lookup *)em->bdev;
  2411. if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
  2412. ret = map->num_stripes;
  2413. else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  2414. ret = map->sub_stripes;
  2415. else
  2416. ret = 1;
  2417. free_extent_map(em);
  2418. return ret;
  2419. }
  2420. static int find_live_mirror(struct map_lookup *map, int first, int num,
  2421. int optimal)
  2422. {
  2423. int i;
  2424. if (map->stripes[optimal].dev->bdev)
  2425. return optimal;
  2426. for (i = first; i < first + num; i++) {
  2427. if (map->stripes[i].dev->bdev)
  2428. return i;
  2429. }
  2430. /* we couldn't find one that doesn't fail. Just return something
  2431. * and the io error handling code will clean up eventually
  2432. */
  2433. return optimal;
  2434. }
  2435. static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  2436. u64 logical, u64 *length,
  2437. struct btrfs_multi_bio **multi_ret,
  2438. int mirror_num)
  2439. {
  2440. struct extent_map *em;
  2441. struct map_lookup *map;
  2442. struct extent_map_tree *em_tree = &map_tree->map_tree;
  2443. u64 offset;
  2444. u64 stripe_offset;
  2445. u64 stripe_end_offset;
  2446. u64 stripe_nr;
  2447. u64 stripe_nr_orig;
  2448. u64 stripe_nr_end;
  2449. int stripes_allocated = 8;
  2450. int stripes_required = 1;
  2451. int stripe_index;
  2452. int i;
  2453. int num_stripes;
  2454. int max_errors = 0;
  2455. struct btrfs_multi_bio *multi = NULL;
  2456. if (multi_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
  2457. stripes_allocated = 1;
  2458. again:
  2459. if (multi_ret) {
  2460. multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
  2461. GFP_NOFS);
  2462. if (!multi)
  2463. return -ENOMEM;
  2464. atomic_set(&multi->error, 0);
  2465. }
  2466. read_lock(&em_tree->lock);
  2467. em = lookup_extent_mapping(em_tree, logical, *length);
  2468. read_unlock(&em_tree->lock);
  2469. if (!em) {
  2470. printk(KERN_CRIT "unable to find logical %llu len %llu\n",
  2471. (unsigned long long)logical,
  2472. (unsigned long long)*length);
  2473. BUG();
  2474. }
  2475. BUG_ON(em->start > logical || em->start + em->len < logical);
  2476. map = (struct map_lookup *)em->bdev;
  2477. offset = logical - em->start;
  2478. if (mirror_num > map->num_stripes)
  2479. mirror_num = 0;
  2480. /* if our multi bio struct is too small, back off and try again */
  2481. if (rw & REQ_WRITE) {
  2482. if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
  2483. BTRFS_BLOCK_GROUP_DUP)) {
  2484. stripes_required = map->num_stripes;
  2485. max_errors = 1;
  2486. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  2487. stripes_required = map->sub_stripes;
  2488. max_errors = 1;
  2489. }
  2490. }
  2491. if (rw & REQ_DISCARD) {
  2492. if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
  2493. BTRFS_BLOCK_GROUP_RAID1 |
  2494. BTRFS_BLOCK_GROUP_DUP |
  2495. BTRFS_BLOCK_GROUP_RAID10)) {
  2496. stripes_required = map->num_stripes;
  2497. }
  2498. }
  2499. if (multi_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
  2500. stripes_allocated < stripes_required) {
  2501. stripes_allocated = map->num_stripes;
  2502. free_extent_map(em);
  2503. kfree(multi);
  2504. goto again;
  2505. }
  2506. stripe_nr = offset;
  2507. /*
  2508. * stripe_nr counts the total number of stripes we have to stride
  2509. * to get to this block
  2510. */
  2511. do_div(stripe_nr, map->stripe_len);
  2512. stripe_offset = stripe_nr * map->stripe_len;
  2513. BUG_ON(offset < stripe_offset);
  2514. /* stripe_offset is the offset of this block in its stripe*/
  2515. stripe_offset = offset - stripe_offset;
  2516. if (rw & REQ_DISCARD)
  2517. *length = min_t(u64, em->len - offset, *length);
  2518. else if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
  2519. BTRFS_BLOCK_GROUP_RAID1 |
  2520. BTRFS_BLOCK_GROUP_RAID10 |
  2521. BTRFS_BLOCK_GROUP_DUP)) {
  2522. /* we limit the length of each bio to what fits in a stripe */
  2523. *length = min_t(u64, em->len - offset,
  2524. map->stripe_len - stripe_offset);
  2525. } else {
  2526. *length = em->len - offset;
  2527. }
  2528. if (!multi_ret)
  2529. goto out;
  2530. num_stripes = 1;
  2531. stripe_index = 0;
  2532. stripe_nr_orig = stripe_nr;
  2533. stripe_nr_end = (offset + *length + map->stripe_len - 1) &
  2534. (~(map->stripe_len - 1));
  2535. do_div(stripe_nr_end, map->stripe_len);
  2536. stripe_end_offset = stripe_nr_end * map->stripe_len -
  2537. (offset + *length);
  2538. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  2539. if (rw & REQ_DISCARD)
  2540. num_stripes = min_t(u64, map->num_stripes,
  2541. stripe_nr_end - stripe_nr_orig);
  2542. stripe_index = do_div(stripe_nr, map->num_stripes);
  2543. } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  2544. if (rw & (REQ_WRITE | REQ_DISCARD))
  2545. num_stripes = map->num_stripes;
  2546. else if (mirror_num)
  2547. stripe_index = mirror_num - 1;
  2548. else {
  2549. stripe_index = find_live_mirror(map, 0,
  2550. map->num_stripes,
  2551. current->pid % map->num_stripes);
  2552. }
  2553. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  2554. if (rw & (REQ_WRITE | REQ_DISCARD))
  2555. num_stripes = map->num_stripes;
  2556. else if (mirror_num)
  2557. stripe_index = mirror_num - 1;
  2558. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  2559. int factor = map->num_stripes / map->sub_stripes;
  2560. stripe_index = do_div(stripe_nr, factor);
  2561. stripe_index *= map->sub_stripes;
  2562. if (rw & REQ_WRITE)
  2563. num_stripes = map->sub_stripes;
  2564. else if (rw & REQ_DISCARD)
  2565. num_stripes = min_t(u64, map->sub_stripes *
  2566. (stripe_nr_end - stripe_nr_orig),
  2567. map->num_stripes);
  2568. else if (mirror_num)
  2569. stripe_index += mirror_num - 1;
  2570. else {
  2571. stripe_index = find_live_mirror(map, stripe_index,
  2572. map->sub_stripes, stripe_index +
  2573. current->pid % map->sub_stripes);
  2574. }
  2575. } else {
  2576. /*
  2577. * after this do_div call, stripe_nr is the number of stripes
  2578. * on this device we have to walk to find the data, and
  2579. * stripe_index is the number of our device in the stripe array
  2580. */
  2581. stripe_index = do_div(stripe_nr, map->num_stripes);
  2582. }
  2583. BUG_ON(stripe_index >= map->num_stripes);
  2584. if (rw & REQ_DISCARD) {
  2585. for (i = 0; i < num_stripes; i++) {
  2586. multi->stripes[i].physical =
  2587. map->stripes[stripe_index].physical +
  2588. stripe_offset + stripe_nr * map->stripe_len;
  2589. multi->stripes[i].dev = map->stripes[stripe_index].dev;
  2590. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  2591. u64 stripes;
  2592. u32 last_stripe = 0;
  2593. int j;
  2594. div_u64_rem(stripe_nr_end - 1,
  2595. map->num_stripes,
  2596. &last_stripe);
  2597. for (j = 0; j < map->num_stripes; j++) {
  2598. u32 test;
  2599. div_u64_rem(stripe_nr_end - 1 - j,
  2600. map->num_stripes, &test);
  2601. if (test == stripe_index)
  2602. break;
  2603. }
  2604. stripes = stripe_nr_end - 1 - j;
  2605. do_div(stripes, map->num_stripes);
  2606. multi->stripes[i].length = map->stripe_len *
  2607. (stripes - stripe_nr + 1);
  2608. if (i == 0) {
  2609. multi->stripes[i].length -=
  2610. stripe_offset;
  2611. stripe_offset = 0;
  2612. }
  2613. if (stripe_index == last_stripe)
  2614. multi->stripes[i].length -=
  2615. stripe_end_offset;
  2616. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  2617. u64 stripes;
  2618. int j;
  2619. int factor = map->num_stripes /
  2620. map->sub_stripes;
  2621. u32 last_stripe = 0;
  2622. div_u64_rem(stripe_nr_end - 1,
  2623. factor, &last_stripe);
  2624. last_stripe *= map->sub_stripes;
  2625. for (j = 0; j < factor; j++) {
  2626. u32 test;
  2627. div_u64_rem(stripe_nr_end - 1 - j,
  2628. factor, &test);
  2629. if (test ==
  2630. stripe_index / map->sub_stripes)
  2631. break;
  2632. }
  2633. stripes = stripe_nr_end - 1 - j;
  2634. do_div(stripes, factor);
  2635. multi->stripes[i].length = map->stripe_len *
  2636. (stripes - stripe_nr + 1);
  2637. if (i < map->sub_stripes) {
  2638. multi->stripes[i].length -=
  2639. stripe_offset;
  2640. if (i == map->sub_stripes - 1)
  2641. stripe_offset = 0;
  2642. }
  2643. if (stripe_index >= last_stripe &&
  2644. stripe_index <= (last_stripe +
  2645. map->sub_stripes - 1)) {
  2646. multi->stripes[i].length -=
  2647. stripe_end_offset;
  2648. }
  2649. } else
  2650. multi->stripes[i].length = *length;
  2651. stripe_index++;
  2652. if (stripe_index == map->num_stripes) {
  2653. /* This could only happen for RAID0/10 */
  2654. stripe_index = 0;
  2655. stripe_nr++;
  2656. }
  2657. }
  2658. } else {
  2659. for (i = 0; i < num_stripes; i++) {
  2660. multi->stripes[i].physical =
  2661. map->stripes[stripe_index].physical +
  2662. stripe_offset +
  2663. stripe_nr * map->stripe_len;
  2664. multi->stripes[i].dev =
  2665. map->stripes[stripe_index].dev;
  2666. stripe_index++;
  2667. }
  2668. }
  2669. if (multi_ret) {
  2670. *multi_ret = multi;
  2671. multi->num_stripes = num_stripes;
  2672. multi->max_errors = max_errors;
  2673. }
  2674. out:
  2675. free_extent_map(em);
  2676. return 0;
  2677. }
  2678. int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  2679. u64 logical, u64 *length,
  2680. struct btrfs_multi_bio **multi_ret, int mirror_num)
  2681. {
  2682. return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
  2683. mirror_num);
  2684. }
  2685. int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
  2686. u64 chunk_start, u64 physical, u64 devid,
  2687. u64 **logical, int *naddrs, int *stripe_len)
  2688. {
  2689. struct extent_map_tree *em_tree = &map_tree->map_tree;
  2690. struct extent_map *em;
  2691. struct map_lookup *map;
  2692. u64 *buf;
  2693. u64 bytenr;
  2694. u64 length;
  2695. u64 stripe_nr;
  2696. int i, j, nr = 0;
  2697. read_lock(&em_tree->lock);
  2698. em = lookup_extent_mapping(em_tree, chunk_start, 1);
  2699. read_unlock(&em_tree->lock);
  2700. BUG_ON(!em || em->start != chunk_start);
  2701. map = (struct map_lookup *)em->bdev;
  2702. length = em->len;
  2703. if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  2704. do_div(length, map->num_stripes / map->sub_stripes);
  2705. else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
  2706. do_div(length, map->num_stripes);
  2707. buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
  2708. BUG_ON(!buf);
  2709. for (i = 0; i < map->num_stripes; i++) {
  2710. if (devid && map->stripes[i].dev->devid != devid)
  2711. continue;
  2712. if (map->stripes[i].physical > physical ||
  2713. map->stripes[i].physical + length <= physical)
  2714. continue;
  2715. stripe_nr = physical - map->stripes[i].physical;
  2716. do_div(stripe_nr, map->stripe_len);
  2717. if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  2718. stripe_nr = stripe_nr * map->num_stripes + i;
  2719. do_div(stripe_nr, map->sub_stripes);
  2720. } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  2721. stripe_nr = stripe_nr * map->num_stripes + i;
  2722. }
  2723. bytenr = chunk_start + stripe_nr * map->stripe_len;
  2724. WARN_ON(nr >= map->num_stripes);
  2725. for (j = 0; j < nr; j++) {
  2726. if (buf[j] == bytenr)
  2727. break;
  2728. }
  2729. if (j == nr) {
  2730. WARN_ON(nr >= map->num_stripes);
  2731. buf[nr++] = bytenr;
  2732. }
  2733. }
  2734. *logical = buf;
  2735. *naddrs = nr;
  2736. *stripe_len = map->stripe_len;
  2737. free_extent_map(em);
  2738. return 0;
  2739. }
  2740. static void end_bio_multi_stripe(struct bio *bio, int err)
  2741. {
  2742. struct btrfs_multi_bio *multi = bio->bi_private;
  2743. int is_orig_bio = 0;
  2744. if (err)
  2745. atomic_inc(&multi->error);
  2746. if (bio == multi->orig_bio)
  2747. is_orig_bio = 1;
  2748. if (atomic_dec_and_test(&multi->stripes_pending)) {
  2749. if (!is_orig_bio) {
  2750. bio_put(bio);
  2751. bio = multi->orig_bio;
  2752. }
  2753. bio->bi_private = multi->private;
  2754. bio->bi_end_io = multi->end_io;
  2755. /* only send an error to the higher layers if it is
  2756. * beyond the tolerance of the multi-bio
  2757. */
  2758. if (atomic_read(&multi->error) > multi->max_errors) {
  2759. err = -EIO;
  2760. } else if (err) {
  2761. /*
  2762. * this bio is actually up to date, we didn't
  2763. * go over the max number of errors
  2764. */
  2765. set_bit(BIO_UPTODATE, &bio->bi_flags);
  2766. err = 0;
  2767. }
  2768. kfree(multi);
  2769. bio_endio(bio, err);
  2770. } else if (!is_orig_bio) {
  2771. bio_put(bio);
  2772. }
  2773. }
  2774. struct async_sched {
  2775. struct bio *bio;
  2776. int rw;
  2777. struct btrfs_fs_info *info;
  2778. struct btrfs_work work;
  2779. };
  2780. /*
  2781. * see run_scheduled_bios for a description of why bios are collected for
  2782. * async submit.
  2783. *
  2784. * This will add one bio to the pending list for a device and make sure
  2785. * the work struct is scheduled.
  2786. */
  2787. static noinline int schedule_bio(struct btrfs_root *root,
  2788. struct btrfs_device *device,
  2789. int rw, struct bio *bio)
  2790. {
  2791. int should_queue = 1;
  2792. struct btrfs_pending_bios *pending_bios;
  2793. /* don't bother with additional async steps for reads, right now */
  2794. if (!(rw & REQ_WRITE)) {
  2795. bio_get(bio);
  2796. submit_bio(rw, bio);
  2797. bio_put(bio);
  2798. return 0;
  2799. }
  2800. /*
  2801. * nr_async_bios allows us to reliably return congestion to the
  2802. * higher layers. Otherwise, the async bio makes it appear we have
  2803. * made progress against dirty pages when we've really just put it
  2804. * on a queue for later
  2805. */
  2806. atomic_inc(&root->fs_info->nr_async_bios);
  2807. WARN_ON(bio->bi_next);
  2808. bio->bi_next = NULL;
  2809. bio->bi_rw |= rw;
  2810. spin_lock(&device->io_lock);
  2811. if (bio->bi_rw & REQ_SYNC)
  2812. pending_bios = &device->pending_sync_bios;
  2813. else
  2814. pending_bios = &device->pending_bios;
  2815. if (pending_bios->tail)
  2816. pending_bios->tail->bi_next = bio;
  2817. pending_bios->tail = bio;
  2818. if (!pending_bios->head)
  2819. pending_bios->head = bio;
  2820. if (device->running_pending)
  2821. should_queue = 0;
  2822. spin_unlock(&device->io_lock);
  2823. if (should_queue)
  2824. btrfs_queue_worker(&root->fs_info->submit_workers,
  2825. &device->work);
  2826. return 0;
  2827. }
  2828. int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
  2829. int mirror_num, int async_submit)
  2830. {
  2831. struct btrfs_mapping_tree *map_tree;
  2832. struct btrfs_device *dev;
  2833. struct bio *first_bio = bio;
  2834. u64 logical = (u64)bio->bi_sector << 9;
  2835. u64 length = 0;
  2836. u64 map_length;
  2837. struct btrfs_multi_bio *multi = NULL;
  2838. int ret;
  2839. int dev_nr = 0;
  2840. int total_devs = 1;
  2841. length = bio->bi_size;
  2842. map_tree = &root->fs_info->mapping_tree;
  2843. map_length = length;
  2844. ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
  2845. mirror_num);
  2846. BUG_ON(ret);
  2847. total_devs = multi->num_stripes;
  2848. if (map_length < length) {
  2849. printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
  2850. "len %llu\n", (unsigned long long)logical,
  2851. (unsigned long long)length,
  2852. (unsigned long long)map_length);
  2853. BUG();
  2854. }
  2855. multi->end_io = first_bio->bi_end_io;
  2856. multi->private = first_bio->bi_private;
  2857. multi->orig_bio = first_bio;
  2858. atomic_set(&multi->stripes_pending, multi->num_stripes);
  2859. while (dev_nr < total_devs) {
  2860. if (total_devs > 1) {
  2861. if (dev_nr < total_devs - 1) {
  2862. bio = bio_clone(first_bio, GFP_NOFS);
  2863. BUG_ON(!bio);
  2864. } else {
  2865. bio = first_bio;
  2866. }
  2867. bio->bi_private = multi;
  2868. bio->bi_end_io = end_bio_multi_stripe;
  2869. }
  2870. bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
  2871. dev = multi->stripes[dev_nr].dev;
  2872. if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
  2873. bio->bi_bdev = dev->bdev;
  2874. if (async_submit)
  2875. schedule_bio(root, dev, rw, bio);
  2876. else
  2877. submit_bio(rw, bio);
  2878. } else {
  2879. bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
  2880. bio->bi_sector = logical >> 9;
  2881. bio_endio(bio, -EIO);
  2882. }
  2883. dev_nr++;
  2884. }
  2885. if (total_devs == 1)
  2886. kfree(multi);
  2887. return 0;
  2888. }
  2889. struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
  2890. u8 *uuid, u8 *fsid)
  2891. {
  2892. struct btrfs_device *device;
  2893. struct btrfs_fs_devices *cur_devices;
  2894. cur_devices = root->fs_info->fs_devices;
  2895. while (cur_devices) {
  2896. if (!fsid ||
  2897. !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
  2898. device = __find_device(&cur_devices->devices,
  2899. devid, uuid);
  2900. if (device)
  2901. return device;
  2902. }
  2903. cur_devices = cur_devices->seed;
  2904. }
  2905. return NULL;
  2906. }
  2907. static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
  2908. u64 devid, u8 *dev_uuid)
  2909. {
  2910. struct btrfs_device *device;
  2911. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  2912. device = kzalloc(sizeof(*device), GFP_NOFS);
  2913. if (!device)
  2914. return NULL;
  2915. list_add(&device->dev_list,
  2916. &fs_devices->devices);
  2917. device->dev_root = root->fs_info->dev_root;
  2918. device->devid = devid;
  2919. device->work.func = pending_bios_fn;
  2920. device->fs_devices = fs_devices;
  2921. device->missing = 1;
  2922. fs_devices->num_devices++;
  2923. fs_devices->missing_devices++;
  2924. spin_lock_init(&device->io_lock);
  2925. INIT_LIST_HEAD(&device->dev_alloc_list);
  2926. memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
  2927. return device;
  2928. }
  2929. static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
  2930. struct extent_buffer *leaf,
  2931. struct btrfs_chunk *chunk)
  2932. {
  2933. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  2934. struct map_lookup *map;
  2935. struct extent_map *em;
  2936. u64 logical;
  2937. u64 length;
  2938. u64 devid;
  2939. u8 uuid[BTRFS_UUID_SIZE];
  2940. int num_stripes;
  2941. int ret;
  2942. int i;
  2943. logical = key->offset;
  2944. length = btrfs_chunk_length(leaf, chunk);
  2945. read_lock(&map_tree->map_tree.lock);
  2946. em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
  2947. read_unlock(&map_tree->map_tree.lock);
  2948. /* already mapped? */
  2949. if (em && em->start <= logical && em->start + em->len > logical) {
  2950. free_extent_map(em);
  2951. return 0;
  2952. } else if (em) {
  2953. free_extent_map(em);
  2954. }
  2955. em = alloc_extent_map();
  2956. if (!em)
  2957. return -ENOMEM;
  2958. num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  2959. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  2960. if (!map) {
  2961. free_extent_map(em);
  2962. return -ENOMEM;
  2963. }
  2964. em->bdev = (struct block_device *)map;
  2965. em->start = logical;
  2966. em->len = length;
  2967. em->block_start = 0;
  2968. em->block_len = em->len;
  2969. map->num_stripes = num_stripes;
  2970. map->io_width = btrfs_chunk_io_width(leaf, chunk);
  2971. map->io_align = btrfs_chunk_io_align(leaf, chunk);
  2972. map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
  2973. map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
  2974. map->type = btrfs_chunk_type(leaf, chunk);
  2975. map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
  2976. for (i = 0; i < num_stripes; i++) {
  2977. map->stripes[i].physical =
  2978. btrfs_stripe_offset_nr(leaf, chunk, i);
  2979. devid = btrfs_stripe_devid_nr(leaf, chunk, i);
  2980. read_extent_buffer(leaf, uuid, (unsigned long)
  2981. btrfs_stripe_dev_uuid_nr(chunk, i),
  2982. BTRFS_UUID_SIZE);
  2983. map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
  2984. NULL);
  2985. if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
  2986. kfree(map);
  2987. free_extent_map(em);
  2988. return -EIO;
  2989. }
  2990. if (!map->stripes[i].dev) {
  2991. map->stripes[i].dev =
  2992. add_missing_dev(root, devid, uuid);
  2993. if (!map->stripes[i].dev) {
  2994. kfree(map);
  2995. free_extent_map(em);
  2996. return -EIO;
  2997. }
  2998. }
  2999. map->stripes[i].dev->in_fs_metadata = 1;
  3000. }
  3001. write_lock(&map_tree->map_tree.lock);
  3002. ret = add_extent_mapping(&map_tree->map_tree, em);
  3003. write_unlock(&map_tree->map_tree.lock);
  3004. BUG_ON(ret);
  3005. free_extent_map(em);
  3006. return 0;
  3007. }
  3008. static int fill_device_from_item(struct extent_buffer *leaf,
  3009. struct btrfs_dev_item *dev_item,
  3010. struct btrfs_device *device)
  3011. {
  3012. unsigned long ptr;
  3013. device->devid = btrfs_device_id(leaf, dev_item);
  3014. device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
  3015. device->total_bytes = device->disk_total_bytes;
  3016. device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
  3017. device->type = btrfs_device_type(leaf, dev_item);
  3018. device->io_align = btrfs_device_io_align(leaf, dev_item);
  3019. device->io_width = btrfs_device_io_width(leaf, dev_item);
  3020. device->sector_size = btrfs_device_sector_size(leaf, dev_item);
  3021. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  3022. read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  3023. return 0;
  3024. }
  3025. static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
  3026. {
  3027. struct btrfs_fs_devices *fs_devices;
  3028. int ret;
  3029. mutex_lock(&uuid_mutex);
  3030. fs_devices = root->fs_info->fs_devices->seed;
  3031. while (fs_devices) {
  3032. if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
  3033. ret = 0;
  3034. goto out;
  3035. }
  3036. fs_devices = fs_devices->seed;
  3037. }
  3038. fs_devices = find_fsid(fsid);
  3039. if (!fs_devices) {
  3040. ret = -ENOENT;
  3041. goto out;
  3042. }
  3043. fs_devices = clone_fs_devices(fs_devices);
  3044. if (IS_ERR(fs_devices)) {
  3045. ret = PTR_ERR(fs_devices);
  3046. goto out;
  3047. }
  3048. ret = __btrfs_open_devices(fs_devices, FMODE_READ,
  3049. root->fs_info->bdev_holder);
  3050. if (ret)
  3051. goto out;
  3052. if (!fs_devices->seeding) {
  3053. __btrfs_close_devices(fs_devices);
  3054. free_fs_devices(fs_devices);
  3055. ret = -EINVAL;
  3056. goto out;
  3057. }
  3058. fs_devices->seed = root->fs_info->fs_devices->seed;
  3059. root->fs_info->fs_devices->seed = fs_devices;
  3060. out:
  3061. mutex_unlock(&uuid_mutex);
  3062. return ret;
  3063. }
  3064. static int read_one_dev(struct btrfs_root *root,
  3065. struct extent_buffer *leaf,
  3066. struct btrfs_dev_item *dev_item)
  3067. {
  3068. struct btrfs_device *device;
  3069. u64 devid;
  3070. int ret;
  3071. u8 fs_uuid[BTRFS_UUID_SIZE];
  3072. u8 dev_uuid[BTRFS_UUID_SIZE];
  3073. devid = btrfs_device_id(leaf, dev_item);
  3074. read_extent_buffer(leaf, dev_uuid,
  3075. (unsigned long)btrfs_device_uuid(dev_item),
  3076. BTRFS_UUID_SIZE);
  3077. read_extent_buffer(leaf, fs_uuid,
  3078. (unsigned long)btrfs_device_fsid(dev_item),
  3079. BTRFS_UUID_SIZE);
  3080. if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
  3081. ret = open_seed_devices(root, fs_uuid);
  3082. if (ret && !btrfs_test_opt(root, DEGRADED))
  3083. return ret;
  3084. }
  3085. device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
  3086. if (!device || !device->bdev) {
  3087. if (!btrfs_test_opt(root, DEGRADED))
  3088. return -EIO;
  3089. if (!device) {
  3090. printk(KERN_WARNING "warning devid %llu missing\n",
  3091. (unsigned long long)devid);
  3092. device = add_missing_dev(root, devid, dev_uuid);
  3093. if (!device)
  3094. return -ENOMEM;
  3095. } else if (!device->missing) {
  3096. /*
  3097. * this happens when a device that was properly setup
  3098. * in the device info lists suddenly goes bad.
  3099. * device->bdev is NULL, and so we have to set
  3100. * device->missing to one here
  3101. */
  3102. root->fs_info->fs_devices->missing_devices++;
  3103. device->missing = 1;
  3104. }
  3105. }
  3106. if (device->fs_devices != root->fs_info->fs_devices) {
  3107. BUG_ON(device->writeable);
  3108. if (device->generation !=
  3109. btrfs_device_generation(leaf, dev_item))
  3110. return -EINVAL;
  3111. }
  3112. fill_device_from_item(leaf, dev_item, device);
  3113. device->dev_root = root->fs_info->dev_root;
  3114. device->in_fs_metadata = 1;
  3115. if (device->writeable)
  3116. device->fs_devices->total_rw_bytes += device->total_bytes;
  3117. ret = 0;
  3118. return ret;
  3119. }
  3120. int btrfs_read_sys_array(struct btrfs_root *root)
  3121. {
  3122. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  3123. struct extent_buffer *sb;
  3124. struct btrfs_disk_key *disk_key;
  3125. struct btrfs_chunk *chunk;
  3126. u8 *ptr;
  3127. unsigned long sb_ptr;
  3128. int ret = 0;
  3129. u32 num_stripes;
  3130. u32 array_size;
  3131. u32 len = 0;
  3132. u32 cur;
  3133. struct btrfs_key key;
  3134. sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
  3135. BTRFS_SUPER_INFO_SIZE);
  3136. if (!sb)
  3137. return -ENOMEM;
  3138. btrfs_set_buffer_uptodate(sb);
  3139. btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
  3140. write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
  3141. array_size = btrfs_super_sys_array_size(super_copy);
  3142. ptr = super_copy->sys_chunk_array;
  3143. sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
  3144. cur = 0;
  3145. while (cur < array_size) {
  3146. disk_key = (struct btrfs_disk_key *)ptr;
  3147. btrfs_disk_key_to_cpu(&key, disk_key);
  3148. len = sizeof(*disk_key); ptr += len;
  3149. sb_ptr += len;
  3150. cur += len;
  3151. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  3152. chunk = (struct btrfs_chunk *)sb_ptr;
  3153. ret = read_one_chunk(root, &key, sb, chunk);
  3154. if (ret)
  3155. break;
  3156. num_stripes = btrfs_chunk_num_stripes(sb, chunk);
  3157. len = btrfs_chunk_item_size(num_stripes);
  3158. } else {
  3159. ret = -EIO;
  3160. break;
  3161. }
  3162. ptr += len;
  3163. sb_ptr += len;
  3164. cur += len;
  3165. }
  3166. free_extent_buffer(sb);
  3167. return ret;
  3168. }
  3169. int btrfs_read_chunk_tree(struct btrfs_root *root)
  3170. {
  3171. struct btrfs_path *path;
  3172. struct extent_buffer *leaf;
  3173. struct btrfs_key key;
  3174. struct btrfs_key found_key;
  3175. int ret;
  3176. int slot;
  3177. root = root->fs_info->chunk_root;
  3178. path = btrfs_alloc_path();
  3179. if (!path)
  3180. return -ENOMEM;
  3181. /* first we search for all of the device items, and then we
  3182. * read in all of the chunk items. This way we can create chunk
  3183. * mappings that reference all of the devices that are afound
  3184. */
  3185. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  3186. key.offset = 0;
  3187. key.type = 0;
  3188. again:
  3189. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3190. if (ret < 0)
  3191. goto error;
  3192. while (1) {
  3193. leaf = path->nodes[0];
  3194. slot = path->slots[0];
  3195. if (slot >= btrfs_header_nritems(leaf)) {
  3196. ret = btrfs_next_leaf(root, path);
  3197. if (ret == 0)
  3198. continue;
  3199. if (ret < 0)
  3200. goto error;
  3201. break;
  3202. }
  3203. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  3204. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  3205. if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
  3206. break;
  3207. if (found_key.type == BTRFS_DEV_ITEM_KEY) {
  3208. struct btrfs_dev_item *dev_item;
  3209. dev_item = btrfs_item_ptr(leaf, slot,
  3210. struct btrfs_dev_item);
  3211. ret = read_one_dev(root, leaf, dev_item);
  3212. if (ret)
  3213. goto error;
  3214. }
  3215. } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
  3216. struct btrfs_chunk *chunk;
  3217. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  3218. ret = read_one_chunk(root, &found_key, leaf, chunk);
  3219. if (ret)
  3220. goto error;
  3221. }
  3222. path->slots[0]++;
  3223. }
  3224. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  3225. key.objectid = 0;
  3226. btrfs_release_path(path);
  3227. goto again;
  3228. }
  3229. ret = 0;
  3230. error:
  3231. btrfs_free_path(path);
  3232. return ret;
  3233. }