volumes.c 58 KB

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