volumes.c 62 KB

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