volumes.c 54 KB

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