volumes.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560
  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 <asm/div64.h>
  24. #include "ctree.h"
  25. #include "extent_map.h"
  26. #include "disk-io.h"
  27. #include "transaction.h"
  28. #include "print-tree.h"
  29. #include "volumes.h"
  30. #include "async-thread.h"
  31. struct map_lookup {
  32. u64 type;
  33. int io_align;
  34. int io_width;
  35. int stripe_len;
  36. int sector_size;
  37. int num_stripes;
  38. int sub_stripes;
  39. struct btrfs_bio_stripe stripes[];
  40. };
  41. #define map_lookup_size(n) (sizeof(struct map_lookup) + \
  42. (sizeof(struct btrfs_bio_stripe) * (n)))
  43. static DEFINE_MUTEX(uuid_mutex);
  44. static LIST_HEAD(fs_uuids);
  45. void btrfs_lock_volumes(void)
  46. {
  47. mutex_lock(&uuid_mutex);
  48. }
  49. void btrfs_unlock_volumes(void)
  50. {
  51. mutex_unlock(&uuid_mutex);
  52. }
  53. static void lock_chunks(struct btrfs_root *root)
  54. {
  55. mutex_lock(&root->fs_info->alloc_mutex);
  56. mutex_lock(&root->fs_info->chunk_mutex);
  57. }
  58. static void unlock_chunks(struct btrfs_root *root)
  59. {
  60. mutex_unlock(&root->fs_info->alloc_mutex);
  61. mutex_unlock(&root->fs_info->chunk_mutex);
  62. }
  63. int btrfs_cleanup_fs_uuids(void)
  64. {
  65. struct btrfs_fs_devices *fs_devices;
  66. struct list_head *uuid_cur;
  67. struct list_head *devices_cur;
  68. struct btrfs_device *dev;
  69. list_for_each(uuid_cur, &fs_uuids) {
  70. fs_devices = list_entry(uuid_cur, struct btrfs_fs_devices,
  71. list);
  72. while(!list_empty(&fs_devices->devices)) {
  73. devices_cur = fs_devices->devices.next;
  74. dev = list_entry(devices_cur, struct btrfs_device,
  75. dev_list);
  76. if (dev->bdev) {
  77. close_bdev_excl(dev->bdev);
  78. fs_devices->open_devices--;
  79. }
  80. list_del(&dev->dev_list);
  81. kfree(dev->name);
  82. kfree(dev);
  83. }
  84. }
  85. return 0;
  86. }
  87. static noinline struct btrfs_device *__find_device(struct list_head *head,
  88. u64 devid, u8 *uuid)
  89. {
  90. struct btrfs_device *dev;
  91. struct list_head *cur;
  92. list_for_each(cur, head) {
  93. dev = list_entry(cur, struct btrfs_device, dev_list);
  94. if (dev->devid == devid &&
  95. (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
  96. return dev;
  97. }
  98. }
  99. return NULL;
  100. }
  101. static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
  102. {
  103. struct list_head *cur;
  104. struct btrfs_fs_devices *fs_devices;
  105. list_for_each(cur, &fs_uuids) {
  106. fs_devices = list_entry(cur, struct btrfs_fs_devices, 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 int noinline 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. bdi = device->bdev->bd_inode->i_mapping->backing_dev_info;
  134. fs_info = device->dev_root->fs_info;
  135. limit = btrfs_async_submit_limit(fs_info);
  136. limit = limit * 2 / 3;
  137. loop:
  138. spin_lock(&device->io_lock);
  139. /* take all the bios off the list at once and process them
  140. * later on (without the lock held). But, remember the
  141. * tail and other pointers so the bios can be properly reinserted
  142. * into the list if we hit congestion
  143. */
  144. pending = device->pending_bios;
  145. tail = device->pending_bio_tail;
  146. WARN_ON(pending && !tail);
  147. device->pending_bios = NULL;
  148. device->pending_bio_tail = NULL;
  149. /*
  150. * if pending was null this time around, no bios need processing
  151. * at all and we can stop. Otherwise it'll loop back up again
  152. * and do an additional check so no bios are missed.
  153. *
  154. * device->running_pending is used to synchronize with the
  155. * schedule_bio code.
  156. */
  157. if (pending) {
  158. again = 1;
  159. device->running_pending = 1;
  160. } else {
  161. again = 0;
  162. device->running_pending = 0;
  163. }
  164. spin_unlock(&device->io_lock);
  165. while(pending) {
  166. cur = pending;
  167. pending = pending->bi_next;
  168. cur->bi_next = NULL;
  169. atomic_dec(&fs_info->nr_async_bios);
  170. if (atomic_read(&fs_info->nr_async_bios) < limit &&
  171. waitqueue_active(&fs_info->async_submit_wait))
  172. wake_up(&fs_info->async_submit_wait);
  173. BUG_ON(atomic_read(&cur->bi_cnt) == 0);
  174. bio_get(cur);
  175. submit_bio(cur->bi_rw, cur);
  176. bio_put(cur);
  177. num_run++;
  178. /*
  179. * we made progress, there is more work to do and the bdi
  180. * is now congested. Back off and let other work structs
  181. * run instead
  182. */
  183. if (pending && bdi_write_congested(bdi)) {
  184. struct bio *old_head;
  185. spin_lock(&device->io_lock);
  186. old_head = device->pending_bios;
  187. device->pending_bios = pending;
  188. if (device->pending_bio_tail)
  189. tail->bi_next = old_head;
  190. else
  191. device->pending_bio_tail = tail;
  192. spin_unlock(&device->io_lock);
  193. btrfs_requeue_work(&device->work);
  194. goto done;
  195. }
  196. }
  197. if (again)
  198. goto loop;
  199. done:
  200. return 0;
  201. }
  202. void pending_bios_fn(struct btrfs_work *work)
  203. {
  204. struct btrfs_device *device;
  205. device = container_of(work, struct btrfs_device, work);
  206. run_scheduled_bios(device);
  207. }
  208. static noinline int device_list_add(const char *path,
  209. struct btrfs_super_block *disk_super,
  210. u64 devid, struct btrfs_fs_devices **fs_devices_ret)
  211. {
  212. struct btrfs_device *device;
  213. struct btrfs_fs_devices *fs_devices;
  214. u64 found_transid = btrfs_super_generation(disk_super);
  215. fs_devices = find_fsid(disk_super->fsid);
  216. if (!fs_devices) {
  217. fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
  218. if (!fs_devices)
  219. return -ENOMEM;
  220. INIT_LIST_HEAD(&fs_devices->devices);
  221. INIT_LIST_HEAD(&fs_devices->alloc_list);
  222. list_add(&fs_devices->list, &fs_uuids);
  223. memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
  224. fs_devices->latest_devid = devid;
  225. fs_devices->latest_trans = found_transid;
  226. device = NULL;
  227. } else {
  228. device = __find_device(&fs_devices->devices, devid,
  229. disk_super->dev_item.uuid);
  230. }
  231. if (!device) {
  232. device = kzalloc(sizeof(*device), GFP_NOFS);
  233. if (!device) {
  234. /* we can safely leave the fs_devices entry around */
  235. return -ENOMEM;
  236. }
  237. device->devid = devid;
  238. device->work.func = pending_bios_fn;
  239. memcpy(device->uuid, disk_super->dev_item.uuid,
  240. BTRFS_UUID_SIZE);
  241. device->barriers = 1;
  242. spin_lock_init(&device->io_lock);
  243. device->name = kstrdup(path, GFP_NOFS);
  244. if (!device->name) {
  245. kfree(device);
  246. return -ENOMEM;
  247. }
  248. list_add(&device->dev_list, &fs_devices->devices);
  249. list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
  250. fs_devices->num_devices++;
  251. }
  252. if (found_transid > fs_devices->latest_trans) {
  253. fs_devices->latest_devid = devid;
  254. fs_devices->latest_trans = found_transid;
  255. }
  256. *fs_devices_ret = fs_devices;
  257. return 0;
  258. }
  259. int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
  260. {
  261. struct list_head *head = &fs_devices->devices;
  262. struct list_head *cur;
  263. struct btrfs_device *device;
  264. mutex_lock(&uuid_mutex);
  265. again:
  266. list_for_each(cur, head) {
  267. device = list_entry(cur, struct btrfs_device, dev_list);
  268. if (!device->in_fs_metadata) {
  269. struct block_device *bdev;
  270. list_del(&device->dev_list);
  271. list_del(&device->dev_alloc_list);
  272. fs_devices->num_devices--;
  273. if (device->bdev) {
  274. bdev = device->bdev;
  275. fs_devices->open_devices--;
  276. mutex_unlock(&uuid_mutex);
  277. close_bdev_excl(bdev);
  278. mutex_lock(&uuid_mutex);
  279. }
  280. kfree(device->name);
  281. kfree(device);
  282. goto again;
  283. }
  284. }
  285. mutex_unlock(&uuid_mutex);
  286. return 0;
  287. }
  288. int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
  289. {
  290. struct list_head *head = &fs_devices->devices;
  291. struct list_head *cur;
  292. struct btrfs_device *device;
  293. mutex_lock(&uuid_mutex);
  294. list_for_each(cur, head) {
  295. device = list_entry(cur, struct btrfs_device, dev_list);
  296. if (device->bdev) {
  297. close_bdev_excl(device->bdev);
  298. fs_devices->open_devices--;
  299. }
  300. device->bdev = NULL;
  301. device->in_fs_metadata = 0;
  302. }
  303. fs_devices->mounted = 0;
  304. mutex_unlock(&uuid_mutex);
  305. return 0;
  306. }
  307. int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
  308. int flags, void *holder)
  309. {
  310. struct block_device *bdev;
  311. struct list_head *head = &fs_devices->devices;
  312. struct list_head *cur;
  313. struct btrfs_device *device;
  314. struct block_device *latest_bdev = NULL;
  315. struct buffer_head *bh;
  316. struct btrfs_super_block *disk_super;
  317. u64 latest_devid = 0;
  318. u64 latest_transid = 0;
  319. u64 transid;
  320. u64 devid;
  321. int ret = 0;
  322. mutex_lock(&uuid_mutex);
  323. if (fs_devices->mounted)
  324. goto out;
  325. list_for_each(cur, head) {
  326. device = list_entry(cur, struct btrfs_device, dev_list);
  327. if (device->bdev)
  328. continue;
  329. if (!device->name)
  330. continue;
  331. bdev = open_bdev_excl(device->name, flags, holder);
  332. if (IS_ERR(bdev)) {
  333. printk("open %s failed\n", device->name);
  334. goto error;
  335. }
  336. set_blocksize(bdev, 4096);
  337. bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
  338. if (!bh)
  339. goto error_close;
  340. disk_super = (struct btrfs_super_block *)bh->b_data;
  341. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  342. sizeof(disk_super->magic)))
  343. goto error_brelse;
  344. devid = le64_to_cpu(disk_super->dev_item.devid);
  345. if (devid != device->devid)
  346. goto error_brelse;
  347. transid = btrfs_super_generation(disk_super);
  348. if (!latest_transid || transid > latest_transid) {
  349. latest_devid = devid;
  350. latest_transid = transid;
  351. latest_bdev = bdev;
  352. }
  353. device->bdev = bdev;
  354. device->in_fs_metadata = 0;
  355. fs_devices->open_devices++;
  356. continue;
  357. error_brelse:
  358. brelse(bh);
  359. error_close:
  360. close_bdev_excl(bdev);
  361. error:
  362. continue;
  363. }
  364. if (fs_devices->open_devices == 0) {
  365. ret = -EIO;
  366. goto out;
  367. }
  368. fs_devices->mounted = 1;
  369. fs_devices->latest_bdev = latest_bdev;
  370. fs_devices->latest_devid = latest_devid;
  371. fs_devices->latest_trans = latest_transid;
  372. out:
  373. mutex_unlock(&uuid_mutex);
  374. return ret;
  375. }
  376. int btrfs_scan_one_device(const char *path, int flags, void *holder,
  377. struct btrfs_fs_devices **fs_devices_ret)
  378. {
  379. struct btrfs_super_block *disk_super;
  380. struct block_device *bdev;
  381. struct buffer_head *bh;
  382. int ret;
  383. u64 devid;
  384. u64 transid;
  385. mutex_lock(&uuid_mutex);
  386. bdev = open_bdev_excl(path, flags, holder);
  387. if (IS_ERR(bdev)) {
  388. ret = PTR_ERR(bdev);
  389. goto error;
  390. }
  391. ret = set_blocksize(bdev, 4096);
  392. if (ret)
  393. goto error_close;
  394. bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
  395. if (!bh) {
  396. ret = -EIO;
  397. goto error_close;
  398. }
  399. disk_super = (struct btrfs_super_block *)bh->b_data;
  400. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  401. sizeof(disk_super->magic))) {
  402. ret = -EINVAL;
  403. goto error_brelse;
  404. }
  405. devid = le64_to_cpu(disk_super->dev_item.devid);
  406. transid = btrfs_super_generation(disk_super);
  407. if (disk_super->label[0])
  408. printk("device label %s ", disk_super->label);
  409. else {
  410. /* FIXME, make a readl uuid parser */
  411. printk("device fsid %llx-%llx ",
  412. *(unsigned long long *)disk_super->fsid,
  413. *(unsigned long long *)(disk_super->fsid + 8));
  414. }
  415. printk("devid %Lu transid %Lu %s\n", devid, transid, path);
  416. ret = device_list_add(path, disk_super, devid, fs_devices_ret);
  417. error_brelse:
  418. brelse(bh);
  419. error_close:
  420. close_bdev_excl(bdev);
  421. error:
  422. mutex_unlock(&uuid_mutex);
  423. return ret;
  424. }
  425. /*
  426. * this uses a pretty simple search, the expectation is that it is
  427. * called very infrequently and that a given device has a small number
  428. * of extents
  429. */
  430. static noinline int find_free_dev_extent(struct btrfs_trans_handle *trans,
  431. struct btrfs_device *device,
  432. struct btrfs_path *path,
  433. u64 num_bytes, u64 *start)
  434. {
  435. struct btrfs_key key;
  436. struct btrfs_root *root = device->dev_root;
  437. struct btrfs_dev_extent *dev_extent = NULL;
  438. u64 hole_size = 0;
  439. u64 last_byte = 0;
  440. u64 search_start = 0;
  441. u64 search_end = device->total_bytes;
  442. int ret;
  443. int slot = 0;
  444. int start_found;
  445. struct extent_buffer *l;
  446. start_found = 0;
  447. path->reada = 2;
  448. /* FIXME use last free of some kind */
  449. /* we don't want to overwrite the superblock on the drive,
  450. * so we make sure to start at an offset of at least 1MB
  451. */
  452. search_start = max((u64)1024 * 1024, search_start);
  453. if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
  454. search_start = max(root->fs_info->alloc_start, search_start);
  455. key.objectid = device->devid;
  456. key.offset = search_start;
  457. key.type = BTRFS_DEV_EXTENT_KEY;
  458. ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
  459. if (ret < 0)
  460. goto error;
  461. ret = btrfs_previous_item(root, path, 0, key.type);
  462. if (ret < 0)
  463. goto error;
  464. l = path->nodes[0];
  465. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  466. while (1) {
  467. l = path->nodes[0];
  468. slot = path->slots[0];
  469. if (slot >= btrfs_header_nritems(l)) {
  470. ret = btrfs_next_leaf(root, path);
  471. if (ret == 0)
  472. continue;
  473. if (ret < 0)
  474. goto error;
  475. no_more_items:
  476. if (!start_found) {
  477. if (search_start >= search_end) {
  478. ret = -ENOSPC;
  479. goto error;
  480. }
  481. *start = search_start;
  482. start_found = 1;
  483. goto check_pending;
  484. }
  485. *start = last_byte > search_start ?
  486. last_byte : search_start;
  487. if (search_end <= *start) {
  488. ret = -ENOSPC;
  489. goto error;
  490. }
  491. goto check_pending;
  492. }
  493. btrfs_item_key_to_cpu(l, &key, slot);
  494. if (key.objectid < device->devid)
  495. goto next;
  496. if (key.objectid > device->devid)
  497. goto no_more_items;
  498. if (key.offset >= search_start && key.offset > last_byte &&
  499. start_found) {
  500. if (last_byte < search_start)
  501. last_byte = search_start;
  502. hole_size = key.offset - last_byte;
  503. if (key.offset > last_byte &&
  504. hole_size >= num_bytes) {
  505. *start = last_byte;
  506. goto check_pending;
  507. }
  508. }
  509. if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
  510. goto next;
  511. }
  512. start_found = 1;
  513. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  514. last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
  515. next:
  516. path->slots[0]++;
  517. cond_resched();
  518. }
  519. check_pending:
  520. /* we have to make sure we didn't find an extent that has already
  521. * been allocated by the map tree or the original allocation
  522. */
  523. btrfs_release_path(root, path);
  524. BUG_ON(*start < search_start);
  525. if (*start + num_bytes > search_end) {
  526. ret = -ENOSPC;
  527. goto error;
  528. }
  529. /* check for pending inserts here */
  530. return 0;
  531. error:
  532. btrfs_release_path(root, path);
  533. return ret;
  534. }
  535. int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
  536. struct btrfs_device *device,
  537. u64 start)
  538. {
  539. int ret;
  540. struct btrfs_path *path;
  541. struct btrfs_root *root = device->dev_root;
  542. struct btrfs_key key;
  543. struct btrfs_key found_key;
  544. struct extent_buffer *leaf = NULL;
  545. struct btrfs_dev_extent *extent = NULL;
  546. path = btrfs_alloc_path();
  547. if (!path)
  548. return -ENOMEM;
  549. key.objectid = device->devid;
  550. key.offset = start;
  551. key.type = BTRFS_DEV_EXTENT_KEY;
  552. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  553. if (ret > 0) {
  554. ret = btrfs_previous_item(root, path, key.objectid,
  555. BTRFS_DEV_EXTENT_KEY);
  556. BUG_ON(ret);
  557. leaf = path->nodes[0];
  558. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  559. extent = btrfs_item_ptr(leaf, path->slots[0],
  560. struct btrfs_dev_extent);
  561. BUG_ON(found_key.offset > start || found_key.offset +
  562. btrfs_dev_extent_length(leaf, extent) < start);
  563. ret = 0;
  564. } else if (ret == 0) {
  565. leaf = path->nodes[0];
  566. extent = btrfs_item_ptr(leaf, path->slots[0],
  567. struct btrfs_dev_extent);
  568. }
  569. BUG_ON(ret);
  570. if (device->bytes_used > 0)
  571. device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
  572. ret = btrfs_del_item(trans, root, path);
  573. BUG_ON(ret);
  574. btrfs_free_path(path);
  575. return ret;
  576. }
  577. int noinline btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
  578. struct btrfs_device *device,
  579. u64 chunk_tree, u64 chunk_objectid,
  580. u64 chunk_offset,
  581. u64 num_bytes, u64 *start)
  582. {
  583. int ret;
  584. struct btrfs_path *path;
  585. struct btrfs_root *root = device->dev_root;
  586. struct btrfs_dev_extent *extent;
  587. struct extent_buffer *leaf;
  588. struct btrfs_key key;
  589. WARN_ON(!device->in_fs_metadata);
  590. path = btrfs_alloc_path();
  591. if (!path)
  592. return -ENOMEM;
  593. ret = find_free_dev_extent(trans, device, path, num_bytes, start);
  594. if (ret) {
  595. goto err;
  596. }
  597. key.objectid = device->devid;
  598. key.offset = *start;
  599. key.type = BTRFS_DEV_EXTENT_KEY;
  600. ret = btrfs_insert_empty_item(trans, root, path, &key,
  601. sizeof(*extent));
  602. BUG_ON(ret);
  603. leaf = path->nodes[0];
  604. extent = btrfs_item_ptr(leaf, path->slots[0],
  605. struct btrfs_dev_extent);
  606. btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
  607. btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
  608. btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
  609. write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
  610. (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
  611. BTRFS_UUID_SIZE);
  612. btrfs_set_dev_extent_length(leaf, extent, num_bytes);
  613. btrfs_mark_buffer_dirty(leaf);
  614. err:
  615. btrfs_free_path(path);
  616. return ret;
  617. }
  618. static noinline int find_next_chunk(struct btrfs_root *root,
  619. u64 objectid, u64 *offset)
  620. {
  621. struct btrfs_path *path;
  622. int ret;
  623. struct btrfs_key key;
  624. struct btrfs_chunk *chunk;
  625. struct btrfs_key found_key;
  626. path = btrfs_alloc_path();
  627. BUG_ON(!path);
  628. key.objectid = objectid;
  629. key.offset = (u64)-1;
  630. key.type = BTRFS_CHUNK_ITEM_KEY;
  631. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  632. if (ret < 0)
  633. goto error;
  634. BUG_ON(ret == 0);
  635. ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
  636. if (ret) {
  637. *offset = 0;
  638. } else {
  639. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  640. path->slots[0]);
  641. if (found_key.objectid != objectid)
  642. *offset = 0;
  643. else {
  644. chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
  645. struct btrfs_chunk);
  646. *offset = found_key.offset +
  647. btrfs_chunk_length(path->nodes[0], chunk);
  648. }
  649. }
  650. ret = 0;
  651. error:
  652. btrfs_free_path(path);
  653. return ret;
  654. }
  655. static noinline int find_next_devid(struct btrfs_root *root,
  656. struct btrfs_path *path, u64 *objectid)
  657. {
  658. int ret;
  659. struct btrfs_key key;
  660. struct btrfs_key found_key;
  661. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  662. key.type = BTRFS_DEV_ITEM_KEY;
  663. key.offset = (u64)-1;
  664. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  665. if (ret < 0)
  666. goto error;
  667. BUG_ON(ret == 0);
  668. ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
  669. BTRFS_DEV_ITEM_KEY);
  670. if (ret) {
  671. *objectid = 1;
  672. } else {
  673. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  674. path->slots[0]);
  675. *objectid = found_key.offset + 1;
  676. }
  677. ret = 0;
  678. error:
  679. btrfs_release_path(root, path);
  680. return ret;
  681. }
  682. /*
  683. * the device information is stored in the chunk root
  684. * the btrfs_device struct should be fully filled in
  685. */
  686. int btrfs_add_device(struct btrfs_trans_handle *trans,
  687. struct btrfs_root *root,
  688. struct btrfs_device *device)
  689. {
  690. int ret;
  691. struct btrfs_path *path;
  692. struct btrfs_dev_item *dev_item;
  693. struct extent_buffer *leaf;
  694. struct btrfs_key key;
  695. unsigned long ptr;
  696. u64 free_devid = 0;
  697. root = root->fs_info->chunk_root;
  698. path = btrfs_alloc_path();
  699. if (!path)
  700. return -ENOMEM;
  701. ret = find_next_devid(root, path, &free_devid);
  702. if (ret)
  703. goto out;
  704. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  705. key.type = BTRFS_DEV_ITEM_KEY;
  706. key.offset = free_devid;
  707. ret = btrfs_insert_empty_item(trans, root, path, &key,
  708. sizeof(*dev_item));
  709. if (ret)
  710. goto out;
  711. leaf = path->nodes[0];
  712. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  713. device->devid = free_devid;
  714. btrfs_set_device_id(leaf, dev_item, device->devid);
  715. btrfs_set_device_type(leaf, dev_item, device->type);
  716. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  717. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  718. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  719. btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
  720. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  721. btrfs_set_device_group(leaf, dev_item, 0);
  722. btrfs_set_device_seek_speed(leaf, dev_item, 0);
  723. btrfs_set_device_bandwidth(leaf, dev_item, 0);
  724. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  725. write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  726. btrfs_mark_buffer_dirty(leaf);
  727. ret = 0;
  728. out:
  729. btrfs_free_path(path);
  730. return ret;
  731. }
  732. static int btrfs_rm_dev_item(struct btrfs_root *root,
  733. struct btrfs_device *device)
  734. {
  735. int ret;
  736. struct btrfs_path *path;
  737. struct block_device *bdev = device->bdev;
  738. struct btrfs_device *next_dev;
  739. struct btrfs_key key;
  740. u64 total_bytes;
  741. struct btrfs_fs_devices *fs_devices;
  742. struct btrfs_trans_handle *trans;
  743. root = root->fs_info->chunk_root;
  744. path = btrfs_alloc_path();
  745. if (!path)
  746. return -ENOMEM;
  747. trans = btrfs_start_transaction(root, 1);
  748. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  749. key.type = BTRFS_DEV_ITEM_KEY;
  750. key.offset = device->devid;
  751. lock_chunks(root);
  752. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  753. if (ret < 0)
  754. goto out;
  755. if (ret > 0) {
  756. ret = -ENOENT;
  757. goto out;
  758. }
  759. ret = btrfs_del_item(trans, root, path);
  760. if (ret)
  761. goto out;
  762. /*
  763. * at this point, the device is zero sized. We want to
  764. * remove it from the devices list and zero out the old super
  765. */
  766. list_del_init(&device->dev_list);
  767. list_del_init(&device->dev_alloc_list);
  768. fs_devices = root->fs_info->fs_devices;
  769. next_dev = list_entry(fs_devices->devices.next, struct btrfs_device,
  770. dev_list);
  771. if (bdev == root->fs_info->sb->s_bdev)
  772. root->fs_info->sb->s_bdev = next_dev->bdev;
  773. if (bdev == fs_devices->latest_bdev)
  774. fs_devices->latest_bdev = next_dev->bdev;
  775. total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
  776. btrfs_set_super_num_devices(&root->fs_info->super_copy,
  777. total_bytes - 1);
  778. out:
  779. btrfs_free_path(path);
  780. unlock_chunks(root);
  781. btrfs_commit_transaction(trans, root);
  782. return ret;
  783. }
  784. int btrfs_rm_device(struct btrfs_root *root, char *device_path)
  785. {
  786. struct btrfs_device *device;
  787. struct block_device *bdev;
  788. struct buffer_head *bh = NULL;
  789. struct btrfs_super_block *disk_super;
  790. u64 all_avail;
  791. u64 devid;
  792. int ret = 0;
  793. mutex_lock(&uuid_mutex);
  794. mutex_lock(&root->fs_info->volume_mutex);
  795. all_avail = root->fs_info->avail_data_alloc_bits |
  796. root->fs_info->avail_system_alloc_bits |
  797. root->fs_info->avail_metadata_alloc_bits;
  798. if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
  799. btrfs_super_num_devices(&root->fs_info->super_copy) <= 4) {
  800. printk("btrfs: unable to go below four devices on raid10\n");
  801. ret = -EINVAL;
  802. goto out;
  803. }
  804. if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
  805. btrfs_super_num_devices(&root->fs_info->super_copy) <= 2) {
  806. printk("btrfs: unable to go below two devices on raid1\n");
  807. ret = -EINVAL;
  808. goto out;
  809. }
  810. if (strcmp(device_path, "missing") == 0) {
  811. struct list_head *cur;
  812. struct list_head *devices;
  813. struct btrfs_device *tmp;
  814. device = NULL;
  815. devices = &root->fs_info->fs_devices->devices;
  816. list_for_each(cur, devices) {
  817. tmp = list_entry(cur, struct btrfs_device, dev_list);
  818. if (tmp->in_fs_metadata && !tmp->bdev) {
  819. device = tmp;
  820. break;
  821. }
  822. }
  823. bdev = NULL;
  824. bh = NULL;
  825. disk_super = NULL;
  826. if (!device) {
  827. printk("btrfs: no missing devices found to remove\n");
  828. goto out;
  829. }
  830. } else {
  831. bdev = open_bdev_excl(device_path, 0,
  832. root->fs_info->bdev_holder);
  833. if (IS_ERR(bdev)) {
  834. ret = PTR_ERR(bdev);
  835. goto out;
  836. }
  837. bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
  838. if (!bh) {
  839. ret = -EIO;
  840. goto error_close;
  841. }
  842. disk_super = (struct btrfs_super_block *)bh->b_data;
  843. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  844. sizeof(disk_super->magic))) {
  845. ret = -ENOENT;
  846. goto error_brelse;
  847. }
  848. if (memcmp(disk_super->fsid, root->fs_info->fsid,
  849. BTRFS_FSID_SIZE)) {
  850. ret = -ENOENT;
  851. goto error_brelse;
  852. }
  853. devid = le64_to_cpu(disk_super->dev_item.devid);
  854. device = btrfs_find_device(root, devid, NULL);
  855. if (!device) {
  856. ret = -ENOENT;
  857. goto error_brelse;
  858. }
  859. }
  860. root->fs_info->fs_devices->num_devices--;
  861. root->fs_info->fs_devices->open_devices--;
  862. ret = btrfs_shrink_device(device, 0);
  863. if (ret)
  864. goto error_brelse;
  865. ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
  866. if (ret)
  867. goto error_brelse;
  868. if (bh) {
  869. /* make sure this device isn't detected as part of
  870. * the FS anymore
  871. */
  872. memset(&disk_super->magic, 0, sizeof(disk_super->magic));
  873. set_buffer_dirty(bh);
  874. sync_dirty_buffer(bh);
  875. brelse(bh);
  876. }
  877. if (device->bdev) {
  878. /* one close for the device struct or super_block */
  879. close_bdev_excl(device->bdev);
  880. }
  881. if (bdev) {
  882. /* one close for us */
  883. close_bdev_excl(bdev);
  884. }
  885. kfree(device->name);
  886. kfree(device);
  887. ret = 0;
  888. goto out;
  889. error_brelse:
  890. brelse(bh);
  891. error_close:
  892. if (bdev)
  893. close_bdev_excl(bdev);
  894. out:
  895. mutex_unlock(&root->fs_info->volume_mutex);
  896. mutex_unlock(&uuid_mutex);
  897. return ret;
  898. }
  899. int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
  900. {
  901. struct btrfs_trans_handle *trans;
  902. struct btrfs_device *device;
  903. struct block_device *bdev;
  904. struct list_head *cur;
  905. struct list_head *devices;
  906. u64 total_bytes;
  907. int ret = 0;
  908. bdev = open_bdev_excl(device_path, 0, root->fs_info->bdev_holder);
  909. if (!bdev) {
  910. return -EIO;
  911. }
  912. mutex_lock(&root->fs_info->volume_mutex);
  913. trans = btrfs_start_transaction(root, 1);
  914. lock_chunks(root);
  915. devices = &root->fs_info->fs_devices->devices;
  916. list_for_each(cur, devices) {
  917. device = list_entry(cur, struct btrfs_device, dev_list);
  918. if (device->bdev == bdev) {
  919. ret = -EEXIST;
  920. goto out;
  921. }
  922. }
  923. device = kzalloc(sizeof(*device), GFP_NOFS);
  924. if (!device) {
  925. /* we can safely leave the fs_devices entry around */
  926. ret = -ENOMEM;
  927. goto out_close_bdev;
  928. }
  929. device->barriers = 1;
  930. device->work.func = pending_bios_fn;
  931. generate_random_uuid(device->uuid);
  932. spin_lock_init(&device->io_lock);
  933. device->name = kstrdup(device_path, GFP_NOFS);
  934. if (!device->name) {
  935. kfree(device);
  936. goto out_close_bdev;
  937. }
  938. device->io_width = root->sectorsize;
  939. device->io_align = root->sectorsize;
  940. device->sector_size = root->sectorsize;
  941. device->total_bytes = i_size_read(bdev->bd_inode);
  942. device->dev_root = root->fs_info->dev_root;
  943. device->bdev = bdev;
  944. device->in_fs_metadata = 1;
  945. ret = btrfs_add_device(trans, root, device);
  946. if (ret)
  947. goto out_close_bdev;
  948. set_blocksize(device->bdev, 4096);
  949. total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
  950. btrfs_set_super_total_bytes(&root->fs_info->super_copy,
  951. total_bytes + device->total_bytes);
  952. total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
  953. btrfs_set_super_num_devices(&root->fs_info->super_copy,
  954. total_bytes + 1);
  955. list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
  956. list_add(&device->dev_alloc_list,
  957. &root->fs_info->fs_devices->alloc_list);
  958. root->fs_info->fs_devices->num_devices++;
  959. root->fs_info->fs_devices->open_devices++;
  960. out:
  961. unlock_chunks(root);
  962. btrfs_end_transaction(trans, root);
  963. mutex_unlock(&root->fs_info->volume_mutex);
  964. return ret;
  965. out_close_bdev:
  966. close_bdev_excl(bdev);
  967. goto out;
  968. }
  969. int noinline btrfs_update_device(struct btrfs_trans_handle *trans,
  970. struct btrfs_device *device)
  971. {
  972. int ret;
  973. struct btrfs_path *path;
  974. struct btrfs_root *root;
  975. struct btrfs_dev_item *dev_item;
  976. struct extent_buffer *leaf;
  977. struct btrfs_key key;
  978. root = device->dev_root->fs_info->chunk_root;
  979. path = btrfs_alloc_path();
  980. if (!path)
  981. return -ENOMEM;
  982. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  983. key.type = BTRFS_DEV_ITEM_KEY;
  984. key.offset = device->devid;
  985. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  986. if (ret < 0)
  987. goto out;
  988. if (ret > 0) {
  989. ret = -ENOENT;
  990. goto out;
  991. }
  992. leaf = path->nodes[0];
  993. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  994. btrfs_set_device_id(leaf, dev_item, device->devid);
  995. btrfs_set_device_type(leaf, dev_item, device->type);
  996. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  997. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  998. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  999. btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
  1000. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  1001. btrfs_mark_buffer_dirty(leaf);
  1002. out:
  1003. btrfs_free_path(path);
  1004. return ret;
  1005. }
  1006. static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
  1007. struct btrfs_device *device, u64 new_size)
  1008. {
  1009. struct btrfs_super_block *super_copy =
  1010. &device->dev_root->fs_info->super_copy;
  1011. u64 old_total = btrfs_super_total_bytes(super_copy);
  1012. u64 diff = new_size - device->total_bytes;
  1013. btrfs_set_super_total_bytes(super_copy, old_total + diff);
  1014. return btrfs_update_device(trans, device);
  1015. }
  1016. int btrfs_grow_device(struct btrfs_trans_handle *trans,
  1017. struct btrfs_device *device, u64 new_size)
  1018. {
  1019. int ret;
  1020. lock_chunks(device->dev_root);
  1021. ret = __btrfs_grow_device(trans, device, new_size);
  1022. unlock_chunks(device->dev_root);
  1023. return ret;
  1024. }
  1025. static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
  1026. struct btrfs_root *root,
  1027. u64 chunk_tree, u64 chunk_objectid,
  1028. u64 chunk_offset)
  1029. {
  1030. int ret;
  1031. struct btrfs_path *path;
  1032. struct btrfs_key key;
  1033. root = root->fs_info->chunk_root;
  1034. path = btrfs_alloc_path();
  1035. if (!path)
  1036. return -ENOMEM;
  1037. key.objectid = chunk_objectid;
  1038. key.offset = chunk_offset;
  1039. key.type = BTRFS_CHUNK_ITEM_KEY;
  1040. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1041. BUG_ON(ret);
  1042. ret = btrfs_del_item(trans, root, path);
  1043. BUG_ON(ret);
  1044. btrfs_free_path(path);
  1045. return 0;
  1046. }
  1047. int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
  1048. chunk_offset)
  1049. {
  1050. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  1051. struct btrfs_disk_key *disk_key;
  1052. struct btrfs_chunk *chunk;
  1053. u8 *ptr;
  1054. int ret = 0;
  1055. u32 num_stripes;
  1056. u32 array_size;
  1057. u32 len = 0;
  1058. u32 cur;
  1059. struct btrfs_key key;
  1060. array_size = btrfs_super_sys_array_size(super_copy);
  1061. ptr = super_copy->sys_chunk_array;
  1062. cur = 0;
  1063. while (cur < array_size) {
  1064. disk_key = (struct btrfs_disk_key *)ptr;
  1065. btrfs_disk_key_to_cpu(&key, disk_key);
  1066. len = sizeof(*disk_key);
  1067. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  1068. chunk = (struct btrfs_chunk *)(ptr + len);
  1069. num_stripes = btrfs_stack_chunk_num_stripes(chunk);
  1070. len += btrfs_chunk_item_size(num_stripes);
  1071. } else {
  1072. ret = -EIO;
  1073. break;
  1074. }
  1075. if (key.objectid == chunk_objectid &&
  1076. key.offset == chunk_offset) {
  1077. memmove(ptr, ptr + len, array_size - (cur + len));
  1078. array_size -= len;
  1079. btrfs_set_super_sys_array_size(super_copy, array_size);
  1080. } else {
  1081. ptr += len;
  1082. cur += len;
  1083. }
  1084. }
  1085. return ret;
  1086. }
  1087. int btrfs_relocate_chunk(struct btrfs_root *root,
  1088. u64 chunk_tree, u64 chunk_objectid,
  1089. u64 chunk_offset)
  1090. {
  1091. struct extent_map_tree *em_tree;
  1092. struct btrfs_root *extent_root;
  1093. struct btrfs_trans_handle *trans;
  1094. struct extent_map *em;
  1095. struct map_lookup *map;
  1096. int ret;
  1097. int i;
  1098. printk("btrfs relocating chunk %llu\n",
  1099. (unsigned long long)chunk_offset);
  1100. root = root->fs_info->chunk_root;
  1101. extent_root = root->fs_info->extent_root;
  1102. em_tree = &root->fs_info->mapping_tree.map_tree;
  1103. /* step one, relocate all the extents inside this chunk */
  1104. ret = btrfs_shrink_extent_tree(extent_root, chunk_offset);
  1105. BUG_ON(ret);
  1106. trans = btrfs_start_transaction(root, 1);
  1107. BUG_ON(!trans);
  1108. lock_chunks(root);
  1109. /*
  1110. * step two, delete the device extents and the
  1111. * chunk tree entries
  1112. */
  1113. spin_lock(&em_tree->lock);
  1114. em = lookup_extent_mapping(em_tree, chunk_offset, 1);
  1115. spin_unlock(&em_tree->lock);
  1116. BUG_ON(em->start > chunk_offset ||
  1117. em->start + em->len < chunk_offset);
  1118. map = (struct map_lookup *)em->bdev;
  1119. for (i = 0; i < map->num_stripes; i++) {
  1120. ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
  1121. map->stripes[i].physical);
  1122. BUG_ON(ret);
  1123. if (map->stripes[i].dev) {
  1124. ret = btrfs_update_device(trans, map->stripes[i].dev);
  1125. BUG_ON(ret);
  1126. }
  1127. }
  1128. ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
  1129. chunk_offset);
  1130. BUG_ON(ret);
  1131. if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1132. ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
  1133. BUG_ON(ret);
  1134. }
  1135. spin_lock(&em_tree->lock);
  1136. remove_extent_mapping(em_tree, em);
  1137. kfree(map);
  1138. em->bdev = NULL;
  1139. /* once for the tree */
  1140. free_extent_map(em);
  1141. spin_unlock(&em_tree->lock);
  1142. /* once for us */
  1143. free_extent_map(em);
  1144. unlock_chunks(root);
  1145. btrfs_end_transaction(trans, root);
  1146. return 0;
  1147. }
  1148. static u64 div_factor(u64 num, int factor)
  1149. {
  1150. if (factor == 10)
  1151. return num;
  1152. num *= factor;
  1153. do_div(num, 10);
  1154. return num;
  1155. }
  1156. int btrfs_balance(struct btrfs_root *dev_root)
  1157. {
  1158. int ret;
  1159. struct list_head *cur;
  1160. struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
  1161. struct btrfs_device *device;
  1162. u64 old_size;
  1163. u64 size_to_free;
  1164. struct btrfs_path *path;
  1165. struct btrfs_key key;
  1166. struct btrfs_chunk *chunk;
  1167. struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
  1168. struct btrfs_trans_handle *trans;
  1169. struct btrfs_key found_key;
  1170. mutex_lock(&dev_root->fs_info->volume_mutex);
  1171. dev_root = dev_root->fs_info->dev_root;
  1172. /* step one make some room on all the devices */
  1173. list_for_each(cur, devices) {
  1174. device = list_entry(cur, struct btrfs_device, dev_list);
  1175. old_size = device->total_bytes;
  1176. size_to_free = div_factor(old_size, 1);
  1177. size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
  1178. if (device->total_bytes - device->bytes_used > size_to_free)
  1179. continue;
  1180. ret = btrfs_shrink_device(device, old_size - size_to_free);
  1181. BUG_ON(ret);
  1182. trans = btrfs_start_transaction(dev_root, 1);
  1183. BUG_ON(!trans);
  1184. ret = btrfs_grow_device(trans, device, old_size);
  1185. BUG_ON(ret);
  1186. btrfs_end_transaction(trans, dev_root);
  1187. }
  1188. /* step two, relocate all the chunks */
  1189. path = btrfs_alloc_path();
  1190. BUG_ON(!path);
  1191. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  1192. key.offset = (u64)-1;
  1193. key.type = BTRFS_CHUNK_ITEM_KEY;
  1194. while(1) {
  1195. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  1196. if (ret < 0)
  1197. goto error;
  1198. /*
  1199. * this shouldn't happen, it means the last relocate
  1200. * failed
  1201. */
  1202. if (ret == 0)
  1203. break;
  1204. ret = btrfs_previous_item(chunk_root, path, 0,
  1205. BTRFS_CHUNK_ITEM_KEY);
  1206. if (ret)
  1207. break;
  1208. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1209. path->slots[0]);
  1210. if (found_key.objectid != key.objectid)
  1211. break;
  1212. chunk = btrfs_item_ptr(path->nodes[0],
  1213. path->slots[0],
  1214. struct btrfs_chunk);
  1215. key.offset = found_key.offset;
  1216. /* chunk zero is special */
  1217. if (key.offset == 0)
  1218. break;
  1219. btrfs_release_path(chunk_root, path);
  1220. ret = btrfs_relocate_chunk(chunk_root,
  1221. chunk_root->root_key.objectid,
  1222. found_key.objectid,
  1223. found_key.offset);
  1224. BUG_ON(ret);
  1225. }
  1226. ret = 0;
  1227. error:
  1228. btrfs_free_path(path);
  1229. mutex_unlock(&dev_root->fs_info->volume_mutex);
  1230. return ret;
  1231. }
  1232. /*
  1233. * shrinking a device means finding all of the device extents past
  1234. * the new size, and then following the back refs to the chunks.
  1235. * The chunk relocation code actually frees the device extent
  1236. */
  1237. int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
  1238. {
  1239. struct btrfs_trans_handle *trans;
  1240. struct btrfs_root *root = device->dev_root;
  1241. struct btrfs_dev_extent *dev_extent = NULL;
  1242. struct btrfs_path *path;
  1243. u64 length;
  1244. u64 chunk_tree;
  1245. u64 chunk_objectid;
  1246. u64 chunk_offset;
  1247. int ret;
  1248. int slot;
  1249. struct extent_buffer *l;
  1250. struct btrfs_key key;
  1251. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  1252. u64 old_total = btrfs_super_total_bytes(super_copy);
  1253. u64 diff = device->total_bytes - new_size;
  1254. path = btrfs_alloc_path();
  1255. if (!path)
  1256. return -ENOMEM;
  1257. trans = btrfs_start_transaction(root, 1);
  1258. if (!trans) {
  1259. ret = -ENOMEM;
  1260. goto done;
  1261. }
  1262. path->reada = 2;
  1263. lock_chunks(root);
  1264. device->total_bytes = new_size;
  1265. ret = btrfs_update_device(trans, device);
  1266. if (ret) {
  1267. unlock_chunks(root);
  1268. btrfs_end_transaction(trans, root);
  1269. goto done;
  1270. }
  1271. WARN_ON(diff > old_total);
  1272. btrfs_set_super_total_bytes(super_copy, old_total - diff);
  1273. unlock_chunks(root);
  1274. btrfs_end_transaction(trans, root);
  1275. key.objectid = device->devid;
  1276. key.offset = (u64)-1;
  1277. key.type = BTRFS_DEV_EXTENT_KEY;
  1278. while (1) {
  1279. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1280. if (ret < 0)
  1281. goto done;
  1282. ret = btrfs_previous_item(root, path, 0, key.type);
  1283. if (ret < 0)
  1284. goto done;
  1285. if (ret) {
  1286. ret = 0;
  1287. goto done;
  1288. }
  1289. l = path->nodes[0];
  1290. slot = path->slots[0];
  1291. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  1292. if (key.objectid != device->devid)
  1293. goto done;
  1294. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  1295. length = btrfs_dev_extent_length(l, dev_extent);
  1296. if (key.offset + length <= new_size)
  1297. goto done;
  1298. chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
  1299. chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
  1300. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  1301. btrfs_release_path(root, path);
  1302. ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
  1303. chunk_offset);
  1304. if (ret)
  1305. goto done;
  1306. }
  1307. done:
  1308. btrfs_free_path(path);
  1309. return ret;
  1310. }
  1311. int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
  1312. struct btrfs_root *root,
  1313. struct btrfs_key *key,
  1314. struct btrfs_chunk *chunk, int item_size)
  1315. {
  1316. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  1317. struct btrfs_disk_key disk_key;
  1318. u32 array_size;
  1319. u8 *ptr;
  1320. array_size = btrfs_super_sys_array_size(super_copy);
  1321. if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
  1322. return -EFBIG;
  1323. ptr = super_copy->sys_chunk_array + array_size;
  1324. btrfs_cpu_key_to_disk(&disk_key, key);
  1325. memcpy(ptr, &disk_key, sizeof(disk_key));
  1326. ptr += sizeof(disk_key);
  1327. memcpy(ptr, chunk, item_size);
  1328. item_size += sizeof(disk_key);
  1329. btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
  1330. return 0;
  1331. }
  1332. static u64 noinline chunk_bytes_by_type(u64 type, u64 calc_size,
  1333. int num_stripes, int sub_stripes)
  1334. {
  1335. if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
  1336. return calc_size;
  1337. else if (type & BTRFS_BLOCK_GROUP_RAID10)
  1338. return calc_size * (num_stripes / sub_stripes);
  1339. else
  1340. return calc_size * num_stripes;
  1341. }
  1342. int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  1343. struct btrfs_root *extent_root, u64 *start,
  1344. u64 *num_bytes, u64 type)
  1345. {
  1346. u64 dev_offset;
  1347. struct btrfs_fs_info *info = extent_root->fs_info;
  1348. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  1349. struct btrfs_path *path;
  1350. struct btrfs_stripe *stripes;
  1351. struct btrfs_device *device = NULL;
  1352. struct btrfs_chunk *chunk;
  1353. struct list_head private_devs;
  1354. struct list_head *dev_list;
  1355. struct list_head *cur;
  1356. struct extent_map_tree *em_tree;
  1357. struct map_lookup *map;
  1358. struct extent_map *em;
  1359. int min_stripe_size = 1 * 1024 * 1024;
  1360. u64 physical;
  1361. u64 calc_size = 1024 * 1024 * 1024;
  1362. u64 max_chunk_size = calc_size;
  1363. u64 min_free;
  1364. u64 avail;
  1365. u64 max_avail = 0;
  1366. u64 percent_max;
  1367. int num_stripes = 1;
  1368. int min_stripes = 1;
  1369. int sub_stripes = 0;
  1370. int looped = 0;
  1371. int ret;
  1372. int index;
  1373. int stripe_len = 64 * 1024;
  1374. struct btrfs_key key;
  1375. if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
  1376. (type & BTRFS_BLOCK_GROUP_DUP)) {
  1377. WARN_ON(1);
  1378. type &= ~BTRFS_BLOCK_GROUP_DUP;
  1379. }
  1380. dev_list = &extent_root->fs_info->fs_devices->alloc_list;
  1381. if (list_empty(dev_list))
  1382. return -ENOSPC;
  1383. if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
  1384. num_stripes = extent_root->fs_info->fs_devices->open_devices;
  1385. min_stripes = 2;
  1386. }
  1387. if (type & (BTRFS_BLOCK_GROUP_DUP)) {
  1388. num_stripes = 2;
  1389. min_stripes = 2;
  1390. }
  1391. if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
  1392. num_stripes = min_t(u64, 2,
  1393. extent_root->fs_info->fs_devices->open_devices);
  1394. if (num_stripes < 2)
  1395. return -ENOSPC;
  1396. min_stripes = 2;
  1397. }
  1398. if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
  1399. num_stripes = extent_root->fs_info->fs_devices->open_devices;
  1400. if (num_stripes < 4)
  1401. return -ENOSPC;
  1402. num_stripes &= ~(u32)1;
  1403. sub_stripes = 2;
  1404. min_stripes = 4;
  1405. }
  1406. if (type & BTRFS_BLOCK_GROUP_DATA) {
  1407. max_chunk_size = 10 * calc_size;
  1408. min_stripe_size = 64 * 1024 * 1024;
  1409. } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
  1410. max_chunk_size = 4 * calc_size;
  1411. min_stripe_size = 32 * 1024 * 1024;
  1412. } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1413. calc_size = 8 * 1024 * 1024;
  1414. max_chunk_size = calc_size * 2;
  1415. min_stripe_size = 1 * 1024 * 1024;
  1416. }
  1417. path = btrfs_alloc_path();
  1418. if (!path)
  1419. return -ENOMEM;
  1420. /* we don't want a chunk larger than 10% of the FS */
  1421. percent_max = div_factor(btrfs_super_total_bytes(&info->super_copy), 1);
  1422. max_chunk_size = min(percent_max, max_chunk_size);
  1423. again:
  1424. if (calc_size * num_stripes > max_chunk_size) {
  1425. calc_size = max_chunk_size;
  1426. do_div(calc_size, num_stripes);
  1427. do_div(calc_size, stripe_len);
  1428. calc_size *= stripe_len;
  1429. }
  1430. /* we don't want tiny stripes */
  1431. calc_size = max_t(u64, min_stripe_size, calc_size);
  1432. do_div(calc_size, stripe_len);
  1433. calc_size *= stripe_len;
  1434. INIT_LIST_HEAD(&private_devs);
  1435. cur = dev_list->next;
  1436. index = 0;
  1437. if (type & BTRFS_BLOCK_GROUP_DUP)
  1438. min_free = calc_size * 2;
  1439. else
  1440. min_free = calc_size;
  1441. /* we add 1MB because we never use the first 1MB of the device */
  1442. min_free += 1024 * 1024;
  1443. /* build a private list of devices we will allocate from */
  1444. while(index < num_stripes) {
  1445. device = list_entry(cur, struct btrfs_device, dev_alloc_list);
  1446. if (device->total_bytes > device->bytes_used)
  1447. avail = device->total_bytes - device->bytes_used;
  1448. else
  1449. avail = 0;
  1450. cur = cur->next;
  1451. if (device->in_fs_metadata && avail >= min_free) {
  1452. u64 ignored_start = 0;
  1453. ret = find_free_dev_extent(trans, device, path,
  1454. min_free,
  1455. &ignored_start);
  1456. if (ret == 0) {
  1457. list_move_tail(&device->dev_alloc_list,
  1458. &private_devs);
  1459. index++;
  1460. if (type & BTRFS_BLOCK_GROUP_DUP)
  1461. index++;
  1462. }
  1463. } else if (device->in_fs_metadata && avail > max_avail)
  1464. max_avail = avail;
  1465. if (cur == dev_list)
  1466. break;
  1467. }
  1468. if (index < num_stripes) {
  1469. list_splice(&private_devs, dev_list);
  1470. if (index >= min_stripes) {
  1471. num_stripes = index;
  1472. if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
  1473. num_stripes /= sub_stripes;
  1474. num_stripes *= sub_stripes;
  1475. }
  1476. looped = 1;
  1477. goto again;
  1478. }
  1479. if (!looped && max_avail > 0) {
  1480. looped = 1;
  1481. calc_size = max_avail;
  1482. goto again;
  1483. }
  1484. btrfs_free_path(path);
  1485. return -ENOSPC;
  1486. }
  1487. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  1488. key.type = BTRFS_CHUNK_ITEM_KEY;
  1489. ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  1490. &key.offset);
  1491. if (ret) {
  1492. btrfs_free_path(path);
  1493. return ret;
  1494. }
  1495. chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
  1496. if (!chunk) {
  1497. btrfs_free_path(path);
  1498. return -ENOMEM;
  1499. }
  1500. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  1501. if (!map) {
  1502. kfree(chunk);
  1503. btrfs_free_path(path);
  1504. return -ENOMEM;
  1505. }
  1506. btrfs_free_path(path);
  1507. path = NULL;
  1508. stripes = &chunk->stripe;
  1509. *num_bytes = chunk_bytes_by_type(type, calc_size,
  1510. num_stripes, sub_stripes);
  1511. index = 0;
  1512. while(index < num_stripes) {
  1513. struct btrfs_stripe *stripe;
  1514. BUG_ON(list_empty(&private_devs));
  1515. cur = private_devs.next;
  1516. device = list_entry(cur, struct btrfs_device, dev_alloc_list);
  1517. /* loop over this device again if we're doing a dup group */
  1518. if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
  1519. (index == num_stripes - 1))
  1520. list_move_tail(&device->dev_alloc_list, dev_list);
  1521. ret = btrfs_alloc_dev_extent(trans, device,
  1522. info->chunk_root->root_key.objectid,
  1523. BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
  1524. calc_size, &dev_offset);
  1525. BUG_ON(ret);
  1526. device->bytes_used += calc_size;
  1527. ret = btrfs_update_device(trans, device);
  1528. BUG_ON(ret);
  1529. map->stripes[index].dev = device;
  1530. map->stripes[index].physical = dev_offset;
  1531. stripe = stripes + index;
  1532. btrfs_set_stack_stripe_devid(stripe, device->devid);
  1533. btrfs_set_stack_stripe_offset(stripe, dev_offset);
  1534. memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
  1535. physical = dev_offset;
  1536. index++;
  1537. }
  1538. BUG_ON(!list_empty(&private_devs));
  1539. /* key was set above */
  1540. btrfs_set_stack_chunk_length(chunk, *num_bytes);
  1541. btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
  1542. btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
  1543. btrfs_set_stack_chunk_type(chunk, type);
  1544. btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
  1545. btrfs_set_stack_chunk_io_align(chunk, stripe_len);
  1546. btrfs_set_stack_chunk_io_width(chunk, stripe_len);
  1547. btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
  1548. btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
  1549. map->sector_size = extent_root->sectorsize;
  1550. map->stripe_len = stripe_len;
  1551. map->io_align = stripe_len;
  1552. map->io_width = stripe_len;
  1553. map->type = type;
  1554. map->num_stripes = num_stripes;
  1555. map->sub_stripes = sub_stripes;
  1556. ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
  1557. btrfs_chunk_item_size(num_stripes));
  1558. BUG_ON(ret);
  1559. *start = key.offset;;
  1560. em = alloc_extent_map(GFP_NOFS);
  1561. if (!em)
  1562. return -ENOMEM;
  1563. em->bdev = (struct block_device *)map;
  1564. em->start = key.offset;
  1565. em->len = *num_bytes;
  1566. em->block_start = 0;
  1567. if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1568. ret = btrfs_add_system_chunk(trans, chunk_root, &key,
  1569. chunk, btrfs_chunk_item_size(num_stripes));
  1570. BUG_ON(ret);
  1571. }
  1572. kfree(chunk);
  1573. em_tree = &extent_root->fs_info->mapping_tree.map_tree;
  1574. spin_lock(&em_tree->lock);
  1575. ret = add_extent_mapping(em_tree, em);
  1576. spin_unlock(&em_tree->lock);
  1577. BUG_ON(ret);
  1578. free_extent_map(em);
  1579. return ret;
  1580. }
  1581. void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
  1582. {
  1583. extent_map_tree_init(&tree->map_tree, GFP_NOFS);
  1584. }
  1585. void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
  1586. {
  1587. struct extent_map *em;
  1588. while(1) {
  1589. spin_lock(&tree->map_tree.lock);
  1590. em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
  1591. if (em)
  1592. remove_extent_mapping(&tree->map_tree, em);
  1593. spin_unlock(&tree->map_tree.lock);
  1594. if (!em)
  1595. break;
  1596. kfree(em->bdev);
  1597. /* once for us */
  1598. free_extent_map(em);
  1599. /* once for the tree */
  1600. free_extent_map(em);
  1601. }
  1602. }
  1603. int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
  1604. {
  1605. struct extent_map *em;
  1606. struct map_lookup *map;
  1607. struct extent_map_tree *em_tree = &map_tree->map_tree;
  1608. int ret;
  1609. spin_lock(&em_tree->lock);
  1610. em = lookup_extent_mapping(em_tree, logical, len);
  1611. spin_unlock(&em_tree->lock);
  1612. BUG_ON(!em);
  1613. BUG_ON(em->start > logical || em->start + em->len < logical);
  1614. map = (struct map_lookup *)em->bdev;
  1615. if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
  1616. ret = map->num_stripes;
  1617. else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  1618. ret = map->sub_stripes;
  1619. else
  1620. ret = 1;
  1621. free_extent_map(em);
  1622. return ret;
  1623. }
  1624. static int find_live_mirror(struct map_lookup *map, int first, int num,
  1625. int optimal)
  1626. {
  1627. int i;
  1628. if (map->stripes[optimal].dev->bdev)
  1629. return optimal;
  1630. for (i = first; i < first + num; i++) {
  1631. if (map->stripes[i].dev->bdev)
  1632. return i;
  1633. }
  1634. /* we couldn't find one that doesn't fail. Just return something
  1635. * and the io error handling code will clean up eventually
  1636. */
  1637. return optimal;
  1638. }
  1639. static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  1640. u64 logical, u64 *length,
  1641. struct btrfs_multi_bio **multi_ret,
  1642. int mirror_num, struct page *unplug_page)
  1643. {
  1644. struct extent_map *em;
  1645. struct map_lookup *map;
  1646. struct extent_map_tree *em_tree = &map_tree->map_tree;
  1647. u64 offset;
  1648. u64 stripe_offset;
  1649. u64 stripe_nr;
  1650. int stripes_allocated = 8;
  1651. int stripes_required = 1;
  1652. int stripe_index;
  1653. int i;
  1654. int num_stripes;
  1655. int max_errors = 0;
  1656. struct btrfs_multi_bio *multi = NULL;
  1657. if (multi_ret && !(rw & (1 << BIO_RW))) {
  1658. stripes_allocated = 1;
  1659. }
  1660. again:
  1661. if (multi_ret) {
  1662. multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
  1663. GFP_NOFS);
  1664. if (!multi)
  1665. return -ENOMEM;
  1666. atomic_set(&multi->error, 0);
  1667. }
  1668. spin_lock(&em_tree->lock);
  1669. em = lookup_extent_mapping(em_tree, logical, *length);
  1670. spin_unlock(&em_tree->lock);
  1671. if (!em && unplug_page)
  1672. return 0;
  1673. if (!em) {
  1674. printk("unable to find logical %Lu len %Lu\n", logical, *length);
  1675. BUG();
  1676. }
  1677. BUG_ON(em->start > logical || em->start + em->len < logical);
  1678. map = (struct map_lookup *)em->bdev;
  1679. offset = logical - em->start;
  1680. if (mirror_num > map->num_stripes)
  1681. mirror_num = 0;
  1682. /* if our multi bio struct is too small, back off and try again */
  1683. if (rw & (1 << BIO_RW)) {
  1684. if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
  1685. BTRFS_BLOCK_GROUP_DUP)) {
  1686. stripes_required = map->num_stripes;
  1687. max_errors = 1;
  1688. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  1689. stripes_required = map->sub_stripes;
  1690. max_errors = 1;
  1691. }
  1692. }
  1693. if (multi_ret && rw == WRITE &&
  1694. stripes_allocated < stripes_required) {
  1695. stripes_allocated = map->num_stripes;
  1696. free_extent_map(em);
  1697. kfree(multi);
  1698. goto again;
  1699. }
  1700. stripe_nr = offset;
  1701. /*
  1702. * stripe_nr counts the total number of stripes we have to stride
  1703. * to get to this block
  1704. */
  1705. do_div(stripe_nr, map->stripe_len);
  1706. stripe_offset = stripe_nr * map->stripe_len;
  1707. BUG_ON(offset < stripe_offset);
  1708. /* stripe_offset is the offset of this block in its stripe*/
  1709. stripe_offset = offset - stripe_offset;
  1710. if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
  1711. BTRFS_BLOCK_GROUP_RAID10 |
  1712. BTRFS_BLOCK_GROUP_DUP)) {
  1713. /* we limit the length of each bio to what fits in a stripe */
  1714. *length = min_t(u64, em->len - offset,
  1715. map->stripe_len - stripe_offset);
  1716. } else {
  1717. *length = em->len - offset;
  1718. }
  1719. if (!multi_ret && !unplug_page)
  1720. goto out;
  1721. num_stripes = 1;
  1722. stripe_index = 0;
  1723. if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  1724. if (unplug_page || (rw & (1 << BIO_RW)))
  1725. num_stripes = map->num_stripes;
  1726. else if (mirror_num)
  1727. stripe_index = mirror_num - 1;
  1728. else {
  1729. stripe_index = find_live_mirror(map, 0,
  1730. map->num_stripes,
  1731. current->pid % map->num_stripes);
  1732. }
  1733. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  1734. if (rw & (1 << BIO_RW))
  1735. num_stripes = map->num_stripes;
  1736. else if (mirror_num)
  1737. stripe_index = mirror_num - 1;
  1738. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  1739. int factor = map->num_stripes / map->sub_stripes;
  1740. stripe_index = do_div(stripe_nr, factor);
  1741. stripe_index *= map->sub_stripes;
  1742. if (unplug_page || (rw & (1 << BIO_RW)))
  1743. num_stripes = map->sub_stripes;
  1744. else if (mirror_num)
  1745. stripe_index += mirror_num - 1;
  1746. else {
  1747. stripe_index = find_live_mirror(map, stripe_index,
  1748. map->sub_stripes, stripe_index +
  1749. current->pid % map->sub_stripes);
  1750. }
  1751. } else {
  1752. /*
  1753. * after this do_div call, stripe_nr is the number of stripes
  1754. * on this device we have to walk to find the data, and
  1755. * stripe_index is the number of our device in the stripe array
  1756. */
  1757. stripe_index = do_div(stripe_nr, map->num_stripes);
  1758. }
  1759. BUG_ON(stripe_index >= map->num_stripes);
  1760. for (i = 0; i < num_stripes; i++) {
  1761. if (unplug_page) {
  1762. struct btrfs_device *device;
  1763. struct backing_dev_info *bdi;
  1764. device = map->stripes[stripe_index].dev;
  1765. if (device->bdev) {
  1766. bdi = blk_get_backing_dev_info(device->bdev);
  1767. if (bdi->unplug_io_fn) {
  1768. bdi->unplug_io_fn(bdi, unplug_page);
  1769. }
  1770. }
  1771. } else {
  1772. multi->stripes[i].physical =
  1773. map->stripes[stripe_index].physical +
  1774. stripe_offset + stripe_nr * map->stripe_len;
  1775. multi->stripes[i].dev = map->stripes[stripe_index].dev;
  1776. }
  1777. stripe_index++;
  1778. }
  1779. if (multi_ret) {
  1780. *multi_ret = multi;
  1781. multi->num_stripes = num_stripes;
  1782. multi->max_errors = max_errors;
  1783. }
  1784. out:
  1785. free_extent_map(em);
  1786. return 0;
  1787. }
  1788. int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  1789. u64 logical, u64 *length,
  1790. struct btrfs_multi_bio **multi_ret, int mirror_num)
  1791. {
  1792. return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
  1793. mirror_num, NULL);
  1794. }
  1795. int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
  1796. u64 logical, struct page *page)
  1797. {
  1798. u64 length = PAGE_CACHE_SIZE;
  1799. return __btrfs_map_block(map_tree, READ, logical, &length,
  1800. NULL, 0, page);
  1801. }
  1802. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
  1803. static void end_bio_multi_stripe(struct bio *bio, int err)
  1804. #else
  1805. static int end_bio_multi_stripe(struct bio *bio,
  1806. unsigned int bytes_done, int err)
  1807. #endif
  1808. {
  1809. struct btrfs_multi_bio *multi = bio->bi_private;
  1810. int is_orig_bio = 0;
  1811. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  1812. if (bio->bi_size)
  1813. return 1;
  1814. #endif
  1815. if (err)
  1816. atomic_inc(&multi->error);
  1817. if (bio == multi->orig_bio)
  1818. is_orig_bio = 1;
  1819. if (atomic_dec_and_test(&multi->stripes_pending)) {
  1820. if (!is_orig_bio) {
  1821. bio_put(bio);
  1822. bio = multi->orig_bio;
  1823. }
  1824. bio->bi_private = multi->private;
  1825. bio->bi_end_io = multi->end_io;
  1826. /* only send an error to the higher layers if it is
  1827. * beyond the tolerance of the multi-bio
  1828. */
  1829. if (atomic_read(&multi->error) > multi->max_errors) {
  1830. err = -EIO;
  1831. } else if (err) {
  1832. /*
  1833. * this bio is actually up to date, we didn't
  1834. * go over the max number of errors
  1835. */
  1836. set_bit(BIO_UPTODATE, &bio->bi_flags);
  1837. err = 0;
  1838. }
  1839. kfree(multi);
  1840. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  1841. bio_endio(bio, bio->bi_size, err);
  1842. #else
  1843. bio_endio(bio, err);
  1844. #endif
  1845. } else if (!is_orig_bio) {
  1846. bio_put(bio);
  1847. }
  1848. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  1849. return 0;
  1850. #endif
  1851. }
  1852. struct async_sched {
  1853. struct bio *bio;
  1854. int rw;
  1855. struct btrfs_fs_info *info;
  1856. struct btrfs_work work;
  1857. };
  1858. /*
  1859. * see run_scheduled_bios for a description of why bios are collected for
  1860. * async submit.
  1861. *
  1862. * This will add one bio to the pending list for a device and make sure
  1863. * the work struct is scheduled.
  1864. */
  1865. static int noinline schedule_bio(struct btrfs_root *root,
  1866. struct btrfs_device *device,
  1867. int rw, struct bio *bio)
  1868. {
  1869. int should_queue = 1;
  1870. /* don't bother with additional async steps for reads, right now */
  1871. if (!(rw & (1 << BIO_RW))) {
  1872. bio_get(bio);
  1873. submit_bio(rw, bio);
  1874. bio_put(bio);
  1875. return 0;
  1876. }
  1877. /*
  1878. * nr_async_bios allows us to reliably return congestion to the
  1879. * higher layers. Otherwise, the async bio makes it appear we have
  1880. * made progress against dirty pages when we've really just put it
  1881. * on a queue for later
  1882. */
  1883. atomic_inc(&root->fs_info->nr_async_bios);
  1884. WARN_ON(bio->bi_next);
  1885. bio->bi_next = NULL;
  1886. bio->bi_rw |= rw;
  1887. spin_lock(&device->io_lock);
  1888. if (device->pending_bio_tail)
  1889. device->pending_bio_tail->bi_next = bio;
  1890. device->pending_bio_tail = bio;
  1891. if (!device->pending_bios)
  1892. device->pending_bios = bio;
  1893. if (device->running_pending)
  1894. should_queue = 0;
  1895. spin_unlock(&device->io_lock);
  1896. if (should_queue)
  1897. btrfs_queue_worker(&root->fs_info->submit_workers,
  1898. &device->work);
  1899. return 0;
  1900. }
  1901. int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
  1902. int mirror_num, int async_submit)
  1903. {
  1904. struct btrfs_mapping_tree *map_tree;
  1905. struct btrfs_device *dev;
  1906. struct bio *first_bio = bio;
  1907. u64 logical = bio->bi_sector << 9;
  1908. u64 length = 0;
  1909. u64 map_length;
  1910. struct btrfs_multi_bio *multi = NULL;
  1911. int ret;
  1912. int dev_nr = 0;
  1913. int total_devs = 1;
  1914. length = bio->bi_size;
  1915. map_tree = &root->fs_info->mapping_tree;
  1916. map_length = length;
  1917. ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
  1918. mirror_num);
  1919. BUG_ON(ret);
  1920. total_devs = multi->num_stripes;
  1921. if (map_length < length) {
  1922. printk("mapping failed logical %Lu bio len %Lu "
  1923. "len %Lu\n", logical, length, map_length);
  1924. BUG();
  1925. }
  1926. multi->end_io = first_bio->bi_end_io;
  1927. multi->private = first_bio->bi_private;
  1928. multi->orig_bio = first_bio;
  1929. atomic_set(&multi->stripes_pending, multi->num_stripes);
  1930. while(dev_nr < total_devs) {
  1931. if (total_devs > 1) {
  1932. if (dev_nr < total_devs - 1) {
  1933. bio = bio_clone(first_bio, GFP_NOFS);
  1934. BUG_ON(!bio);
  1935. } else {
  1936. bio = first_bio;
  1937. }
  1938. bio->bi_private = multi;
  1939. bio->bi_end_io = end_bio_multi_stripe;
  1940. }
  1941. bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
  1942. dev = multi->stripes[dev_nr].dev;
  1943. if (dev && dev->bdev) {
  1944. bio->bi_bdev = dev->bdev;
  1945. if (async_submit)
  1946. schedule_bio(root, dev, rw, bio);
  1947. else
  1948. submit_bio(rw, bio);
  1949. } else {
  1950. bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
  1951. bio->bi_sector = logical >> 9;
  1952. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  1953. bio_endio(bio, bio->bi_size, -EIO);
  1954. #else
  1955. bio_endio(bio, -EIO);
  1956. #endif
  1957. }
  1958. dev_nr++;
  1959. }
  1960. if (total_devs == 1)
  1961. kfree(multi);
  1962. return 0;
  1963. }
  1964. struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
  1965. u8 *uuid)
  1966. {
  1967. struct list_head *head = &root->fs_info->fs_devices->devices;
  1968. return __find_device(head, devid, uuid);
  1969. }
  1970. static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
  1971. u64 devid, u8 *dev_uuid)
  1972. {
  1973. struct btrfs_device *device;
  1974. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  1975. device = kzalloc(sizeof(*device), GFP_NOFS);
  1976. list_add(&device->dev_list,
  1977. &fs_devices->devices);
  1978. list_add(&device->dev_alloc_list,
  1979. &fs_devices->alloc_list);
  1980. device->barriers = 1;
  1981. device->dev_root = root->fs_info->dev_root;
  1982. device->devid = devid;
  1983. device->work.func = pending_bios_fn;
  1984. fs_devices->num_devices++;
  1985. spin_lock_init(&device->io_lock);
  1986. memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
  1987. return device;
  1988. }
  1989. static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
  1990. struct extent_buffer *leaf,
  1991. struct btrfs_chunk *chunk)
  1992. {
  1993. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  1994. struct map_lookup *map;
  1995. struct extent_map *em;
  1996. u64 logical;
  1997. u64 length;
  1998. u64 devid;
  1999. u8 uuid[BTRFS_UUID_SIZE];
  2000. int num_stripes;
  2001. int ret;
  2002. int i;
  2003. logical = key->offset;
  2004. length = btrfs_chunk_length(leaf, chunk);
  2005. spin_lock(&map_tree->map_tree.lock);
  2006. em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
  2007. spin_unlock(&map_tree->map_tree.lock);
  2008. /* already mapped? */
  2009. if (em && em->start <= logical && em->start + em->len > logical) {
  2010. free_extent_map(em);
  2011. return 0;
  2012. } else if (em) {
  2013. free_extent_map(em);
  2014. }
  2015. map = kzalloc(sizeof(*map), GFP_NOFS);
  2016. if (!map)
  2017. return -ENOMEM;
  2018. em = alloc_extent_map(GFP_NOFS);
  2019. if (!em)
  2020. return -ENOMEM;
  2021. num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  2022. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  2023. if (!map) {
  2024. free_extent_map(em);
  2025. return -ENOMEM;
  2026. }
  2027. em->bdev = (struct block_device *)map;
  2028. em->start = logical;
  2029. em->len = length;
  2030. em->block_start = 0;
  2031. map->num_stripes = num_stripes;
  2032. map->io_width = btrfs_chunk_io_width(leaf, chunk);
  2033. map->io_align = btrfs_chunk_io_align(leaf, chunk);
  2034. map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
  2035. map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
  2036. map->type = btrfs_chunk_type(leaf, chunk);
  2037. map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
  2038. for (i = 0; i < num_stripes; i++) {
  2039. map->stripes[i].physical =
  2040. btrfs_stripe_offset_nr(leaf, chunk, i);
  2041. devid = btrfs_stripe_devid_nr(leaf, chunk, i);
  2042. read_extent_buffer(leaf, uuid, (unsigned long)
  2043. btrfs_stripe_dev_uuid_nr(chunk, i),
  2044. BTRFS_UUID_SIZE);
  2045. map->stripes[i].dev = btrfs_find_device(root, devid, uuid);
  2046. if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
  2047. kfree(map);
  2048. free_extent_map(em);
  2049. return -EIO;
  2050. }
  2051. if (!map->stripes[i].dev) {
  2052. map->stripes[i].dev =
  2053. add_missing_dev(root, devid, uuid);
  2054. if (!map->stripes[i].dev) {
  2055. kfree(map);
  2056. free_extent_map(em);
  2057. return -EIO;
  2058. }
  2059. }
  2060. map->stripes[i].dev->in_fs_metadata = 1;
  2061. }
  2062. spin_lock(&map_tree->map_tree.lock);
  2063. ret = add_extent_mapping(&map_tree->map_tree, em);
  2064. spin_unlock(&map_tree->map_tree.lock);
  2065. BUG_ON(ret);
  2066. free_extent_map(em);
  2067. return 0;
  2068. }
  2069. static int fill_device_from_item(struct extent_buffer *leaf,
  2070. struct btrfs_dev_item *dev_item,
  2071. struct btrfs_device *device)
  2072. {
  2073. unsigned long ptr;
  2074. device->devid = btrfs_device_id(leaf, dev_item);
  2075. device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
  2076. device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
  2077. device->type = btrfs_device_type(leaf, dev_item);
  2078. device->io_align = btrfs_device_io_align(leaf, dev_item);
  2079. device->io_width = btrfs_device_io_width(leaf, dev_item);
  2080. device->sector_size = btrfs_device_sector_size(leaf, dev_item);
  2081. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  2082. read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  2083. return 0;
  2084. }
  2085. static int read_one_dev(struct btrfs_root *root,
  2086. struct extent_buffer *leaf,
  2087. struct btrfs_dev_item *dev_item)
  2088. {
  2089. struct btrfs_device *device;
  2090. u64 devid;
  2091. int ret;
  2092. u8 dev_uuid[BTRFS_UUID_SIZE];
  2093. devid = btrfs_device_id(leaf, dev_item);
  2094. read_extent_buffer(leaf, dev_uuid,
  2095. (unsigned long)btrfs_device_uuid(dev_item),
  2096. BTRFS_UUID_SIZE);
  2097. device = btrfs_find_device(root, devid, dev_uuid);
  2098. if (!device) {
  2099. printk("warning devid %Lu missing\n", devid);
  2100. device = add_missing_dev(root, devid, dev_uuid);
  2101. if (!device)
  2102. return -ENOMEM;
  2103. }
  2104. fill_device_from_item(leaf, dev_item, device);
  2105. device->dev_root = root->fs_info->dev_root;
  2106. device->in_fs_metadata = 1;
  2107. ret = 0;
  2108. #if 0
  2109. ret = btrfs_open_device(device);
  2110. if (ret) {
  2111. kfree(device);
  2112. }
  2113. #endif
  2114. return ret;
  2115. }
  2116. int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
  2117. {
  2118. struct btrfs_dev_item *dev_item;
  2119. dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
  2120. dev_item);
  2121. return read_one_dev(root, buf, dev_item);
  2122. }
  2123. int btrfs_read_sys_array(struct btrfs_root *root)
  2124. {
  2125. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  2126. struct extent_buffer *sb;
  2127. struct btrfs_disk_key *disk_key;
  2128. struct btrfs_chunk *chunk;
  2129. u8 *ptr;
  2130. unsigned long sb_ptr;
  2131. int ret = 0;
  2132. u32 num_stripes;
  2133. u32 array_size;
  2134. u32 len = 0;
  2135. u32 cur;
  2136. struct btrfs_key key;
  2137. sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
  2138. BTRFS_SUPER_INFO_SIZE);
  2139. if (!sb)
  2140. return -ENOMEM;
  2141. btrfs_set_buffer_uptodate(sb);
  2142. write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
  2143. array_size = btrfs_super_sys_array_size(super_copy);
  2144. ptr = super_copy->sys_chunk_array;
  2145. sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
  2146. cur = 0;
  2147. while (cur < array_size) {
  2148. disk_key = (struct btrfs_disk_key *)ptr;
  2149. btrfs_disk_key_to_cpu(&key, disk_key);
  2150. len = sizeof(*disk_key); ptr += len;
  2151. sb_ptr += len;
  2152. cur += len;
  2153. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  2154. chunk = (struct btrfs_chunk *)sb_ptr;
  2155. ret = read_one_chunk(root, &key, sb, chunk);
  2156. if (ret)
  2157. break;
  2158. num_stripes = btrfs_chunk_num_stripes(sb, chunk);
  2159. len = btrfs_chunk_item_size(num_stripes);
  2160. } else {
  2161. ret = -EIO;
  2162. break;
  2163. }
  2164. ptr += len;
  2165. sb_ptr += len;
  2166. cur += len;
  2167. }
  2168. free_extent_buffer(sb);
  2169. return ret;
  2170. }
  2171. int btrfs_read_chunk_tree(struct btrfs_root *root)
  2172. {
  2173. struct btrfs_path *path;
  2174. struct extent_buffer *leaf;
  2175. struct btrfs_key key;
  2176. struct btrfs_key found_key;
  2177. int ret;
  2178. int slot;
  2179. root = root->fs_info->chunk_root;
  2180. path = btrfs_alloc_path();
  2181. if (!path)
  2182. return -ENOMEM;
  2183. /* first we search for all of the device items, and then we
  2184. * read in all of the chunk items. This way we can create chunk
  2185. * mappings that reference all of the devices that are afound
  2186. */
  2187. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  2188. key.offset = 0;
  2189. key.type = 0;
  2190. again:
  2191. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2192. while(1) {
  2193. leaf = path->nodes[0];
  2194. slot = path->slots[0];
  2195. if (slot >= btrfs_header_nritems(leaf)) {
  2196. ret = btrfs_next_leaf(root, path);
  2197. if (ret == 0)
  2198. continue;
  2199. if (ret < 0)
  2200. goto error;
  2201. break;
  2202. }
  2203. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  2204. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  2205. if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
  2206. break;
  2207. if (found_key.type == BTRFS_DEV_ITEM_KEY) {
  2208. struct btrfs_dev_item *dev_item;
  2209. dev_item = btrfs_item_ptr(leaf, slot,
  2210. struct btrfs_dev_item);
  2211. ret = read_one_dev(root, leaf, dev_item);
  2212. BUG_ON(ret);
  2213. }
  2214. } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
  2215. struct btrfs_chunk *chunk;
  2216. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  2217. ret = read_one_chunk(root, &found_key, leaf, chunk);
  2218. }
  2219. path->slots[0]++;
  2220. }
  2221. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  2222. key.objectid = 0;
  2223. btrfs_release_path(root, path);
  2224. goto again;
  2225. }
  2226. btrfs_free_path(path);
  2227. ret = 0;
  2228. error:
  2229. return ret;
  2230. }