volumes.c 81 KB

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