volumes.c 62 KB

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