volumes.c 54 KB

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