volumes.c 63 KB

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