volumes.c 62 KB

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