volumes.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340
  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_total_bytes(&root->fs_info->super_copy);
  662. btrfs_set_super_total_bytes(&root->fs_info->super_copy,
  663. total_bytes - device->total_bytes);
  664. total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
  665. btrfs_set_super_num_devices(&root->fs_info->super_copy,
  666. total_bytes - 1);
  667. out:
  668. btrfs_free_path(path);
  669. btrfs_commit_transaction(trans, root);
  670. return ret;
  671. }
  672. int btrfs_rm_device(struct btrfs_root *root, char *device_path)
  673. {
  674. struct btrfs_device *device;
  675. struct block_device *bdev;
  676. struct buffer_head *bh = NULL;
  677. struct btrfs_super_block *disk_super;
  678. u64 all_avail;
  679. u64 devid;
  680. int ret = 0;
  681. mutex_lock(&root->fs_info->fs_mutex);
  682. mutex_lock(&uuid_mutex);
  683. all_avail = root->fs_info->avail_data_alloc_bits |
  684. root->fs_info->avail_system_alloc_bits |
  685. root->fs_info->avail_metadata_alloc_bits;
  686. if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
  687. btrfs_super_num_devices(&root->fs_info->super_copy) <= 4) {
  688. printk("btrfs: unable to go below four devices on raid10\n");
  689. ret = -EINVAL;
  690. goto out;
  691. }
  692. if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
  693. btrfs_super_num_devices(&root->fs_info->super_copy) <= 2) {
  694. printk("btrfs: unable to go below two devices on raid1\n");
  695. ret = -EINVAL;
  696. goto out;
  697. }
  698. if (strcmp(device_path, "missing") == 0) {
  699. struct list_head *cur;
  700. struct list_head *devices;
  701. struct btrfs_device *tmp;
  702. device = NULL;
  703. devices = &root->fs_info->fs_devices->devices;
  704. list_for_each(cur, devices) {
  705. tmp = list_entry(cur, struct btrfs_device, dev_list);
  706. if (tmp->in_fs_metadata && !tmp->bdev) {
  707. device = tmp;
  708. break;
  709. }
  710. }
  711. bdev = NULL;
  712. bh = NULL;
  713. disk_super = NULL;
  714. if (!device) {
  715. printk("btrfs: no missing devices found to remove\n");
  716. goto out;
  717. }
  718. } else {
  719. bdev = open_bdev_excl(device_path, 0,
  720. root->fs_info->bdev_holder);
  721. if (IS_ERR(bdev)) {
  722. ret = PTR_ERR(bdev);
  723. goto out;
  724. }
  725. bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
  726. if (!bh) {
  727. ret = -EIO;
  728. goto error_close;
  729. }
  730. disk_super = (struct btrfs_super_block *)bh->b_data;
  731. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  732. sizeof(disk_super->magic))) {
  733. ret = -ENOENT;
  734. goto error_brelse;
  735. }
  736. if (memcmp(disk_super->fsid, root->fs_info->fsid,
  737. BTRFS_FSID_SIZE)) {
  738. ret = -ENOENT;
  739. goto error_brelse;
  740. }
  741. devid = le64_to_cpu(disk_super->dev_item.devid);
  742. device = btrfs_find_device(root, devid, NULL);
  743. if (!device) {
  744. ret = -ENOENT;
  745. goto error_brelse;
  746. }
  747. }
  748. root->fs_info->fs_devices->num_devices--;
  749. ret = btrfs_shrink_device(device, 0);
  750. if (ret)
  751. goto error_brelse;
  752. ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
  753. if (ret)
  754. goto error_brelse;
  755. if (bh) {
  756. /* make sure this device isn't detected as part of
  757. * the FS anymore
  758. */
  759. memset(&disk_super->magic, 0, sizeof(disk_super->magic));
  760. set_buffer_dirty(bh);
  761. sync_dirty_buffer(bh);
  762. brelse(bh);
  763. }
  764. if (device->bdev) {
  765. /* one close for the device struct or super_block */
  766. close_bdev_excl(device->bdev);
  767. root->fs_info->fs_devices->open_devices--;
  768. }
  769. if (bdev) {
  770. /* one close for us */
  771. close_bdev_excl(bdev);
  772. }
  773. kfree(device->name);
  774. kfree(device);
  775. ret = 0;
  776. goto out;
  777. error_brelse:
  778. brelse(bh);
  779. error_close:
  780. if (bdev)
  781. close_bdev_excl(bdev);
  782. out:
  783. mutex_unlock(&uuid_mutex);
  784. mutex_unlock(&root->fs_info->fs_mutex);
  785. return ret;
  786. }
  787. int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
  788. {
  789. struct btrfs_trans_handle *trans;
  790. struct btrfs_device *device;
  791. struct block_device *bdev;
  792. struct list_head *cur;
  793. struct list_head *devices;
  794. u64 total_bytes;
  795. int ret = 0;
  796. bdev = open_bdev_excl(device_path, 0, root->fs_info->bdev_holder);
  797. if (!bdev) {
  798. return -EIO;
  799. }
  800. mutex_lock(&root->fs_info->fs_mutex);
  801. trans = btrfs_start_transaction(root, 1);
  802. devices = &root->fs_info->fs_devices->devices;
  803. list_for_each(cur, devices) {
  804. device = list_entry(cur, struct btrfs_device, dev_list);
  805. if (device->bdev == bdev) {
  806. ret = -EEXIST;
  807. goto out;
  808. }
  809. }
  810. device = kzalloc(sizeof(*device), GFP_NOFS);
  811. if (!device) {
  812. /* we can safely leave the fs_devices entry around */
  813. ret = -ENOMEM;
  814. goto out_close_bdev;
  815. }
  816. device->barriers = 1;
  817. generate_random_uuid(device->uuid);
  818. spin_lock_init(&device->io_lock);
  819. device->name = kstrdup(device_path, GFP_NOFS);
  820. if (!device->name) {
  821. kfree(device);
  822. goto out_close_bdev;
  823. }
  824. device->io_width = root->sectorsize;
  825. device->io_align = root->sectorsize;
  826. device->sector_size = root->sectorsize;
  827. device->total_bytes = i_size_read(bdev->bd_inode);
  828. device->dev_root = root->fs_info->dev_root;
  829. device->bdev = bdev;
  830. device->in_fs_metadata = 1;
  831. ret = btrfs_add_device(trans, root, device);
  832. if (ret)
  833. goto out_close_bdev;
  834. total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
  835. btrfs_set_super_total_bytes(&root->fs_info->super_copy,
  836. total_bytes + device->total_bytes);
  837. total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
  838. btrfs_set_super_num_devices(&root->fs_info->super_copy,
  839. total_bytes + 1);
  840. list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
  841. list_add(&device->dev_alloc_list,
  842. &root->fs_info->fs_devices->alloc_list);
  843. root->fs_info->fs_devices->num_devices++;
  844. root->fs_info->fs_devices->open_devices++;
  845. out:
  846. btrfs_end_transaction(trans, root);
  847. mutex_unlock(&root->fs_info->fs_mutex);
  848. return ret;
  849. out_close_bdev:
  850. close_bdev_excl(bdev);
  851. goto out;
  852. }
  853. int btrfs_update_device(struct btrfs_trans_handle *trans,
  854. struct btrfs_device *device)
  855. {
  856. int ret;
  857. struct btrfs_path *path;
  858. struct btrfs_root *root;
  859. struct btrfs_dev_item *dev_item;
  860. struct extent_buffer *leaf;
  861. struct btrfs_key key;
  862. root = device->dev_root->fs_info->chunk_root;
  863. path = btrfs_alloc_path();
  864. if (!path)
  865. return -ENOMEM;
  866. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  867. key.type = BTRFS_DEV_ITEM_KEY;
  868. key.offset = device->devid;
  869. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  870. if (ret < 0)
  871. goto out;
  872. if (ret > 0) {
  873. ret = -ENOENT;
  874. goto out;
  875. }
  876. leaf = path->nodes[0];
  877. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  878. btrfs_set_device_id(leaf, dev_item, device->devid);
  879. btrfs_set_device_type(leaf, dev_item, device->type);
  880. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  881. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  882. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  883. btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
  884. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  885. btrfs_mark_buffer_dirty(leaf);
  886. out:
  887. btrfs_free_path(path);
  888. return ret;
  889. }
  890. int btrfs_grow_device(struct btrfs_trans_handle *trans,
  891. struct btrfs_device *device, u64 new_size)
  892. {
  893. struct btrfs_super_block *super_copy =
  894. &device->dev_root->fs_info->super_copy;
  895. u64 old_total = btrfs_super_total_bytes(super_copy);
  896. u64 diff = new_size - device->total_bytes;
  897. btrfs_set_super_total_bytes(super_copy, old_total + diff);
  898. return btrfs_update_device(trans, device);
  899. }
  900. static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
  901. struct btrfs_root *root,
  902. u64 chunk_tree, u64 chunk_objectid,
  903. u64 chunk_offset)
  904. {
  905. int ret;
  906. struct btrfs_path *path;
  907. struct btrfs_key key;
  908. root = root->fs_info->chunk_root;
  909. path = btrfs_alloc_path();
  910. if (!path)
  911. return -ENOMEM;
  912. key.objectid = chunk_objectid;
  913. key.offset = chunk_offset;
  914. key.type = BTRFS_CHUNK_ITEM_KEY;
  915. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  916. BUG_ON(ret);
  917. ret = btrfs_del_item(trans, root, path);
  918. BUG_ON(ret);
  919. btrfs_free_path(path);
  920. return 0;
  921. }
  922. int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
  923. chunk_offset)
  924. {
  925. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  926. struct btrfs_disk_key *disk_key;
  927. struct btrfs_chunk *chunk;
  928. u8 *ptr;
  929. int ret = 0;
  930. u32 num_stripes;
  931. u32 array_size;
  932. u32 len = 0;
  933. u32 cur;
  934. struct btrfs_key key;
  935. array_size = btrfs_super_sys_array_size(super_copy);
  936. ptr = super_copy->sys_chunk_array;
  937. cur = 0;
  938. while (cur < array_size) {
  939. disk_key = (struct btrfs_disk_key *)ptr;
  940. btrfs_disk_key_to_cpu(&key, disk_key);
  941. len = sizeof(*disk_key);
  942. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  943. chunk = (struct btrfs_chunk *)(ptr + len);
  944. num_stripes = btrfs_stack_chunk_num_stripes(chunk);
  945. len += btrfs_chunk_item_size(num_stripes);
  946. } else {
  947. ret = -EIO;
  948. break;
  949. }
  950. if (key.objectid == chunk_objectid &&
  951. key.offset == chunk_offset) {
  952. memmove(ptr, ptr + len, array_size - (cur + len));
  953. array_size -= len;
  954. btrfs_set_super_sys_array_size(super_copy, array_size);
  955. } else {
  956. ptr += len;
  957. cur += len;
  958. }
  959. }
  960. return ret;
  961. }
  962. int btrfs_relocate_chunk(struct btrfs_root *root,
  963. u64 chunk_tree, u64 chunk_objectid,
  964. u64 chunk_offset)
  965. {
  966. struct extent_map_tree *em_tree;
  967. struct btrfs_root *extent_root;
  968. struct btrfs_trans_handle *trans;
  969. struct extent_map *em;
  970. struct map_lookup *map;
  971. int ret;
  972. int i;
  973. printk("btrfs relocating chunk %llu\n",
  974. (unsigned long long)chunk_offset);
  975. root = root->fs_info->chunk_root;
  976. extent_root = root->fs_info->extent_root;
  977. em_tree = &root->fs_info->mapping_tree.map_tree;
  978. /* step one, relocate all the extents inside this chunk */
  979. ret = btrfs_shrink_extent_tree(extent_root, chunk_offset);
  980. BUG_ON(ret);
  981. trans = btrfs_start_transaction(root, 1);
  982. BUG_ON(!trans);
  983. /*
  984. * step two, delete the device extents and the
  985. * chunk tree entries
  986. */
  987. spin_lock(&em_tree->lock);
  988. em = lookup_extent_mapping(em_tree, chunk_offset, 1);
  989. spin_unlock(&em_tree->lock);
  990. BUG_ON(em->start > chunk_offset ||
  991. em->start + em->len < chunk_offset);
  992. map = (struct map_lookup *)em->bdev;
  993. for (i = 0; i < map->num_stripes; i++) {
  994. ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
  995. map->stripes[i].physical);
  996. BUG_ON(ret);
  997. if (map->stripes[i].dev) {
  998. ret = btrfs_update_device(trans, map->stripes[i].dev);
  999. BUG_ON(ret);
  1000. }
  1001. }
  1002. ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
  1003. chunk_offset);
  1004. BUG_ON(ret);
  1005. if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1006. ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
  1007. BUG_ON(ret);
  1008. }
  1009. spin_lock(&em_tree->lock);
  1010. remove_extent_mapping(em_tree, em);
  1011. kfree(map);
  1012. em->bdev = NULL;
  1013. /* once for the tree */
  1014. free_extent_map(em);
  1015. spin_unlock(&em_tree->lock);
  1016. /* once for us */
  1017. free_extent_map(em);
  1018. btrfs_end_transaction(trans, root);
  1019. return 0;
  1020. }
  1021. static u64 div_factor(u64 num, int factor)
  1022. {
  1023. if (factor == 10)
  1024. return num;
  1025. num *= factor;
  1026. do_div(num, 10);
  1027. return num;
  1028. }
  1029. int btrfs_balance(struct btrfs_root *dev_root)
  1030. {
  1031. int ret;
  1032. struct list_head *cur;
  1033. struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
  1034. struct btrfs_device *device;
  1035. u64 old_size;
  1036. u64 size_to_free;
  1037. struct btrfs_path *path;
  1038. struct btrfs_key key;
  1039. struct btrfs_chunk *chunk;
  1040. struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
  1041. struct btrfs_trans_handle *trans;
  1042. struct btrfs_key found_key;
  1043. dev_root = dev_root->fs_info->dev_root;
  1044. mutex_lock(&dev_root->fs_info->fs_mutex);
  1045. /* step one make some room on all the devices */
  1046. list_for_each(cur, devices) {
  1047. device = list_entry(cur, struct btrfs_device, dev_list);
  1048. old_size = device->total_bytes;
  1049. size_to_free = div_factor(old_size, 1);
  1050. size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
  1051. if (device->total_bytes - device->bytes_used > size_to_free)
  1052. continue;
  1053. ret = btrfs_shrink_device(device, old_size - size_to_free);
  1054. BUG_ON(ret);
  1055. trans = btrfs_start_transaction(dev_root, 1);
  1056. BUG_ON(!trans);
  1057. ret = btrfs_grow_device(trans, device, old_size);
  1058. BUG_ON(ret);
  1059. btrfs_end_transaction(trans, dev_root);
  1060. }
  1061. /* step two, relocate all the chunks */
  1062. path = btrfs_alloc_path();
  1063. BUG_ON(!path);
  1064. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  1065. key.offset = (u64)-1;
  1066. key.type = BTRFS_CHUNK_ITEM_KEY;
  1067. while(1) {
  1068. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  1069. if (ret < 0)
  1070. goto error;
  1071. /*
  1072. * this shouldn't happen, it means the last relocate
  1073. * failed
  1074. */
  1075. if (ret == 0)
  1076. break;
  1077. ret = btrfs_previous_item(chunk_root, path, 0,
  1078. BTRFS_CHUNK_ITEM_KEY);
  1079. if (ret) {
  1080. break;
  1081. }
  1082. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1083. path->slots[0]);
  1084. if (found_key.objectid != key.objectid)
  1085. break;
  1086. chunk = btrfs_item_ptr(path->nodes[0],
  1087. path->slots[0],
  1088. struct btrfs_chunk);
  1089. key.offset = found_key.offset;
  1090. /* chunk zero is special */
  1091. if (key.offset == 0)
  1092. break;
  1093. ret = btrfs_relocate_chunk(chunk_root,
  1094. chunk_root->root_key.objectid,
  1095. found_key.objectid,
  1096. found_key.offset);
  1097. BUG_ON(ret);
  1098. btrfs_release_path(chunk_root, path);
  1099. }
  1100. ret = 0;
  1101. error:
  1102. btrfs_free_path(path);
  1103. mutex_unlock(&dev_root->fs_info->fs_mutex);
  1104. return ret;
  1105. }
  1106. /*
  1107. * shrinking a device means finding all of the device extents past
  1108. * the new size, and then following the back refs to the chunks.
  1109. * The chunk relocation code actually frees the device extent
  1110. */
  1111. int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
  1112. {
  1113. struct btrfs_trans_handle *trans;
  1114. struct btrfs_root *root = device->dev_root;
  1115. struct btrfs_dev_extent *dev_extent = NULL;
  1116. struct btrfs_path *path;
  1117. u64 length;
  1118. u64 chunk_tree;
  1119. u64 chunk_objectid;
  1120. u64 chunk_offset;
  1121. int ret;
  1122. int slot;
  1123. struct extent_buffer *l;
  1124. struct btrfs_key key;
  1125. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  1126. u64 old_total = btrfs_super_total_bytes(super_copy);
  1127. u64 diff = device->total_bytes - new_size;
  1128. path = btrfs_alloc_path();
  1129. if (!path)
  1130. return -ENOMEM;
  1131. trans = btrfs_start_transaction(root, 1);
  1132. if (!trans) {
  1133. ret = -ENOMEM;
  1134. goto done;
  1135. }
  1136. path->reada = 2;
  1137. device->total_bytes = new_size;
  1138. ret = btrfs_update_device(trans, device);
  1139. if (ret) {
  1140. btrfs_end_transaction(trans, root);
  1141. goto done;
  1142. }
  1143. WARN_ON(diff > old_total);
  1144. btrfs_set_super_total_bytes(super_copy, old_total - diff);
  1145. btrfs_end_transaction(trans, root);
  1146. key.objectid = device->devid;
  1147. key.offset = (u64)-1;
  1148. key.type = BTRFS_DEV_EXTENT_KEY;
  1149. while (1) {
  1150. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1151. if (ret < 0)
  1152. goto done;
  1153. ret = btrfs_previous_item(root, path, 0, key.type);
  1154. if (ret < 0)
  1155. goto done;
  1156. if (ret) {
  1157. ret = 0;
  1158. goto done;
  1159. }
  1160. l = path->nodes[0];
  1161. slot = path->slots[0];
  1162. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  1163. if (key.objectid != device->devid)
  1164. goto done;
  1165. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  1166. length = btrfs_dev_extent_length(l, dev_extent);
  1167. if (key.offset + length <= new_size)
  1168. goto done;
  1169. chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
  1170. chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
  1171. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  1172. btrfs_release_path(root, path);
  1173. ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
  1174. chunk_offset);
  1175. if (ret)
  1176. goto done;
  1177. }
  1178. done:
  1179. btrfs_free_path(path);
  1180. return ret;
  1181. }
  1182. int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
  1183. struct btrfs_root *root,
  1184. struct btrfs_key *key,
  1185. struct btrfs_chunk *chunk, int item_size)
  1186. {
  1187. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  1188. struct btrfs_disk_key disk_key;
  1189. u32 array_size;
  1190. u8 *ptr;
  1191. array_size = btrfs_super_sys_array_size(super_copy);
  1192. if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
  1193. return -EFBIG;
  1194. ptr = super_copy->sys_chunk_array + array_size;
  1195. btrfs_cpu_key_to_disk(&disk_key, key);
  1196. memcpy(ptr, &disk_key, sizeof(disk_key));
  1197. ptr += sizeof(disk_key);
  1198. memcpy(ptr, chunk, item_size);
  1199. item_size += sizeof(disk_key);
  1200. btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
  1201. return 0;
  1202. }
  1203. static u64 chunk_bytes_by_type(u64 type, u64 calc_size, int num_stripes,
  1204. int sub_stripes)
  1205. {
  1206. if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
  1207. return calc_size;
  1208. else if (type & BTRFS_BLOCK_GROUP_RAID10)
  1209. return calc_size * (num_stripes / sub_stripes);
  1210. else
  1211. return calc_size * num_stripes;
  1212. }
  1213. int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  1214. struct btrfs_root *extent_root, u64 *start,
  1215. u64 *num_bytes, u64 type)
  1216. {
  1217. u64 dev_offset;
  1218. struct btrfs_fs_info *info = extent_root->fs_info;
  1219. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  1220. struct btrfs_path *path;
  1221. struct btrfs_stripe *stripes;
  1222. struct btrfs_device *device = NULL;
  1223. struct btrfs_chunk *chunk;
  1224. struct list_head private_devs;
  1225. struct list_head *dev_list;
  1226. struct list_head *cur;
  1227. struct extent_map_tree *em_tree;
  1228. struct map_lookup *map;
  1229. struct extent_map *em;
  1230. int min_stripe_size = 1 * 1024 * 1024;
  1231. u64 physical;
  1232. u64 calc_size = 1024 * 1024 * 1024;
  1233. u64 max_chunk_size = calc_size;
  1234. u64 min_free;
  1235. u64 avail;
  1236. u64 max_avail = 0;
  1237. u64 percent_max;
  1238. int num_stripes = 1;
  1239. int min_stripes = 1;
  1240. int sub_stripes = 0;
  1241. int looped = 0;
  1242. int ret;
  1243. int index;
  1244. int stripe_len = 64 * 1024;
  1245. struct btrfs_key key;
  1246. if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
  1247. (type & BTRFS_BLOCK_GROUP_DUP)) {
  1248. WARN_ON(1);
  1249. type &= ~BTRFS_BLOCK_GROUP_DUP;
  1250. }
  1251. dev_list = &extent_root->fs_info->fs_devices->alloc_list;
  1252. if (list_empty(dev_list))
  1253. return -ENOSPC;
  1254. if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
  1255. num_stripes = btrfs_super_num_devices(&info->super_copy);
  1256. min_stripes = 2;
  1257. }
  1258. if (type & (BTRFS_BLOCK_GROUP_DUP)) {
  1259. num_stripes = 2;
  1260. min_stripes = 2;
  1261. }
  1262. if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
  1263. num_stripes = min_t(u64, 2,
  1264. btrfs_super_num_devices(&info->super_copy));
  1265. if (num_stripes < 2)
  1266. return -ENOSPC;
  1267. min_stripes = 2;
  1268. }
  1269. if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
  1270. num_stripes = btrfs_super_num_devices(&info->super_copy);
  1271. if (num_stripes < 4)
  1272. return -ENOSPC;
  1273. num_stripes &= ~(u32)1;
  1274. sub_stripes = 2;
  1275. min_stripes = 4;
  1276. }
  1277. if (type & BTRFS_BLOCK_GROUP_DATA) {
  1278. max_chunk_size = 10 * calc_size;
  1279. min_stripe_size = 64 * 1024 * 1024;
  1280. } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
  1281. max_chunk_size = 4 * calc_size;
  1282. min_stripe_size = 32 * 1024 * 1024;
  1283. } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1284. calc_size = 8 * 1024 * 1024;
  1285. max_chunk_size = calc_size * 2;
  1286. min_stripe_size = 1 * 1024 * 1024;
  1287. }
  1288. path = btrfs_alloc_path();
  1289. if (!path)
  1290. return -ENOMEM;
  1291. /* we don't want a chunk larger than 10% of the FS */
  1292. percent_max = div_factor(btrfs_super_total_bytes(&info->super_copy), 1);
  1293. max_chunk_size = min(percent_max, max_chunk_size);
  1294. again:
  1295. if (calc_size * num_stripes > max_chunk_size) {
  1296. calc_size = max_chunk_size;
  1297. do_div(calc_size, num_stripes);
  1298. do_div(calc_size, stripe_len);
  1299. calc_size *= stripe_len;
  1300. }
  1301. /* we don't want tiny stripes */
  1302. calc_size = max_t(u64, min_stripe_size, calc_size);
  1303. do_div(calc_size, stripe_len);
  1304. calc_size *= stripe_len;
  1305. INIT_LIST_HEAD(&private_devs);
  1306. cur = dev_list->next;
  1307. index = 0;
  1308. if (type & BTRFS_BLOCK_GROUP_DUP)
  1309. min_free = calc_size * 2;
  1310. else
  1311. min_free = calc_size;
  1312. /* we add 1MB because we never use the first 1MB of the device */
  1313. min_free += 1024 * 1024;
  1314. /* build a private list of devices we will allocate from */
  1315. while(index < num_stripes) {
  1316. device = list_entry(cur, struct btrfs_device, dev_alloc_list);
  1317. if (device->total_bytes > device->bytes_used)
  1318. avail = device->total_bytes - device->bytes_used;
  1319. else
  1320. avail = 0;
  1321. cur = cur->next;
  1322. if (device->in_fs_metadata && avail >= min_free) {
  1323. u64 ignored_start = 0;
  1324. ret = find_free_dev_extent(trans, device, path,
  1325. min_free,
  1326. &ignored_start);
  1327. if (ret == 0) {
  1328. list_move_tail(&device->dev_alloc_list,
  1329. &private_devs);
  1330. index++;
  1331. if (type & BTRFS_BLOCK_GROUP_DUP)
  1332. index++;
  1333. }
  1334. } else if (device->in_fs_metadata && avail > max_avail)
  1335. max_avail = avail;
  1336. if (cur == dev_list)
  1337. break;
  1338. }
  1339. if (index < num_stripes) {
  1340. list_splice(&private_devs, dev_list);
  1341. if (index >= min_stripes) {
  1342. num_stripes = index;
  1343. if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
  1344. num_stripes /= sub_stripes;
  1345. num_stripes *= sub_stripes;
  1346. }
  1347. looped = 1;
  1348. goto again;
  1349. }
  1350. if (!looped && max_avail > 0) {
  1351. looped = 1;
  1352. calc_size = max_avail;
  1353. goto again;
  1354. }
  1355. btrfs_free_path(path);
  1356. return -ENOSPC;
  1357. }
  1358. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  1359. key.type = BTRFS_CHUNK_ITEM_KEY;
  1360. ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  1361. &key.offset);
  1362. if (ret) {
  1363. btrfs_free_path(path);
  1364. return ret;
  1365. }
  1366. chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
  1367. if (!chunk) {
  1368. btrfs_free_path(path);
  1369. return -ENOMEM;
  1370. }
  1371. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  1372. if (!map) {
  1373. kfree(chunk);
  1374. btrfs_free_path(path);
  1375. return -ENOMEM;
  1376. }
  1377. btrfs_free_path(path);
  1378. path = NULL;
  1379. stripes = &chunk->stripe;
  1380. *num_bytes = chunk_bytes_by_type(type, calc_size,
  1381. num_stripes, sub_stripes);
  1382. index = 0;
  1383. while(index < num_stripes) {
  1384. struct btrfs_stripe *stripe;
  1385. BUG_ON(list_empty(&private_devs));
  1386. cur = private_devs.next;
  1387. device = list_entry(cur, struct btrfs_device, dev_alloc_list);
  1388. /* loop over this device again if we're doing a dup group */
  1389. if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
  1390. (index == num_stripes - 1))
  1391. list_move_tail(&device->dev_alloc_list, dev_list);
  1392. ret = btrfs_alloc_dev_extent(trans, device,
  1393. info->chunk_root->root_key.objectid,
  1394. BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
  1395. calc_size, &dev_offset);
  1396. BUG_ON(ret);
  1397. device->bytes_used += calc_size;
  1398. ret = btrfs_update_device(trans, device);
  1399. BUG_ON(ret);
  1400. map->stripes[index].dev = device;
  1401. map->stripes[index].physical = dev_offset;
  1402. stripe = stripes + index;
  1403. btrfs_set_stack_stripe_devid(stripe, device->devid);
  1404. btrfs_set_stack_stripe_offset(stripe, dev_offset);
  1405. memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
  1406. physical = dev_offset;
  1407. index++;
  1408. }
  1409. BUG_ON(!list_empty(&private_devs));
  1410. /* key was set above */
  1411. btrfs_set_stack_chunk_length(chunk, *num_bytes);
  1412. btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
  1413. btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
  1414. btrfs_set_stack_chunk_type(chunk, type);
  1415. btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
  1416. btrfs_set_stack_chunk_io_align(chunk, stripe_len);
  1417. btrfs_set_stack_chunk_io_width(chunk, stripe_len);
  1418. btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
  1419. btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
  1420. map->sector_size = extent_root->sectorsize;
  1421. map->stripe_len = stripe_len;
  1422. map->io_align = stripe_len;
  1423. map->io_width = stripe_len;
  1424. map->type = type;
  1425. map->num_stripes = num_stripes;
  1426. map->sub_stripes = sub_stripes;
  1427. ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
  1428. btrfs_chunk_item_size(num_stripes));
  1429. BUG_ON(ret);
  1430. *start = key.offset;;
  1431. em = alloc_extent_map(GFP_NOFS);
  1432. if (!em)
  1433. return -ENOMEM;
  1434. em->bdev = (struct block_device *)map;
  1435. em->start = key.offset;
  1436. em->len = *num_bytes;
  1437. em->block_start = 0;
  1438. if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1439. ret = btrfs_add_system_chunk(trans, chunk_root, &key,
  1440. chunk, btrfs_chunk_item_size(num_stripes));
  1441. BUG_ON(ret);
  1442. }
  1443. kfree(chunk);
  1444. em_tree = &extent_root->fs_info->mapping_tree.map_tree;
  1445. spin_lock(&em_tree->lock);
  1446. ret = add_extent_mapping(em_tree, em);
  1447. spin_unlock(&em_tree->lock);
  1448. BUG_ON(ret);
  1449. free_extent_map(em);
  1450. return ret;
  1451. }
  1452. void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
  1453. {
  1454. extent_map_tree_init(&tree->map_tree, GFP_NOFS);
  1455. }
  1456. void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
  1457. {
  1458. struct extent_map *em;
  1459. while(1) {
  1460. spin_lock(&tree->map_tree.lock);
  1461. em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
  1462. if (em)
  1463. remove_extent_mapping(&tree->map_tree, em);
  1464. spin_unlock(&tree->map_tree.lock);
  1465. if (!em)
  1466. break;
  1467. kfree(em->bdev);
  1468. /* once for us */
  1469. free_extent_map(em);
  1470. /* once for the tree */
  1471. free_extent_map(em);
  1472. }
  1473. }
  1474. int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
  1475. {
  1476. struct extent_map *em;
  1477. struct map_lookup *map;
  1478. struct extent_map_tree *em_tree = &map_tree->map_tree;
  1479. int ret;
  1480. spin_lock(&em_tree->lock);
  1481. em = lookup_extent_mapping(em_tree, logical, len);
  1482. spin_unlock(&em_tree->lock);
  1483. BUG_ON(!em);
  1484. BUG_ON(em->start > logical || em->start + em->len < logical);
  1485. map = (struct map_lookup *)em->bdev;
  1486. if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
  1487. ret = map->num_stripes;
  1488. else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  1489. ret = map->sub_stripes;
  1490. else
  1491. ret = 1;
  1492. free_extent_map(em);
  1493. return ret;
  1494. }
  1495. static int find_live_mirror(struct map_lookup *map, int first, int num,
  1496. int optimal)
  1497. {
  1498. int i;
  1499. if (map->stripes[optimal].dev->bdev)
  1500. return optimal;
  1501. for (i = first; i < first + num; i++) {
  1502. if (map->stripes[i].dev->bdev)
  1503. return i;
  1504. }
  1505. /* we couldn't find one that doesn't fail. Just return something
  1506. * and the io error handling code will clean up eventually
  1507. */
  1508. return optimal;
  1509. }
  1510. static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  1511. u64 logical, u64 *length,
  1512. struct btrfs_multi_bio **multi_ret,
  1513. int mirror_num, struct page *unplug_page)
  1514. {
  1515. struct extent_map *em;
  1516. struct map_lookup *map;
  1517. struct extent_map_tree *em_tree = &map_tree->map_tree;
  1518. u64 offset;
  1519. u64 stripe_offset;
  1520. u64 stripe_nr;
  1521. int stripes_allocated = 8;
  1522. int stripes_required = 1;
  1523. int stripe_index;
  1524. int i;
  1525. int num_stripes;
  1526. int max_errors = 0;
  1527. struct btrfs_multi_bio *multi = NULL;
  1528. if (multi_ret && !(rw & (1 << BIO_RW))) {
  1529. stripes_allocated = 1;
  1530. }
  1531. again:
  1532. if (multi_ret) {
  1533. multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
  1534. GFP_NOFS);
  1535. if (!multi)
  1536. return -ENOMEM;
  1537. atomic_set(&multi->error, 0);
  1538. }
  1539. spin_lock(&em_tree->lock);
  1540. em = lookup_extent_mapping(em_tree, logical, *length);
  1541. spin_unlock(&em_tree->lock);
  1542. if (!em && unplug_page)
  1543. return 0;
  1544. if (!em) {
  1545. printk("unable to find logical %Lu len %Lu\n", logical, *length);
  1546. BUG();
  1547. }
  1548. BUG_ON(em->start > logical || em->start + em->len < logical);
  1549. map = (struct map_lookup *)em->bdev;
  1550. offset = logical - em->start;
  1551. if (mirror_num > map->num_stripes)
  1552. mirror_num = 0;
  1553. /* if our multi bio struct is too small, back off and try again */
  1554. if (rw & (1 << BIO_RW)) {
  1555. if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
  1556. BTRFS_BLOCK_GROUP_DUP)) {
  1557. stripes_required = map->num_stripes;
  1558. max_errors = 1;
  1559. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  1560. stripes_required = map->sub_stripes;
  1561. max_errors = 1;
  1562. }
  1563. }
  1564. if (multi_ret && rw == WRITE &&
  1565. stripes_allocated < stripes_required) {
  1566. stripes_allocated = map->num_stripes;
  1567. free_extent_map(em);
  1568. kfree(multi);
  1569. goto again;
  1570. }
  1571. stripe_nr = offset;
  1572. /*
  1573. * stripe_nr counts the total number of stripes we have to stride
  1574. * to get to this block
  1575. */
  1576. do_div(stripe_nr, map->stripe_len);
  1577. stripe_offset = stripe_nr * map->stripe_len;
  1578. BUG_ON(offset < stripe_offset);
  1579. /* stripe_offset is the offset of this block in its stripe*/
  1580. stripe_offset = offset - stripe_offset;
  1581. if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
  1582. BTRFS_BLOCK_GROUP_RAID10 |
  1583. BTRFS_BLOCK_GROUP_DUP)) {
  1584. /* we limit the length of each bio to what fits in a stripe */
  1585. *length = min_t(u64, em->len - offset,
  1586. map->stripe_len - stripe_offset);
  1587. } else {
  1588. *length = em->len - offset;
  1589. }
  1590. if (!multi_ret && !unplug_page)
  1591. goto out;
  1592. num_stripes = 1;
  1593. stripe_index = 0;
  1594. if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  1595. if (unplug_page || (rw & (1 << BIO_RW)))
  1596. num_stripes = map->num_stripes;
  1597. else if (mirror_num)
  1598. stripe_index = mirror_num - 1;
  1599. else {
  1600. stripe_index = find_live_mirror(map, 0,
  1601. map->num_stripes,
  1602. current->pid % map->num_stripes);
  1603. }
  1604. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  1605. if (rw & (1 << BIO_RW))
  1606. num_stripes = map->num_stripes;
  1607. else if (mirror_num)
  1608. stripe_index = mirror_num - 1;
  1609. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  1610. int factor = map->num_stripes / map->sub_stripes;
  1611. stripe_index = do_div(stripe_nr, factor);
  1612. stripe_index *= map->sub_stripes;
  1613. if (unplug_page || (rw & (1 << BIO_RW)))
  1614. num_stripes = map->sub_stripes;
  1615. else if (mirror_num)
  1616. stripe_index += mirror_num - 1;
  1617. else {
  1618. stripe_index = find_live_mirror(map, stripe_index,
  1619. map->sub_stripes, stripe_index +
  1620. current->pid % map->sub_stripes);
  1621. }
  1622. } else {
  1623. /*
  1624. * after this do_div call, stripe_nr is the number of stripes
  1625. * on this device we have to walk to find the data, and
  1626. * stripe_index is the number of our device in the stripe array
  1627. */
  1628. stripe_index = do_div(stripe_nr, map->num_stripes);
  1629. }
  1630. BUG_ON(stripe_index >= map->num_stripes);
  1631. for (i = 0; i < num_stripes; i++) {
  1632. if (unplug_page) {
  1633. struct btrfs_device *device;
  1634. struct backing_dev_info *bdi;
  1635. device = map->stripes[stripe_index].dev;
  1636. if (device->bdev) {
  1637. bdi = blk_get_backing_dev_info(device->bdev);
  1638. if (bdi->unplug_io_fn) {
  1639. bdi->unplug_io_fn(bdi, unplug_page);
  1640. }
  1641. }
  1642. } else {
  1643. multi->stripes[i].physical =
  1644. map->stripes[stripe_index].physical +
  1645. stripe_offset + stripe_nr * map->stripe_len;
  1646. multi->stripes[i].dev = map->stripes[stripe_index].dev;
  1647. }
  1648. stripe_index++;
  1649. }
  1650. if (multi_ret) {
  1651. *multi_ret = multi;
  1652. multi->num_stripes = num_stripes;
  1653. multi->max_errors = max_errors;
  1654. }
  1655. out:
  1656. free_extent_map(em);
  1657. return 0;
  1658. }
  1659. int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  1660. u64 logical, u64 *length,
  1661. struct btrfs_multi_bio **multi_ret, int mirror_num)
  1662. {
  1663. return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
  1664. mirror_num, NULL);
  1665. }
  1666. int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
  1667. u64 logical, struct page *page)
  1668. {
  1669. u64 length = PAGE_CACHE_SIZE;
  1670. return __btrfs_map_block(map_tree, READ, logical, &length,
  1671. NULL, 0, page);
  1672. }
  1673. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
  1674. static void end_bio_multi_stripe(struct bio *bio, int err)
  1675. #else
  1676. static int end_bio_multi_stripe(struct bio *bio,
  1677. unsigned int bytes_done, int err)
  1678. #endif
  1679. {
  1680. struct btrfs_multi_bio *multi = bio->bi_private;
  1681. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  1682. if (bio->bi_size)
  1683. return 1;
  1684. #endif
  1685. if (err)
  1686. atomic_inc(&multi->error);
  1687. if (atomic_dec_and_test(&multi->stripes_pending)) {
  1688. bio->bi_private = multi->private;
  1689. bio->bi_end_io = multi->end_io;
  1690. /* only send an error to the higher layers if it is
  1691. * beyond the tolerance of the multi-bio
  1692. */
  1693. if (atomic_read(&multi->error) > multi->max_errors) {
  1694. err = -EIO;
  1695. } else if (err) {
  1696. /*
  1697. * this bio is actually up to date, we didn't
  1698. * go over the max number of errors
  1699. */
  1700. set_bit(BIO_UPTODATE, &bio->bi_flags);
  1701. err = 0;
  1702. }
  1703. kfree(multi);
  1704. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  1705. bio_endio(bio, bio->bi_size, err);
  1706. #else
  1707. bio_endio(bio, err);
  1708. #endif
  1709. } else {
  1710. bio_put(bio);
  1711. }
  1712. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  1713. return 0;
  1714. #endif
  1715. }
  1716. int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
  1717. int mirror_num)
  1718. {
  1719. struct btrfs_mapping_tree *map_tree;
  1720. struct btrfs_device *dev;
  1721. struct bio *first_bio = bio;
  1722. u64 logical = bio->bi_sector << 9;
  1723. u64 length = 0;
  1724. u64 map_length;
  1725. struct btrfs_multi_bio *multi = NULL;
  1726. int ret;
  1727. int dev_nr = 0;
  1728. int total_devs = 1;
  1729. length = bio->bi_size;
  1730. map_tree = &root->fs_info->mapping_tree;
  1731. map_length = length;
  1732. ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
  1733. mirror_num);
  1734. BUG_ON(ret);
  1735. total_devs = multi->num_stripes;
  1736. if (map_length < length) {
  1737. printk("mapping failed logical %Lu bio len %Lu "
  1738. "len %Lu\n", logical, length, map_length);
  1739. BUG();
  1740. }
  1741. multi->end_io = first_bio->bi_end_io;
  1742. multi->private = first_bio->bi_private;
  1743. atomic_set(&multi->stripes_pending, multi->num_stripes);
  1744. while(dev_nr < total_devs) {
  1745. if (total_devs > 1) {
  1746. if (dev_nr < total_devs - 1) {
  1747. bio = bio_clone(first_bio, GFP_NOFS);
  1748. BUG_ON(!bio);
  1749. } else {
  1750. bio = first_bio;
  1751. }
  1752. bio->bi_private = multi;
  1753. bio->bi_end_io = end_bio_multi_stripe;
  1754. }
  1755. bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
  1756. dev = multi->stripes[dev_nr].dev;
  1757. if (dev && dev->bdev) {
  1758. bio->bi_bdev = dev->bdev;
  1759. spin_lock(&dev->io_lock);
  1760. dev->total_ios++;
  1761. spin_unlock(&dev->io_lock);
  1762. submit_bio(rw, bio);
  1763. } else {
  1764. bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
  1765. bio->bi_sector = logical >> 9;
  1766. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  1767. bio_endio(bio, bio->bi_size, -EIO);
  1768. #else
  1769. bio_endio(bio, -EIO);
  1770. #endif
  1771. }
  1772. dev_nr++;
  1773. }
  1774. if (total_devs == 1)
  1775. kfree(multi);
  1776. return 0;
  1777. }
  1778. struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
  1779. u8 *uuid)
  1780. {
  1781. struct list_head *head = &root->fs_info->fs_devices->devices;
  1782. return __find_device(head, devid, uuid);
  1783. }
  1784. static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
  1785. u64 devid, u8 *dev_uuid)
  1786. {
  1787. struct btrfs_device *device;
  1788. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  1789. device = kzalloc(sizeof(*device), GFP_NOFS);
  1790. list_add(&device->dev_list,
  1791. &fs_devices->devices);
  1792. list_add(&device->dev_alloc_list,
  1793. &fs_devices->alloc_list);
  1794. device->barriers = 1;
  1795. device->dev_root = root->fs_info->dev_root;
  1796. device->devid = devid;
  1797. fs_devices->num_devices++;
  1798. spin_lock_init(&device->io_lock);
  1799. memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
  1800. return device;
  1801. }
  1802. static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
  1803. struct extent_buffer *leaf,
  1804. struct btrfs_chunk *chunk)
  1805. {
  1806. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  1807. struct map_lookup *map;
  1808. struct extent_map *em;
  1809. u64 logical;
  1810. u64 length;
  1811. u64 devid;
  1812. u8 uuid[BTRFS_UUID_SIZE];
  1813. int num_stripes;
  1814. int ret;
  1815. int i;
  1816. logical = key->offset;
  1817. length = btrfs_chunk_length(leaf, chunk);
  1818. spin_lock(&map_tree->map_tree.lock);
  1819. em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
  1820. spin_unlock(&map_tree->map_tree.lock);
  1821. /* already mapped? */
  1822. if (em && em->start <= logical && em->start + em->len > logical) {
  1823. free_extent_map(em);
  1824. return 0;
  1825. } else if (em) {
  1826. free_extent_map(em);
  1827. }
  1828. map = kzalloc(sizeof(*map), GFP_NOFS);
  1829. if (!map)
  1830. return -ENOMEM;
  1831. em = alloc_extent_map(GFP_NOFS);
  1832. if (!em)
  1833. return -ENOMEM;
  1834. num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  1835. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  1836. if (!map) {
  1837. free_extent_map(em);
  1838. return -ENOMEM;
  1839. }
  1840. em->bdev = (struct block_device *)map;
  1841. em->start = logical;
  1842. em->len = length;
  1843. em->block_start = 0;
  1844. map->num_stripes = num_stripes;
  1845. map->io_width = btrfs_chunk_io_width(leaf, chunk);
  1846. map->io_align = btrfs_chunk_io_align(leaf, chunk);
  1847. map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
  1848. map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
  1849. map->type = btrfs_chunk_type(leaf, chunk);
  1850. map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
  1851. for (i = 0; i < num_stripes; i++) {
  1852. map->stripes[i].physical =
  1853. btrfs_stripe_offset_nr(leaf, chunk, i);
  1854. devid = btrfs_stripe_devid_nr(leaf, chunk, i);
  1855. read_extent_buffer(leaf, uuid, (unsigned long)
  1856. btrfs_stripe_dev_uuid_nr(chunk, i),
  1857. BTRFS_UUID_SIZE);
  1858. map->stripes[i].dev = btrfs_find_device(root, devid, uuid);
  1859. if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
  1860. kfree(map);
  1861. free_extent_map(em);
  1862. return -EIO;
  1863. }
  1864. if (!map->stripes[i].dev) {
  1865. map->stripes[i].dev =
  1866. add_missing_dev(root, devid, uuid);
  1867. if (!map->stripes[i].dev) {
  1868. kfree(map);
  1869. free_extent_map(em);
  1870. return -EIO;
  1871. }
  1872. }
  1873. map->stripes[i].dev->in_fs_metadata = 1;
  1874. }
  1875. spin_lock(&map_tree->map_tree.lock);
  1876. ret = add_extent_mapping(&map_tree->map_tree, em);
  1877. spin_unlock(&map_tree->map_tree.lock);
  1878. BUG_ON(ret);
  1879. free_extent_map(em);
  1880. return 0;
  1881. }
  1882. static int fill_device_from_item(struct extent_buffer *leaf,
  1883. struct btrfs_dev_item *dev_item,
  1884. struct btrfs_device *device)
  1885. {
  1886. unsigned long ptr;
  1887. device->devid = btrfs_device_id(leaf, dev_item);
  1888. device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
  1889. device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
  1890. device->type = btrfs_device_type(leaf, dev_item);
  1891. device->io_align = btrfs_device_io_align(leaf, dev_item);
  1892. device->io_width = btrfs_device_io_width(leaf, dev_item);
  1893. device->sector_size = btrfs_device_sector_size(leaf, dev_item);
  1894. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  1895. read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  1896. return 0;
  1897. }
  1898. static int read_one_dev(struct btrfs_root *root,
  1899. struct extent_buffer *leaf,
  1900. struct btrfs_dev_item *dev_item)
  1901. {
  1902. struct btrfs_device *device;
  1903. u64 devid;
  1904. int ret;
  1905. u8 dev_uuid[BTRFS_UUID_SIZE];
  1906. devid = btrfs_device_id(leaf, dev_item);
  1907. read_extent_buffer(leaf, dev_uuid,
  1908. (unsigned long)btrfs_device_uuid(dev_item),
  1909. BTRFS_UUID_SIZE);
  1910. device = btrfs_find_device(root, devid, dev_uuid);
  1911. if (!device) {
  1912. printk("warning devid %Lu missing\n", devid);
  1913. device = add_missing_dev(root, devid, dev_uuid);
  1914. if (!device)
  1915. return -ENOMEM;
  1916. }
  1917. fill_device_from_item(leaf, dev_item, device);
  1918. device->dev_root = root->fs_info->dev_root;
  1919. device->in_fs_metadata = 1;
  1920. ret = 0;
  1921. #if 0
  1922. ret = btrfs_open_device(device);
  1923. if (ret) {
  1924. kfree(device);
  1925. }
  1926. #endif
  1927. return ret;
  1928. }
  1929. int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
  1930. {
  1931. struct btrfs_dev_item *dev_item;
  1932. dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
  1933. dev_item);
  1934. return read_one_dev(root, buf, dev_item);
  1935. }
  1936. int btrfs_read_sys_array(struct btrfs_root *root)
  1937. {
  1938. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  1939. struct extent_buffer *sb;
  1940. struct btrfs_disk_key *disk_key;
  1941. struct btrfs_chunk *chunk;
  1942. u8 *ptr;
  1943. unsigned long sb_ptr;
  1944. int ret = 0;
  1945. u32 num_stripes;
  1946. u32 array_size;
  1947. u32 len = 0;
  1948. u32 cur;
  1949. struct btrfs_key key;
  1950. sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
  1951. BTRFS_SUPER_INFO_SIZE);
  1952. if (!sb)
  1953. return -ENOMEM;
  1954. btrfs_set_buffer_uptodate(sb);
  1955. write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
  1956. array_size = btrfs_super_sys_array_size(super_copy);
  1957. ptr = super_copy->sys_chunk_array;
  1958. sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
  1959. cur = 0;
  1960. while (cur < array_size) {
  1961. disk_key = (struct btrfs_disk_key *)ptr;
  1962. btrfs_disk_key_to_cpu(&key, disk_key);
  1963. len = sizeof(*disk_key); ptr += len;
  1964. sb_ptr += len;
  1965. cur += len;
  1966. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  1967. chunk = (struct btrfs_chunk *)sb_ptr;
  1968. ret = read_one_chunk(root, &key, sb, chunk);
  1969. if (ret)
  1970. break;
  1971. num_stripes = btrfs_chunk_num_stripes(sb, chunk);
  1972. len = btrfs_chunk_item_size(num_stripes);
  1973. } else {
  1974. ret = -EIO;
  1975. break;
  1976. }
  1977. ptr += len;
  1978. sb_ptr += len;
  1979. cur += len;
  1980. }
  1981. free_extent_buffer(sb);
  1982. return ret;
  1983. }
  1984. int btrfs_read_chunk_tree(struct btrfs_root *root)
  1985. {
  1986. struct btrfs_path *path;
  1987. struct extent_buffer *leaf;
  1988. struct btrfs_key key;
  1989. struct btrfs_key found_key;
  1990. int ret;
  1991. int slot;
  1992. root = root->fs_info->chunk_root;
  1993. path = btrfs_alloc_path();
  1994. if (!path)
  1995. return -ENOMEM;
  1996. /* first we search for all of the device items, and then we
  1997. * read in all of the chunk items. This way we can create chunk
  1998. * mappings that reference all of the devices that are afound
  1999. */
  2000. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  2001. key.offset = 0;
  2002. key.type = 0;
  2003. again:
  2004. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2005. while(1) {
  2006. leaf = path->nodes[0];
  2007. slot = path->slots[0];
  2008. if (slot >= btrfs_header_nritems(leaf)) {
  2009. ret = btrfs_next_leaf(root, path);
  2010. if (ret == 0)
  2011. continue;
  2012. if (ret < 0)
  2013. goto error;
  2014. break;
  2015. }
  2016. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  2017. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  2018. if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
  2019. break;
  2020. if (found_key.type == BTRFS_DEV_ITEM_KEY) {
  2021. struct btrfs_dev_item *dev_item;
  2022. dev_item = btrfs_item_ptr(leaf, slot,
  2023. struct btrfs_dev_item);
  2024. ret = read_one_dev(root, leaf, dev_item);
  2025. BUG_ON(ret);
  2026. }
  2027. } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
  2028. struct btrfs_chunk *chunk;
  2029. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  2030. ret = read_one_chunk(root, &found_key, leaf, chunk);
  2031. }
  2032. path->slots[0]++;
  2033. }
  2034. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  2035. key.objectid = 0;
  2036. btrfs_release_path(root, path);
  2037. goto again;
  2038. }
  2039. btrfs_free_path(path);
  2040. ret = 0;
  2041. error:
  2042. return ret;
  2043. }