volumes.c 58 KB

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