volumes.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558
  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. total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
  949. btrfs_set_super_total_bytes(&root->fs_info->super_copy,
  950. total_bytes + device->total_bytes);
  951. total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
  952. btrfs_set_super_num_devices(&root->fs_info->super_copy,
  953. total_bytes + 1);
  954. list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
  955. list_add(&device->dev_alloc_list,
  956. &root->fs_info->fs_devices->alloc_list);
  957. root->fs_info->fs_devices->num_devices++;
  958. root->fs_info->fs_devices->open_devices++;
  959. out:
  960. unlock_chunks(root);
  961. btrfs_end_transaction(trans, root);
  962. mutex_unlock(&root->fs_info->volume_mutex);
  963. return ret;
  964. out_close_bdev:
  965. close_bdev_excl(bdev);
  966. goto out;
  967. }
  968. int noinline btrfs_update_device(struct btrfs_trans_handle *trans,
  969. struct btrfs_device *device)
  970. {
  971. int ret;
  972. struct btrfs_path *path;
  973. struct btrfs_root *root;
  974. struct btrfs_dev_item *dev_item;
  975. struct extent_buffer *leaf;
  976. struct btrfs_key key;
  977. root = device->dev_root->fs_info->chunk_root;
  978. path = btrfs_alloc_path();
  979. if (!path)
  980. return -ENOMEM;
  981. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  982. key.type = BTRFS_DEV_ITEM_KEY;
  983. key.offset = device->devid;
  984. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  985. if (ret < 0)
  986. goto out;
  987. if (ret > 0) {
  988. ret = -ENOENT;
  989. goto out;
  990. }
  991. leaf = path->nodes[0];
  992. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  993. btrfs_set_device_id(leaf, dev_item, device->devid);
  994. btrfs_set_device_type(leaf, dev_item, device->type);
  995. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  996. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  997. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  998. btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
  999. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  1000. btrfs_mark_buffer_dirty(leaf);
  1001. out:
  1002. btrfs_free_path(path);
  1003. return ret;
  1004. }
  1005. static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
  1006. struct btrfs_device *device, u64 new_size)
  1007. {
  1008. struct btrfs_super_block *super_copy =
  1009. &device->dev_root->fs_info->super_copy;
  1010. u64 old_total = btrfs_super_total_bytes(super_copy);
  1011. u64 diff = new_size - device->total_bytes;
  1012. btrfs_set_super_total_bytes(super_copy, old_total + diff);
  1013. return btrfs_update_device(trans, device);
  1014. }
  1015. int btrfs_grow_device(struct btrfs_trans_handle *trans,
  1016. struct btrfs_device *device, u64 new_size)
  1017. {
  1018. int ret;
  1019. lock_chunks(device->dev_root);
  1020. ret = __btrfs_grow_device(trans, device, new_size);
  1021. unlock_chunks(device->dev_root);
  1022. return ret;
  1023. }
  1024. static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
  1025. struct btrfs_root *root,
  1026. u64 chunk_tree, u64 chunk_objectid,
  1027. u64 chunk_offset)
  1028. {
  1029. int ret;
  1030. struct btrfs_path *path;
  1031. struct btrfs_key key;
  1032. root = root->fs_info->chunk_root;
  1033. path = btrfs_alloc_path();
  1034. if (!path)
  1035. return -ENOMEM;
  1036. key.objectid = chunk_objectid;
  1037. key.offset = chunk_offset;
  1038. key.type = BTRFS_CHUNK_ITEM_KEY;
  1039. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1040. BUG_ON(ret);
  1041. ret = btrfs_del_item(trans, root, path);
  1042. BUG_ON(ret);
  1043. btrfs_free_path(path);
  1044. return 0;
  1045. }
  1046. int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
  1047. chunk_offset)
  1048. {
  1049. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  1050. struct btrfs_disk_key *disk_key;
  1051. struct btrfs_chunk *chunk;
  1052. u8 *ptr;
  1053. int ret = 0;
  1054. u32 num_stripes;
  1055. u32 array_size;
  1056. u32 len = 0;
  1057. u32 cur;
  1058. struct btrfs_key key;
  1059. array_size = btrfs_super_sys_array_size(super_copy);
  1060. ptr = super_copy->sys_chunk_array;
  1061. cur = 0;
  1062. while (cur < array_size) {
  1063. disk_key = (struct btrfs_disk_key *)ptr;
  1064. btrfs_disk_key_to_cpu(&key, disk_key);
  1065. len = sizeof(*disk_key);
  1066. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  1067. chunk = (struct btrfs_chunk *)(ptr + len);
  1068. num_stripes = btrfs_stack_chunk_num_stripes(chunk);
  1069. len += btrfs_chunk_item_size(num_stripes);
  1070. } else {
  1071. ret = -EIO;
  1072. break;
  1073. }
  1074. if (key.objectid == chunk_objectid &&
  1075. key.offset == chunk_offset) {
  1076. memmove(ptr, ptr + len, array_size - (cur + len));
  1077. array_size -= len;
  1078. btrfs_set_super_sys_array_size(super_copy, array_size);
  1079. } else {
  1080. ptr += len;
  1081. cur += len;
  1082. }
  1083. }
  1084. return ret;
  1085. }
  1086. int btrfs_relocate_chunk(struct btrfs_root *root,
  1087. u64 chunk_tree, u64 chunk_objectid,
  1088. u64 chunk_offset)
  1089. {
  1090. struct extent_map_tree *em_tree;
  1091. struct btrfs_root *extent_root;
  1092. struct btrfs_trans_handle *trans;
  1093. struct extent_map *em;
  1094. struct map_lookup *map;
  1095. int ret;
  1096. int i;
  1097. printk("btrfs relocating chunk %llu\n",
  1098. (unsigned long long)chunk_offset);
  1099. root = root->fs_info->chunk_root;
  1100. extent_root = root->fs_info->extent_root;
  1101. em_tree = &root->fs_info->mapping_tree.map_tree;
  1102. /* step one, relocate all the extents inside this chunk */
  1103. ret = btrfs_shrink_extent_tree(extent_root, chunk_offset);
  1104. BUG_ON(ret);
  1105. trans = btrfs_start_transaction(root, 1);
  1106. BUG_ON(!trans);
  1107. lock_chunks(root);
  1108. /*
  1109. * step two, delete the device extents and the
  1110. * chunk tree entries
  1111. */
  1112. spin_lock(&em_tree->lock);
  1113. em = lookup_extent_mapping(em_tree, chunk_offset, 1);
  1114. spin_unlock(&em_tree->lock);
  1115. BUG_ON(em->start > chunk_offset ||
  1116. em->start + em->len < chunk_offset);
  1117. map = (struct map_lookup *)em->bdev;
  1118. for (i = 0; i < map->num_stripes; i++) {
  1119. ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
  1120. map->stripes[i].physical);
  1121. BUG_ON(ret);
  1122. if (map->stripes[i].dev) {
  1123. ret = btrfs_update_device(trans, map->stripes[i].dev);
  1124. BUG_ON(ret);
  1125. }
  1126. }
  1127. ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
  1128. chunk_offset);
  1129. BUG_ON(ret);
  1130. if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1131. ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
  1132. BUG_ON(ret);
  1133. }
  1134. spin_lock(&em_tree->lock);
  1135. remove_extent_mapping(em_tree, em);
  1136. kfree(map);
  1137. em->bdev = NULL;
  1138. /* once for the tree */
  1139. free_extent_map(em);
  1140. spin_unlock(&em_tree->lock);
  1141. /* once for us */
  1142. free_extent_map(em);
  1143. unlock_chunks(root);
  1144. btrfs_end_transaction(trans, root);
  1145. return 0;
  1146. }
  1147. static u64 div_factor(u64 num, int factor)
  1148. {
  1149. if (factor == 10)
  1150. return num;
  1151. num *= factor;
  1152. do_div(num, 10);
  1153. return num;
  1154. }
  1155. int btrfs_balance(struct btrfs_root *dev_root)
  1156. {
  1157. int ret;
  1158. struct list_head *cur;
  1159. struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
  1160. struct btrfs_device *device;
  1161. u64 old_size;
  1162. u64 size_to_free;
  1163. struct btrfs_path *path;
  1164. struct btrfs_key key;
  1165. struct btrfs_chunk *chunk;
  1166. struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
  1167. struct btrfs_trans_handle *trans;
  1168. struct btrfs_key found_key;
  1169. mutex_lock(&dev_root->fs_info->volume_mutex);
  1170. dev_root = dev_root->fs_info->dev_root;
  1171. /* step one make some room on all the devices */
  1172. list_for_each(cur, devices) {
  1173. device = list_entry(cur, struct btrfs_device, dev_list);
  1174. old_size = device->total_bytes;
  1175. size_to_free = div_factor(old_size, 1);
  1176. size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
  1177. if (device->total_bytes - device->bytes_used > size_to_free)
  1178. continue;
  1179. ret = btrfs_shrink_device(device, old_size - size_to_free);
  1180. BUG_ON(ret);
  1181. trans = btrfs_start_transaction(dev_root, 1);
  1182. BUG_ON(!trans);
  1183. ret = btrfs_grow_device(trans, device, old_size);
  1184. BUG_ON(ret);
  1185. btrfs_end_transaction(trans, dev_root);
  1186. }
  1187. /* step two, relocate all the chunks */
  1188. path = btrfs_alloc_path();
  1189. BUG_ON(!path);
  1190. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  1191. key.offset = (u64)-1;
  1192. key.type = BTRFS_CHUNK_ITEM_KEY;
  1193. while(1) {
  1194. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  1195. if (ret < 0)
  1196. goto error;
  1197. /*
  1198. * this shouldn't happen, it means the last relocate
  1199. * failed
  1200. */
  1201. if (ret == 0)
  1202. break;
  1203. ret = btrfs_previous_item(chunk_root, path, 0,
  1204. BTRFS_CHUNK_ITEM_KEY);
  1205. if (ret)
  1206. break;
  1207. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1208. path->slots[0]);
  1209. if (found_key.objectid != key.objectid)
  1210. break;
  1211. chunk = btrfs_item_ptr(path->nodes[0],
  1212. path->slots[0],
  1213. struct btrfs_chunk);
  1214. key.offset = found_key.offset;
  1215. /* chunk zero is special */
  1216. if (key.offset == 0)
  1217. break;
  1218. btrfs_release_path(chunk_root, path);
  1219. ret = btrfs_relocate_chunk(chunk_root,
  1220. chunk_root->root_key.objectid,
  1221. found_key.objectid,
  1222. found_key.offset);
  1223. BUG_ON(ret);
  1224. }
  1225. ret = 0;
  1226. error:
  1227. btrfs_free_path(path);
  1228. mutex_unlock(&dev_root->fs_info->volume_mutex);
  1229. return ret;
  1230. }
  1231. /*
  1232. * shrinking a device means finding all of the device extents past
  1233. * the new size, and then following the back refs to the chunks.
  1234. * The chunk relocation code actually frees the device extent
  1235. */
  1236. int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
  1237. {
  1238. struct btrfs_trans_handle *trans;
  1239. struct btrfs_root *root = device->dev_root;
  1240. struct btrfs_dev_extent *dev_extent = NULL;
  1241. struct btrfs_path *path;
  1242. u64 length;
  1243. u64 chunk_tree;
  1244. u64 chunk_objectid;
  1245. u64 chunk_offset;
  1246. int ret;
  1247. int slot;
  1248. struct extent_buffer *l;
  1249. struct btrfs_key key;
  1250. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  1251. u64 old_total = btrfs_super_total_bytes(super_copy);
  1252. u64 diff = device->total_bytes - new_size;
  1253. path = btrfs_alloc_path();
  1254. if (!path)
  1255. return -ENOMEM;
  1256. trans = btrfs_start_transaction(root, 1);
  1257. if (!trans) {
  1258. ret = -ENOMEM;
  1259. goto done;
  1260. }
  1261. path->reada = 2;
  1262. lock_chunks(root);
  1263. device->total_bytes = new_size;
  1264. ret = btrfs_update_device(trans, device);
  1265. if (ret) {
  1266. unlock_chunks(root);
  1267. btrfs_end_transaction(trans, root);
  1268. goto done;
  1269. }
  1270. WARN_ON(diff > old_total);
  1271. btrfs_set_super_total_bytes(super_copy, old_total - diff);
  1272. unlock_chunks(root);
  1273. btrfs_end_transaction(trans, root);
  1274. key.objectid = device->devid;
  1275. key.offset = (u64)-1;
  1276. key.type = BTRFS_DEV_EXTENT_KEY;
  1277. while (1) {
  1278. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1279. if (ret < 0)
  1280. goto done;
  1281. ret = btrfs_previous_item(root, path, 0, key.type);
  1282. if (ret < 0)
  1283. goto done;
  1284. if (ret) {
  1285. ret = 0;
  1286. goto done;
  1287. }
  1288. l = path->nodes[0];
  1289. slot = path->slots[0];
  1290. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  1291. if (key.objectid != device->devid)
  1292. goto done;
  1293. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  1294. length = btrfs_dev_extent_length(l, dev_extent);
  1295. if (key.offset + length <= new_size)
  1296. goto done;
  1297. chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
  1298. chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
  1299. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  1300. btrfs_release_path(root, path);
  1301. ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
  1302. chunk_offset);
  1303. if (ret)
  1304. goto done;
  1305. }
  1306. done:
  1307. btrfs_free_path(path);
  1308. return ret;
  1309. }
  1310. int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
  1311. struct btrfs_root *root,
  1312. struct btrfs_key *key,
  1313. struct btrfs_chunk *chunk, int item_size)
  1314. {
  1315. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  1316. struct btrfs_disk_key disk_key;
  1317. u32 array_size;
  1318. u8 *ptr;
  1319. array_size = btrfs_super_sys_array_size(super_copy);
  1320. if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
  1321. return -EFBIG;
  1322. ptr = super_copy->sys_chunk_array + array_size;
  1323. btrfs_cpu_key_to_disk(&disk_key, key);
  1324. memcpy(ptr, &disk_key, sizeof(disk_key));
  1325. ptr += sizeof(disk_key);
  1326. memcpy(ptr, chunk, item_size);
  1327. item_size += sizeof(disk_key);
  1328. btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
  1329. return 0;
  1330. }
  1331. static u64 noinline chunk_bytes_by_type(u64 type, u64 calc_size,
  1332. int num_stripes, int sub_stripes)
  1333. {
  1334. if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
  1335. return calc_size;
  1336. else if (type & BTRFS_BLOCK_GROUP_RAID10)
  1337. return calc_size * (num_stripes / sub_stripes);
  1338. else
  1339. return calc_size * num_stripes;
  1340. }
  1341. int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  1342. struct btrfs_root *extent_root, u64 *start,
  1343. u64 *num_bytes, u64 type)
  1344. {
  1345. u64 dev_offset;
  1346. struct btrfs_fs_info *info = extent_root->fs_info;
  1347. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  1348. struct btrfs_path *path;
  1349. struct btrfs_stripe *stripes;
  1350. struct btrfs_device *device = NULL;
  1351. struct btrfs_chunk *chunk;
  1352. struct list_head private_devs;
  1353. struct list_head *dev_list;
  1354. struct list_head *cur;
  1355. struct extent_map_tree *em_tree;
  1356. struct map_lookup *map;
  1357. struct extent_map *em;
  1358. int min_stripe_size = 1 * 1024 * 1024;
  1359. u64 physical;
  1360. u64 calc_size = 1024 * 1024 * 1024;
  1361. u64 max_chunk_size = calc_size;
  1362. u64 min_free;
  1363. u64 avail;
  1364. u64 max_avail = 0;
  1365. u64 percent_max;
  1366. int num_stripes = 1;
  1367. int min_stripes = 1;
  1368. int sub_stripes = 0;
  1369. int looped = 0;
  1370. int ret;
  1371. int index;
  1372. int stripe_len = 64 * 1024;
  1373. struct btrfs_key key;
  1374. if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
  1375. (type & BTRFS_BLOCK_GROUP_DUP)) {
  1376. WARN_ON(1);
  1377. type &= ~BTRFS_BLOCK_GROUP_DUP;
  1378. }
  1379. dev_list = &extent_root->fs_info->fs_devices->alloc_list;
  1380. if (list_empty(dev_list))
  1381. return -ENOSPC;
  1382. if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
  1383. num_stripes = extent_root->fs_info->fs_devices->open_devices;
  1384. min_stripes = 2;
  1385. }
  1386. if (type & (BTRFS_BLOCK_GROUP_DUP)) {
  1387. num_stripes = 2;
  1388. min_stripes = 2;
  1389. }
  1390. if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
  1391. num_stripes = min_t(u64, 2,
  1392. extent_root->fs_info->fs_devices->open_devices);
  1393. if (num_stripes < 2)
  1394. return -ENOSPC;
  1395. min_stripes = 2;
  1396. }
  1397. if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
  1398. num_stripes = extent_root->fs_info->fs_devices->open_devices;
  1399. if (num_stripes < 4)
  1400. return -ENOSPC;
  1401. num_stripes &= ~(u32)1;
  1402. sub_stripes = 2;
  1403. min_stripes = 4;
  1404. }
  1405. if (type & BTRFS_BLOCK_GROUP_DATA) {
  1406. max_chunk_size = 10 * calc_size;
  1407. min_stripe_size = 64 * 1024 * 1024;
  1408. } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
  1409. max_chunk_size = 4 * calc_size;
  1410. min_stripe_size = 32 * 1024 * 1024;
  1411. } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1412. calc_size = 8 * 1024 * 1024;
  1413. max_chunk_size = calc_size * 2;
  1414. min_stripe_size = 1 * 1024 * 1024;
  1415. }
  1416. path = btrfs_alloc_path();
  1417. if (!path)
  1418. return -ENOMEM;
  1419. /* we don't want a chunk larger than 10% of the FS */
  1420. percent_max = div_factor(btrfs_super_total_bytes(&info->super_copy), 1);
  1421. max_chunk_size = min(percent_max, max_chunk_size);
  1422. again:
  1423. if (calc_size * num_stripes > max_chunk_size) {
  1424. calc_size = max_chunk_size;
  1425. do_div(calc_size, num_stripes);
  1426. do_div(calc_size, stripe_len);
  1427. calc_size *= stripe_len;
  1428. }
  1429. /* we don't want tiny stripes */
  1430. calc_size = max_t(u64, min_stripe_size, calc_size);
  1431. do_div(calc_size, stripe_len);
  1432. calc_size *= stripe_len;
  1433. INIT_LIST_HEAD(&private_devs);
  1434. cur = dev_list->next;
  1435. index = 0;
  1436. if (type & BTRFS_BLOCK_GROUP_DUP)
  1437. min_free = calc_size * 2;
  1438. else
  1439. min_free = calc_size;
  1440. /* we add 1MB because we never use the first 1MB of the device */
  1441. min_free += 1024 * 1024;
  1442. /* build a private list of devices we will allocate from */
  1443. while(index < num_stripes) {
  1444. device = list_entry(cur, struct btrfs_device, dev_alloc_list);
  1445. if (device->total_bytes > device->bytes_used)
  1446. avail = device->total_bytes - device->bytes_used;
  1447. else
  1448. avail = 0;
  1449. cur = cur->next;
  1450. if (device->in_fs_metadata && avail >= min_free) {
  1451. u64 ignored_start = 0;
  1452. ret = find_free_dev_extent(trans, device, path,
  1453. min_free,
  1454. &ignored_start);
  1455. if (ret == 0) {
  1456. list_move_tail(&device->dev_alloc_list,
  1457. &private_devs);
  1458. index++;
  1459. if (type & BTRFS_BLOCK_GROUP_DUP)
  1460. index++;
  1461. }
  1462. } else if (device->in_fs_metadata && avail > max_avail)
  1463. max_avail = avail;
  1464. if (cur == dev_list)
  1465. break;
  1466. }
  1467. if (index < num_stripes) {
  1468. list_splice(&private_devs, dev_list);
  1469. if (index >= min_stripes) {
  1470. num_stripes = index;
  1471. if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
  1472. num_stripes /= sub_stripes;
  1473. num_stripes *= sub_stripes;
  1474. }
  1475. looped = 1;
  1476. goto again;
  1477. }
  1478. if (!looped && max_avail > 0) {
  1479. looped = 1;
  1480. calc_size = max_avail;
  1481. goto again;
  1482. }
  1483. btrfs_free_path(path);
  1484. return -ENOSPC;
  1485. }
  1486. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  1487. key.type = BTRFS_CHUNK_ITEM_KEY;
  1488. ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  1489. &key.offset);
  1490. if (ret) {
  1491. btrfs_free_path(path);
  1492. return ret;
  1493. }
  1494. chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
  1495. if (!chunk) {
  1496. btrfs_free_path(path);
  1497. return -ENOMEM;
  1498. }
  1499. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  1500. if (!map) {
  1501. kfree(chunk);
  1502. btrfs_free_path(path);
  1503. return -ENOMEM;
  1504. }
  1505. btrfs_free_path(path);
  1506. path = NULL;
  1507. stripes = &chunk->stripe;
  1508. *num_bytes = chunk_bytes_by_type(type, calc_size,
  1509. num_stripes, sub_stripes);
  1510. index = 0;
  1511. while(index < num_stripes) {
  1512. struct btrfs_stripe *stripe;
  1513. BUG_ON(list_empty(&private_devs));
  1514. cur = private_devs.next;
  1515. device = list_entry(cur, struct btrfs_device, dev_alloc_list);
  1516. /* loop over this device again if we're doing a dup group */
  1517. if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
  1518. (index == num_stripes - 1))
  1519. list_move_tail(&device->dev_alloc_list, dev_list);
  1520. ret = btrfs_alloc_dev_extent(trans, device,
  1521. info->chunk_root->root_key.objectid,
  1522. BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
  1523. calc_size, &dev_offset);
  1524. BUG_ON(ret);
  1525. device->bytes_used += calc_size;
  1526. ret = btrfs_update_device(trans, device);
  1527. BUG_ON(ret);
  1528. map->stripes[index].dev = device;
  1529. map->stripes[index].physical = dev_offset;
  1530. stripe = stripes + index;
  1531. btrfs_set_stack_stripe_devid(stripe, device->devid);
  1532. btrfs_set_stack_stripe_offset(stripe, dev_offset);
  1533. memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
  1534. physical = dev_offset;
  1535. index++;
  1536. }
  1537. BUG_ON(!list_empty(&private_devs));
  1538. /* key was set above */
  1539. btrfs_set_stack_chunk_length(chunk, *num_bytes);
  1540. btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
  1541. btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
  1542. btrfs_set_stack_chunk_type(chunk, type);
  1543. btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
  1544. btrfs_set_stack_chunk_io_align(chunk, stripe_len);
  1545. btrfs_set_stack_chunk_io_width(chunk, stripe_len);
  1546. btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
  1547. btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
  1548. map->sector_size = extent_root->sectorsize;
  1549. map->stripe_len = stripe_len;
  1550. map->io_align = stripe_len;
  1551. map->io_width = stripe_len;
  1552. map->type = type;
  1553. map->num_stripes = num_stripes;
  1554. map->sub_stripes = sub_stripes;
  1555. ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
  1556. btrfs_chunk_item_size(num_stripes));
  1557. BUG_ON(ret);
  1558. *start = key.offset;;
  1559. em = alloc_extent_map(GFP_NOFS);
  1560. if (!em)
  1561. return -ENOMEM;
  1562. em->bdev = (struct block_device *)map;
  1563. em->start = key.offset;
  1564. em->len = *num_bytes;
  1565. em->block_start = 0;
  1566. if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1567. ret = btrfs_add_system_chunk(trans, chunk_root, &key,
  1568. chunk, btrfs_chunk_item_size(num_stripes));
  1569. BUG_ON(ret);
  1570. }
  1571. kfree(chunk);
  1572. em_tree = &extent_root->fs_info->mapping_tree.map_tree;
  1573. spin_lock(&em_tree->lock);
  1574. ret = add_extent_mapping(em_tree, em);
  1575. spin_unlock(&em_tree->lock);
  1576. BUG_ON(ret);
  1577. free_extent_map(em);
  1578. return ret;
  1579. }
  1580. void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
  1581. {
  1582. extent_map_tree_init(&tree->map_tree, GFP_NOFS);
  1583. }
  1584. void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
  1585. {
  1586. struct extent_map *em;
  1587. while(1) {
  1588. spin_lock(&tree->map_tree.lock);
  1589. em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
  1590. if (em)
  1591. remove_extent_mapping(&tree->map_tree, em);
  1592. spin_unlock(&tree->map_tree.lock);
  1593. if (!em)
  1594. break;
  1595. kfree(em->bdev);
  1596. /* once for us */
  1597. free_extent_map(em);
  1598. /* once for the tree */
  1599. free_extent_map(em);
  1600. }
  1601. }
  1602. int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
  1603. {
  1604. struct extent_map *em;
  1605. struct map_lookup *map;
  1606. struct extent_map_tree *em_tree = &map_tree->map_tree;
  1607. int ret;
  1608. spin_lock(&em_tree->lock);
  1609. em = lookup_extent_mapping(em_tree, logical, len);
  1610. spin_unlock(&em_tree->lock);
  1611. BUG_ON(!em);
  1612. BUG_ON(em->start > logical || em->start + em->len < logical);
  1613. map = (struct map_lookup *)em->bdev;
  1614. if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
  1615. ret = map->num_stripes;
  1616. else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  1617. ret = map->sub_stripes;
  1618. else
  1619. ret = 1;
  1620. free_extent_map(em);
  1621. return ret;
  1622. }
  1623. static int find_live_mirror(struct map_lookup *map, int first, int num,
  1624. int optimal)
  1625. {
  1626. int i;
  1627. if (map->stripes[optimal].dev->bdev)
  1628. return optimal;
  1629. for (i = first; i < first + num; i++) {
  1630. if (map->stripes[i].dev->bdev)
  1631. return i;
  1632. }
  1633. /* we couldn't find one that doesn't fail. Just return something
  1634. * and the io error handling code will clean up eventually
  1635. */
  1636. return optimal;
  1637. }
  1638. static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  1639. u64 logical, u64 *length,
  1640. struct btrfs_multi_bio **multi_ret,
  1641. int mirror_num, struct page *unplug_page)
  1642. {
  1643. struct extent_map *em;
  1644. struct map_lookup *map;
  1645. struct extent_map_tree *em_tree = &map_tree->map_tree;
  1646. u64 offset;
  1647. u64 stripe_offset;
  1648. u64 stripe_nr;
  1649. int stripes_allocated = 8;
  1650. int stripes_required = 1;
  1651. int stripe_index;
  1652. int i;
  1653. int num_stripes;
  1654. int max_errors = 0;
  1655. struct btrfs_multi_bio *multi = NULL;
  1656. if (multi_ret && !(rw & (1 << BIO_RW))) {
  1657. stripes_allocated = 1;
  1658. }
  1659. again:
  1660. if (multi_ret) {
  1661. multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
  1662. GFP_NOFS);
  1663. if (!multi)
  1664. return -ENOMEM;
  1665. atomic_set(&multi->error, 0);
  1666. }
  1667. spin_lock(&em_tree->lock);
  1668. em = lookup_extent_mapping(em_tree, logical, *length);
  1669. spin_unlock(&em_tree->lock);
  1670. if (!em && unplug_page)
  1671. return 0;
  1672. if (!em) {
  1673. printk("unable to find logical %Lu len %Lu\n", logical, *length);
  1674. BUG();
  1675. }
  1676. BUG_ON(em->start > logical || em->start + em->len < logical);
  1677. map = (struct map_lookup *)em->bdev;
  1678. offset = logical - em->start;
  1679. if (mirror_num > map->num_stripes)
  1680. mirror_num = 0;
  1681. /* if our multi bio struct is too small, back off and try again */
  1682. if (rw & (1 << BIO_RW)) {
  1683. if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
  1684. BTRFS_BLOCK_GROUP_DUP)) {
  1685. stripes_required = map->num_stripes;
  1686. max_errors = 1;
  1687. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  1688. stripes_required = map->sub_stripes;
  1689. max_errors = 1;
  1690. }
  1691. }
  1692. if (multi_ret && rw == WRITE &&
  1693. stripes_allocated < stripes_required) {
  1694. stripes_allocated = map->num_stripes;
  1695. free_extent_map(em);
  1696. kfree(multi);
  1697. goto again;
  1698. }
  1699. stripe_nr = offset;
  1700. /*
  1701. * stripe_nr counts the total number of stripes we have to stride
  1702. * to get to this block
  1703. */
  1704. do_div(stripe_nr, map->stripe_len);
  1705. stripe_offset = stripe_nr * map->stripe_len;
  1706. BUG_ON(offset < stripe_offset);
  1707. /* stripe_offset is the offset of this block in its stripe*/
  1708. stripe_offset = offset - stripe_offset;
  1709. if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
  1710. BTRFS_BLOCK_GROUP_RAID10 |
  1711. BTRFS_BLOCK_GROUP_DUP)) {
  1712. /* we limit the length of each bio to what fits in a stripe */
  1713. *length = min_t(u64, em->len - offset,
  1714. map->stripe_len - stripe_offset);
  1715. } else {
  1716. *length = em->len - offset;
  1717. }
  1718. if (!multi_ret && !unplug_page)
  1719. goto out;
  1720. num_stripes = 1;
  1721. stripe_index = 0;
  1722. if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  1723. if (unplug_page || (rw & (1 << BIO_RW)))
  1724. num_stripes = map->num_stripes;
  1725. else if (mirror_num)
  1726. stripe_index = mirror_num - 1;
  1727. else {
  1728. stripe_index = find_live_mirror(map, 0,
  1729. map->num_stripes,
  1730. current->pid % map->num_stripes);
  1731. }
  1732. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  1733. if (rw & (1 << BIO_RW))
  1734. num_stripes = map->num_stripes;
  1735. else if (mirror_num)
  1736. stripe_index = mirror_num - 1;
  1737. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  1738. int factor = map->num_stripes / map->sub_stripes;
  1739. stripe_index = do_div(stripe_nr, factor);
  1740. stripe_index *= map->sub_stripes;
  1741. if (unplug_page || (rw & (1 << BIO_RW)))
  1742. num_stripes = map->sub_stripes;
  1743. else if (mirror_num)
  1744. stripe_index += mirror_num - 1;
  1745. else {
  1746. stripe_index = find_live_mirror(map, stripe_index,
  1747. map->sub_stripes, stripe_index +
  1748. current->pid % map->sub_stripes);
  1749. }
  1750. } else {
  1751. /*
  1752. * after this do_div call, stripe_nr is the number of stripes
  1753. * on this device we have to walk to find the data, and
  1754. * stripe_index is the number of our device in the stripe array
  1755. */
  1756. stripe_index = do_div(stripe_nr, map->num_stripes);
  1757. }
  1758. BUG_ON(stripe_index >= map->num_stripes);
  1759. for (i = 0; i < num_stripes; i++) {
  1760. if (unplug_page) {
  1761. struct btrfs_device *device;
  1762. struct backing_dev_info *bdi;
  1763. device = map->stripes[stripe_index].dev;
  1764. if (device->bdev) {
  1765. bdi = blk_get_backing_dev_info(device->bdev);
  1766. if (bdi->unplug_io_fn) {
  1767. bdi->unplug_io_fn(bdi, unplug_page);
  1768. }
  1769. }
  1770. } else {
  1771. multi->stripes[i].physical =
  1772. map->stripes[stripe_index].physical +
  1773. stripe_offset + stripe_nr * map->stripe_len;
  1774. multi->stripes[i].dev = map->stripes[stripe_index].dev;
  1775. }
  1776. stripe_index++;
  1777. }
  1778. if (multi_ret) {
  1779. *multi_ret = multi;
  1780. multi->num_stripes = num_stripes;
  1781. multi->max_errors = max_errors;
  1782. }
  1783. out:
  1784. free_extent_map(em);
  1785. return 0;
  1786. }
  1787. int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  1788. u64 logical, u64 *length,
  1789. struct btrfs_multi_bio **multi_ret, int mirror_num)
  1790. {
  1791. return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
  1792. mirror_num, NULL);
  1793. }
  1794. int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
  1795. u64 logical, struct page *page)
  1796. {
  1797. u64 length = PAGE_CACHE_SIZE;
  1798. return __btrfs_map_block(map_tree, READ, logical, &length,
  1799. NULL, 0, page);
  1800. }
  1801. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
  1802. static void end_bio_multi_stripe(struct bio *bio, int err)
  1803. #else
  1804. static int end_bio_multi_stripe(struct bio *bio,
  1805. unsigned int bytes_done, int err)
  1806. #endif
  1807. {
  1808. struct btrfs_multi_bio *multi = bio->bi_private;
  1809. int is_orig_bio = 0;
  1810. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  1811. if (bio->bi_size)
  1812. return 1;
  1813. #endif
  1814. if (err)
  1815. atomic_inc(&multi->error);
  1816. if (bio == multi->orig_bio)
  1817. is_orig_bio = 1;
  1818. if (atomic_dec_and_test(&multi->stripes_pending)) {
  1819. if (!is_orig_bio) {
  1820. bio_put(bio);
  1821. bio = multi->orig_bio;
  1822. }
  1823. bio->bi_private = multi->private;
  1824. bio->bi_end_io = multi->end_io;
  1825. /* only send an error to the higher layers if it is
  1826. * beyond the tolerance of the multi-bio
  1827. */
  1828. if (atomic_read(&multi->error) > multi->max_errors) {
  1829. err = -EIO;
  1830. } else if (err) {
  1831. /*
  1832. * this bio is actually up to date, we didn't
  1833. * go over the max number of errors
  1834. */
  1835. set_bit(BIO_UPTODATE, &bio->bi_flags);
  1836. err = 0;
  1837. }
  1838. kfree(multi);
  1839. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  1840. bio_endio(bio, bio->bi_size, err);
  1841. #else
  1842. bio_endio(bio, err);
  1843. #endif
  1844. } else if (!is_orig_bio) {
  1845. bio_put(bio);
  1846. }
  1847. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  1848. return 0;
  1849. #endif
  1850. }
  1851. struct async_sched {
  1852. struct bio *bio;
  1853. int rw;
  1854. struct btrfs_fs_info *info;
  1855. struct btrfs_work work;
  1856. };
  1857. /*
  1858. * see run_scheduled_bios for a description of why bios are collected for
  1859. * async submit.
  1860. *
  1861. * This will add one bio to the pending list for a device and make sure
  1862. * the work struct is scheduled.
  1863. */
  1864. static int noinline schedule_bio(struct btrfs_root *root,
  1865. struct btrfs_device *device,
  1866. int rw, struct bio *bio)
  1867. {
  1868. int should_queue = 1;
  1869. /* don't bother with additional async steps for reads, right now */
  1870. if (!(rw & (1 << BIO_RW))) {
  1871. bio_get(bio);
  1872. submit_bio(rw, bio);
  1873. bio_put(bio);
  1874. return 0;
  1875. }
  1876. /*
  1877. * nr_async_bios allows us to reliably return congestion to the
  1878. * higher layers. Otherwise, the async bio makes it appear we have
  1879. * made progress against dirty pages when we've really just put it
  1880. * on a queue for later
  1881. */
  1882. atomic_inc(&root->fs_info->nr_async_bios);
  1883. WARN_ON(bio->bi_next);
  1884. bio->bi_next = NULL;
  1885. bio->bi_rw |= rw;
  1886. spin_lock(&device->io_lock);
  1887. if (device->pending_bio_tail)
  1888. device->pending_bio_tail->bi_next = bio;
  1889. device->pending_bio_tail = bio;
  1890. if (!device->pending_bios)
  1891. device->pending_bios = bio;
  1892. if (device->running_pending)
  1893. should_queue = 0;
  1894. spin_unlock(&device->io_lock);
  1895. if (should_queue)
  1896. btrfs_queue_worker(&root->fs_info->submit_workers,
  1897. &device->work);
  1898. return 0;
  1899. }
  1900. int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
  1901. int mirror_num, int async_submit)
  1902. {
  1903. struct btrfs_mapping_tree *map_tree;
  1904. struct btrfs_device *dev;
  1905. struct bio *first_bio = bio;
  1906. u64 logical = bio->bi_sector << 9;
  1907. u64 length = 0;
  1908. u64 map_length;
  1909. struct btrfs_multi_bio *multi = NULL;
  1910. int ret;
  1911. int dev_nr = 0;
  1912. int total_devs = 1;
  1913. length = bio->bi_size;
  1914. map_tree = &root->fs_info->mapping_tree;
  1915. map_length = length;
  1916. ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
  1917. mirror_num);
  1918. BUG_ON(ret);
  1919. total_devs = multi->num_stripes;
  1920. if (map_length < length) {
  1921. printk("mapping failed logical %Lu bio len %Lu "
  1922. "len %Lu\n", logical, length, map_length);
  1923. BUG();
  1924. }
  1925. multi->end_io = first_bio->bi_end_io;
  1926. multi->private = first_bio->bi_private;
  1927. multi->orig_bio = first_bio;
  1928. atomic_set(&multi->stripes_pending, multi->num_stripes);
  1929. while(dev_nr < total_devs) {
  1930. if (total_devs > 1) {
  1931. if (dev_nr < total_devs - 1) {
  1932. bio = bio_clone(first_bio, GFP_NOFS);
  1933. BUG_ON(!bio);
  1934. } else {
  1935. bio = first_bio;
  1936. }
  1937. bio->bi_private = multi;
  1938. bio->bi_end_io = end_bio_multi_stripe;
  1939. }
  1940. bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
  1941. dev = multi->stripes[dev_nr].dev;
  1942. if (dev && dev->bdev) {
  1943. bio->bi_bdev = dev->bdev;
  1944. if (async_submit)
  1945. schedule_bio(root, dev, rw, bio);
  1946. else
  1947. submit_bio(rw, bio);
  1948. } else {
  1949. bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
  1950. bio->bi_sector = logical >> 9;
  1951. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  1952. bio_endio(bio, bio->bi_size, -EIO);
  1953. #else
  1954. bio_endio(bio, -EIO);
  1955. #endif
  1956. }
  1957. dev_nr++;
  1958. }
  1959. if (total_devs == 1)
  1960. kfree(multi);
  1961. return 0;
  1962. }
  1963. struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
  1964. u8 *uuid)
  1965. {
  1966. struct list_head *head = &root->fs_info->fs_devices->devices;
  1967. return __find_device(head, devid, uuid);
  1968. }
  1969. static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
  1970. u64 devid, u8 *dev_uuid)
  1971. {
  1972. struct btrfs_device *device;
  1973. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  1974. device = kzalloc(sizeof(*device), GFP_NOFS);
  1975. list_add(&device->dev_list,
  1976. &fs_devices->devices);
  1977. list_add(&device->dev_alloc_list,
  1978. &fs_devices->alloc_list);
  1979. device->barriers = 1;
  1980. device->dev_root = root->fs_info->dev_root;
  1981. device->devid = devid;
  1982. device->work.func = pending_bios_fn;
  1983. fs_devices->num_devices++;
  1984. spin_lock_init(&device->io_lock);
  1985. memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
  1986. return device;
  1987. }
  1988. static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
  1989. struct extent_buffer *leaf,
  1990. struct btrfs_chunk *chunk)
  1991. {
  1992. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  1993. struct map_lookup *map;
  1994. struct extent_map *em;
  1995. u64 logical;
  1996. u64 length;
  1997. u64 devid;
  1998. u8 uuid[BTRFS_UUID_SIZE];
  1999. int num_stripes;
  2000. int ret;
  2001. int i;
  2002. logical = key->offset;
  2003. length = btrfs_chunk_length(leaf, chunk);
  2004. spin_lock(&map_tree->map_tree.lock);
  2005. em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
  2006. spin_unlock(&map_tree->map_tree.lock);
  2007. /* already mapped? */
  2008. if (em && em->start <= logical && em->start + em->len > logical) {
  2009. free_extent_map(em);
  2010. return 0;
  2011. } else if (em) {
  2012. free_extent_map(em);
  2013. }
  2014. map = kzalloc(sizeof(*map), GFP_NOFS);
  2015. if (!map)
  2016. return -ENOMEM;
  2017. em = alloc_extent_map(GFP_NOFS);
  2018. if (!em)
  2019. return -ENOMEM;
  2020. num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  2021. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  2022. if (!map) {
  2023. free_extent_map(em);
  2024. return -ENOMEM;
  2025. }
  2026. em->bdev = (struct block_device *)map;
  2027. em->start = logical;
  2028. em->len = length;
  2029. em->block_start = 0;
  2030. map->num_stripes = num_stripes;
  2031. map->io_width = btrfs_chunk_io_width(leaf, chunk);
  2032. map->io_align = btrfs_chunk_io_align(leaf, chunk);
  2033. map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
  2034. map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
  2035. map->type = btrfs_chunk_type(leaf, chunk);
  2036. map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
  2037. for (i = 0; i < num_stripes; i++) {
  2038. map->stripes[i].physical =
  2039. btrfs_stripe_offset_nr(leaf, chunk, i);
  2040. devid = btrfs_stripe_devid_nr(leaf, chunk, i);
  2041. read_extent_buffer(leaf, uuid, (unsigned long)
  2042. btrfs_stripe_dev_uuid_nr(chunk, i),
  2043. BTRFS_UUID_SIZE);
  2044. map->stripes[i].dev = btrfs_find_device(root, devid, uuid);
  2045. if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
  2046. kfree(map);
  2047. free_extent_map(em);
  2048. return -EIO;
  2049. }
  2050. if (!map->stripes[i].dev) {
  2051. map->stripes[i].dev =
  2052. add_missing_dev(root, devid, uuid);
  2053. if (!map->stripes[i].dev) {
  2054. kfree(map);
  2055. free_extent_map(em);
  2056. return -EIO;
  2057. }
  2058. }
  2059. map->stripes[i].dev->in_fs_metadata = 1;
  2060. }
  2061. spin_lock(&map_tree->map_tree.lock);
  2062. ret = add_extent_mapping(&map_tree->map_tree, em);
  2063. spin_unlock(&map_tree->map_tree.lock);
  2064. BUG_ON(ret);
  2065. free_extent_map(em);
  2066. return 0;
  2067. }
  2068. static int fill_device_from_item(struct extent_buffer *leaf,
  2069. struct btrfs_dev_item *dev_item,
  2070. struct btrfs_device *device)
  2071. {
  2072. unsigned long ptr;
  2073. device->devid = btrfs_device_id(leaf, dev_item);
  2074. device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
  2075. device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
  2076. device->type = btrfs_device_type(leaf, dev_item);
  2077. device->io_align = btrfs_device_io_align(leaf, dev_item);
  2078. device->io_width = btrfs_device_io_width(leaf, dev_item);
  2079. device->sector_size = btrfs_device_sector_size(leaf, dev_item);
  2080. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  2081. read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  2082. return 0;
  2083. }
  2084. static int read_one_dev(struct btrfs_root *root,
  2085. struct extent_buffer *leaf,
  2086. struct btrfs_dev_item *dev_item)
  2087. {
  2088. struct btrfs_device *device;
  2089. u64 devid;
  2090. int ret;
  2091. u8 dev_uuid[BTRFS_UUID_SIZE];
  2092. devid = btrfs_device_id(leaf, dev_item);
  2093. read_extent_buffer(leaf, dev_uuid,
  2094. (unsigned long)btrfs_device_uuid(dev_item),
  2095. BTRFS_UUID_SIZE);
  2096. device = btrfs_find_device(root, devid, dev_uuid);
  2097. if (!device) {
  2098. printk("warning devid %Lu missing\n", devid);
  2099. device = add_missing_dev(root, devid, dev_uuid);
  2100. if (!device)
  2101. return -ENOMEM;
  2102. }
  2103. fill_device_from_item(leaf, dev_item, device);
  2104. device->dev_root = root->fs_info->dev_root;
  2105. device->in_fs_metadata = 1;
  2106. ret = 0;
  2107. #if 0
  2108. ret = btrfs_open_device(device);
  2109. if (ret) {
  2110. kfree(device);
  2111. }
  2112. #endif
  2113. return ret;
  2114. }
  2115. int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
  2116. {
  2117. struct btrfs_dev_item *dev_item;
  2118. dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
  2119. dev_item);
  2120. return read_one_dev(root, buf, dev_item);
  2121. }
  2122. int btrfs_read_sys_array(struct btrfs_root *root)
  2123. {
  2124. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  2125. struct extent_buffer *sb;
  2126. struct btrfs_disk_key *disk_key;
  2127. struct btrfs_chunk *chunk;
  2128. u8 *ptr;
  2129. unsigned long sb_ptr;
  2130. int ret = 0;
  2131. u32 num_stripes;
  2132. u32 array_size;
  2133. u32 len = 0;
  2134. u32 cur;
  2135. struct btrfs_key key;
  2136. sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
  2137. BTRFS_SUPER_INFO_SIZE);
  2138. if (!sb)
  2139. return -ENOMEM;
  2140. btrfs_set_buffer_uptodate(sb);
  2141. write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
  2142. array_size = btrfs_super_sys_array_size(super_copy);
  2143. ptr = super_copy->sys_chunk_array;
  2144. sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
  2145. cur = 0;
  2146. while (cur < array_size) {
  2147. disk_key = (struct btrfs_disk_key *)ptr;
  2148. btrfs_disk_key_to_cpu(&key, disk_key);
  2149. len = sizeof(*disk_key); ptr += len;
  2150. sb_ptr += len;
  2151. cur += len;
  2152. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  2153. chunk = (struct btrfs_chunk *)sb_ptr;
  2154. ret = read_one_chunk(root, &key, sb, chunk);
  2155. if (ret)
  2156. break;
  2157. num_stripes = btrfs_chunk_num_stripes(sb, chunk);
  2158. len = btrfs_chunk_item_size(num_stripes);
  2159. } else {
  2160. ret = -EIO;
  2161. break;
  2162. }
  2163. ptr += len;
  2164. sb_ptr += len;
  2165. cur += len;
  2166. }
  2167. free_extent_buffer(sb);
  2168. return ret;
  2169. }
  2170. int btrfs_read_chunk_tree(struct btrfs_root *root)
  2171. {
  2172. struct btrfs_path *path;
  2173. struct extent_buffer *leaf;
  2174. struct btrfs_key key;
  2175. struct btrfs_key found_key;
  2176. int ret;
  2177. int slot;
  2178. root = root->fs_info->chunk_root;
  2179. path = btrfs_alloc_path();
  2180. if (!path)
  2181. return -ENOMEM;
  2182. /* first we search for all of the device items, and then we
  2183. * read in all of the chunk items. This way we can create chunk
  2184. * mappings that reference all of the devices that are afound
  2185. */
  2186. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  2187. key.offset = 0;
  2188. key.type = 0;
  2189. again:
  2190. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2191. while(1) {
  2192. leaf = path->nodes[0];
  2193. slot = path->slots[0];
  2194. if (slot >= btrfs_header_nritems(leaf)) {
  2195. ret = btrfs_next_leaf(root, path);
  2196. if (ret == 0)
  2197. continue;
  2198. if (ret < 0)
  2199. goto error;
  2200. break;
  2201. }
  2202. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  2203. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  2204. if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
  2205. break;
  2206. if (found_key.type == BTRFS_DEV_ITEM_KEY) {
  2207. struct btrfs_dev_item *dev_item;
  2208. dev_item = btrfs_item_ptr(leaf, slot,
  2209. struct btrfs_dev_item);
  2210. ret = read_one_dev(root, leaf, dev_item);
  2211. BUG_ON(ret);
  2212. }
  2213. } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
  2214. struct btrfs_chunk *chunk;
  2215. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  2216. ret = read_one_chunk(root, &found_key, leaf, chunk);
  2217. }
  2218. path->slots[0]++;
  2219. }
  2220. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  2221. key.objectid = 0;
  2222. btrfs_release_path(root, path);
  2223. goto again;
  2224. }
  2225. btrfs_free_path(path);
  2226. ret = 0;
  2227. error:
  2228. return ret;
  2229. }