volumes.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206
  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 "compat.h"
  25. #include "ctree.h"
  26. #include "extent_map.h"
  27. #include "disk-io.h"
  28. #include "transaction.h"
  29. #include "print-tree.h"
  30. #include "volumes.h"
  31. #include "async-thread.h"
  32. struct map_lookup {
  33. u64 type;
  34. int io_align;
  35. int io_width;
  36. int stripe_len;
  37. int sector_size;
  38. int num_stripes;
  39. int sub_stripes;
  40. struct btrfs_bio_stripe stripes[];
  41. };
  42. static int init_first_rw_device(struct btrfs_trans_handle *trans,
  43. struct btrfs_root *root,
  44. struct btrfs_device *device);
  45. static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
  46. #define map_lookup_size(n) (sizeof(struct map_lookup) + \
  47. (sizeof(struct btrfs_bio_stripe) * (n)))
  48. static DEFINE_MUTEX(uuid_mutex);
  49. static LIST_HEAD(fs_uuids);
  50. void btrfs_lock_volumes(void)
  51. {
  52. mutex_lock(&uuid_mutex);
  53. }
  54. void btrfs_unlock_volumes(void)
  55. {
  56. mutex_unlock(&uuid_mutex);
  57. }
  58. static void lock_chunks(struct btrfs_root *root)
  59. {
  60. mutex_lock(&root->fs_info->chunk_mutex);
  61. }
  62. static void unlock_chunks(struct btrfs_root *root)
  63. {
  64. mutex_unlock(&root->fs_info->chunk_mutex);
  65. }
  66. static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
  67. {
  68. struct btrfs_device *device;
  69. WARN_ON(fs_devices->opened);
  70. while (!list_empty(&fs_devices->devices)) {
  71. device = list_entry(fs_devices->devices.next,
  72. struct btrfs_device, dev_list);
  73. list_del(&device->dev_list);
  74. kfree(device->name);
  75. kfree(device);
  76. }
  77. kfree(fs_devices);
  78. }
  79. int btrfs_cleanup_fs_uuids(void)
  80. {
  81. struct btrfs_fs_devices *fs_devices;
  82. while (!list_empty(&fs_uuids)) {
  83. fs_devices = list_entry(fs_uuids.next,
  84. struct btrfs_fs_devices, list);
  85. list_del(&fs_devices->list);
  86. free_fs_devices(fs_devices);
  87. }
  88. return 0;
  89. }
  90. static noinline struct btrfs_device *__find_device(struct list_head *head,
  91. u64 devid, u8 *uuid)
  92. {
  93. struct btrfs_device *dev;
  94. list_for_each_entry(dev, head, dev_list) {
  95. if (dev->devid == devid &&
  96. (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
  97. return dev;
  98. }
  99. }
  100. return NULL;
  101. }
  102. static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
  103. {
  104. struct btrfs_fs_devices *fs_devices;
  105. list_for_each_entry(fs_devices, &fs_uuids, list) {
  106. if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
  107. return fs_devices;
  108. }
  109. return NULL;
  110. }
  111. /*
  112. * we try to collect pending bios for a device so we don't get a large
  113. * number of procs sending bios down to the same device. This greatly
  114. * improves the schedulers ability to collect and merge the bios.
  115. *
  116. * But, it also turns into a long list of bios to process and that is sure
  117. * to eventually make the worker thread block. The solution here is to
  118. * make some progress and then put this work struct back at the end of
  119. * the list if the block device is congested. This way, multiple devices
  120. * can make progress from a single worker thread.
  121. */
  122. static noinline int run_scheduled_bios(struct btrfs_device *device)
  123. {
  124. struct bio *pending;
  125. struct backing_dev_info *bdi;
  126. struct btrfs_fs_info *fs_info;
  127. struct bio *tail;
  128. struct bio *cur;
  129. int again = 0;
  130. unsigned long num_run = 0;
  131. unsigned long limit;
  132. bdi = device->bdev->bd_inode->i_mapping->backing_dev_info;
  133. fs_info = device->dev_root->fs_info;
  134. limit = btrfs_async_submit_limit(fs_info);
  135. limit = limit * 2 / 3;
  136. loop:
  137. spin_lock(&device->io_lock);
  138. loop_lock:
  139. /* take all the bios off the list at once and process them
  140. * later on (without the lock held). But, remember the
  141. * tail and other pointers so the bios can be properly reinserted
  142. * into the list if we hit congestion
  143. */
  144. pending = device->pending_bios;
  145. tail = device->pending_bio_tail;
  146. WARN_ON(pending && !tail);
  147. device->pending_bios = NULL;
  148. device->pending_bio_tail = NULL;
  149. /*
  150. * if pending was null this time around, no bios need processing
  151. * at all and we can stop. Otherwise it'll loop back up again
  152. * and do an additional check so no bios are missed.
  153. *
  154. * device->running_pending is used to synchronize with the
  155. * schedule_bio code.
  156. */
  157. if (pending) {
  158. again = 1;
  159. device->running_pending = 1;
  160. } else {
  161. again = 0;
  162. device->running_pending = 0;
  163. }
  164. spin_unlock(&device->io_lock);
  165. while (pending) {
  166. cur = pending;
  167. pending = pending->bi_next;
  168. cur->bi_next = NULL;
  169. atomic_dec(&fs_info->nr_async_bios);
  170. if (atomic_read(&fs_info->nr_async_bios) < limit &&
  171. waitqueue_active(&fs_info->async_submit_wait))
  172. wake_up(&fs_info->async_submit_wait);
  173. BUG_ON(atomic_read(&cur->bi_cnt) == 0);
  174. bio_get(cur);
  175. submit_bio(cur->bi_rw, cur);
  176. bio_put(cur);
  177. num_run++;
  178. /*
  179. * we made progress, there is more work to do and the bdi
  180. * is now congested. Back off and let other work structs
  181. * run instead
  182. */
  183. if (pending && bdi_write_congested(bdi) && num_run > 16 &&
  184. fs_info->fs_devices->open_devices > 1) {
  185. struct bio *old_head;
  186. spin_lock(&device->io_lock);
  187. old_head = device->pending_bios;
  188. device->pending_bios = pending;
  189. if (device->pending_bio_tail)
  190. tail->bi_next = old_head;
  191. else
  192. device->pending_bio_tail = tail;
  193. device->running_pending = 1;
  194. spin_unlock(&device->io_lock);
  195. btrfs_requeue_work(&device->work);
  196. goto done;
  197. }
  198. }
  199. if (again)
  200. goto loop;
  201. spin_lock(&device->io_lock);
  202. if (device->pending_bios)
  203. goto loop_lock;
  204. spin_unlock(&device->io_lock);
  205. done:
  206. return 0;
  207. }
  208. static void pending_bios_fn(struct btrfs_work *work)
  209. {
  210. struct btrfs_device *device;
  211. device = container_of(work, struct btrfs_device, work);
  212. run_scheduled_bios(device);
  213. }
  214. static noinline int device_list_add(const char *path,
  215. struct btrfs_super_block *disk_super,
  216. u64 devid, struct btrfs_fs_devices **fs_devices_ret)
  217. {
  218. struct btrfs_device *device;
  219. struct btrfs_fs_devices *fs_devices;
  220. u64 found_transid = btrfs_super_generation(disk_super);
  221. fs_devices = find_fsid(disk_super->fsid);
  222. if (!fs_devices) {
  223. fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
  224. if (!fs_devices)
  225. return -ENOMEM;
  226. INIT_LIST_HEAD(&fs_devices->devices);
  227. INIT_LIST_HEAD(&fs_devices->alloc_list);
  228. list_add(&fs_devices->list, &fs_uuids);
  229. memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
  230. fs_devices->latest_devid = devid;
  231. fs_devices->latest_trans = found_transid;
  232. device = NULL;
  233. } else {
  234. device = __find_device(&fs_devices->devices, devid,
  235. disk_super->dev_item.uuid);
  236. }
  237. if (!device) {
  238. if (fs_devices->opened)
  239. return -EBUSY;
  240. device = kzalloc(sizeof(*device), GFP_NOFS);
  241. if (!device) {
  242. /* we can safely leave the fs_devices entry around */
  243. return -ENOMEM;
  244. }
  245. device->devid = devid;
  246. device->work.func = pending_bios_fn;
  247. memcpy(device->uuid, disk_super->dev_item.uuid,
  248. BTRFS_UUID_SIZE);
  249. device->barriers = 1;
  250. spin_lock_init(&device->io_lock);
  251. device->name = kstrdup(path, GFP_NOFS);
  252. if (!device->name) {
  253. kfree(device);
  254. return -ENOMEM;
  255. }
  256. INIT_LIST_HEAD(&device->dev_alloc_list);
  257. list_add(&device->dev_list, &fs_devices->devices);
  258. device->fs_devices = fs_devices;
  259. fs_devices->num_devices++;
  260. }
  261. if (found_transid > fs_devices->latest_trans) {
  262. fs_devices->latest_devid = devid;
  263. fs_devices->latest_trans = found_transid;
  264. }
  265. *fs_devices_ret = fs_devices;
  266. return 0;
  267. }
  268. static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
  269. {
  270. struct btrfs_fs_devices *fs_devices;
  271. struct btrfs_device *device;
  272. struct btrfs_device *orig_dev;
  273. fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
  274. if (!fs_devices)
  275. return ERR_PTR(-ENOMEM);
  276. INIT_LIST_HEAD(&fs_devices->devices);
  277. INIT_LIST_HEAD(&fs_devices->alloc_list);
  278. INIT_LIST_HEAD(&fs_devices->list);
  279. fs_devices->latest_devid = orig->latest_devid;
  280. fs_devices->latest_trans = orig->latest_trans;
  281. memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
  282. list_for_each_entry(orig_dev, &orig->devices, dev_list) {
  283. device = kzalloc(sizeof(*device), GFP_NOFS);
  284. if (!device)
  285. goto error;
  286. device->name = kstrdup(orig_dev->name, GFP_NOFS);
  287. if (!device->name)
  288. goto error;
  289. device->devid = orig_dev->devid;
  290. device->work.func = pending_bios_fn;
  291. memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
  292. device->barriers = 1;
  293. spin_lock_init(&device->io_lock);
  294. INIT_LIST_HEAD(&device->dev_list);
  295. INIT_LIST_HEAD(&device->dev_alloc_list);
  296. list_add(&device->dev_list, &fs_devices->devices);
  297. device->fs_devices = fs_devices;
  298. fs_devices->num_devices++;
  299. }
  300. return fs_devices;
  301. error:
  302. free_fs_devices(fs_devices);
  303. return ERR_PTR(-ENOMEM);
  304. }
  305. int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
  306. {
  307. struct btrfs_device *device, *next;
  308. mutex_lock(&uuid_mutex);
  309. again:
  310. list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
  311. if (device->in_fs_metadata)
  312. continue;
  313. if (device->bdev) {
  314. close_bdev_exclusive(device->bdev, device->mode);
  315. device->bdev = NULL;
  316. fs_devices->open_devices--;
  317. }
  318. if (device->writeable) {
  319. list_del_init(&device->dev_alloc_list);
  320. device->writeable = 0;
  321. fs_devices->rw_devices--;
  322. }
  323. list_del_init(&device->dev_list);
  324. fs_devices->num_devices--;
  325. kfree(device->name);
  326. kfree(device);
  327. }
  328. if (fs_devices->seed) {
  329. fs_devices = fs_devices->seed;
  330. goto again;
  331. }
  332. mutex_unlock(&uuid_mutex);
  333. return 0;
  334. }
  335. static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
  336. {
  337. struct btrfs_device *device;
  338. if (--fs_devices->opened > 0)
  339. return 0;
  340. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  341. if (device->bdev) {
  342. close_bdev_exclusive(device->bdev, device->mode);
  343. fs_devices->open_devices--;
  344. }
  345. if (device->writeable) {
  346. list_del_init(&device->dev_alloc_list);
  347. fs_devices->rw_devices--;
  348. }
  349. device->bdev = NULL;
  350. device->writeable = 0;
  351. device->in_fs_metadata = 0;
  352. }
  353. WARN_ON(fs_devices->open_devices);
  354. WARN_ON(fs_devices->rw_devices);
  355. fs_devices->opened = 0;
  356. fs_devices->seeding = 0;
  357. return 0;
  358. }
  359. int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
  360. {
  361. struct btrfs_fs_devices *seed_devices = NULL;
  362. int ret;
  363. mutex_lock(&uuid_mutex);
  364. ret = __btrfs_close_devices(fs_devices);
  365. if (!fs_devices->opened) {
  366. seed_devices = fs_devices->seed;
  367. fs_devices->seed = NULL;
  368. }
  369. mutex_unlock(&uuid_mutex);
  370. while (seed_devices) {
  371. fs_devices = seed_devices;
  372. seed_devices = fs_devices->seed;
  373. __btrfs_close_devices(fs_devices);
  374. free_fs_devices(fs_devices);
  375. }
  376. return ret;
  377. }
  378. static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
  379. fmode_t flags, void *holder)
  380. {
  381. struct block_device *bdev;
  382. struct list_head *head = &fs_devices->devices;
  383. struct btrfs_device *device;
  384. struct block_device *latest_bdev = NULL;
  385. struct buffer_head *bh;
  386. struct btrfs_super_block *disk_super;
  387. u64 latest_devid = 0;
  388. u64 latest_transid = 0;
  389. u64 devid;
  390. int seeding = 1;
  391. int ret = 0;
  392. list_for_each_entry(device, head, dev_list) {
  393. if (device->bdev)
  394. continue;
  395. if (!device->name)
  396. continue;
  397. bdev = open_bdev_exclusive(device->name, flags, holder);
  398. if (IS_ERR(bdev)) {
  399. printk(KERN_INFO "open %s failed\n", device->name);
  400. goto error;
  401. }
  402. set_blocksize(bdev, 4096);
  403. bh = btrfs_read_dev_super(bdev);
  404. if (!bh)
  405. goto error_close;
  406. disk_super = (struct btrfs_super_block *)bh->b_data;
  407. devid = le64_to_cpu(disk_super->dev_item.devid);
  408. if (devid != device->devid)
  409. goto error_brelse;
  410. if (memcmp(device->uuid, disk_super->dev_item.uuid,
  411. BTRFS_UUID_SIZE))
  412. goto error_brelse;
  413. device->generation = btrfs_super_generation(disk_super);
  414. if (!latest_transid || device->generation > latest_transid) {
  415. latest_devid = devid;
  416. latest_transid = device->generation;
  417. latest_bdev = bdev;
  418. }
  419. if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
  420. device->writeable = 0;
  421. } else {
  422. device->writeable = !bdev_read_only(bdev);
  423. seeding = 0;
  424. }
  425. device->bdev = bdev;
  426. device->in_fs_metadata = 0;
  427. device->mode = flags;
  428. fs_devices->open_devices++;
  429. if (device->writeable) {
  430. fs_devices->rw_devices++;
  431. list_add(&device->dev_alloc_list,
  432. &fs_devices->alloc_list);
  433. }
  434. continue;
  435. error_brelse:
  436. brelse(bh);
  437. error_close:
  438. close_bdev_exclusive(bdev, FMODE_READ);
  439. error:
  440. continue;
  441. }
  442. if (fs_devices->open_devices == 0) {
  443. ret = -EIO;
  444. goto out;
  445. }
  446. fs_devices->seeding = seeding;
  447. fs_devices->opened = 1;
  448. fs_devices->latest_bdev = latest_bdev;
  449. fs_devices->latest_devid = latest_devid;
  450. fs_devices->latest_trans = latest_transid;
  451. fs_devices->total_rw_bytes = 0;
  452. out:
  453. return ret;
  454. }
  455. int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
  456. fmode_t flags, void *holder)
  457. {
  458. int ret;
  459. mutex_lock(&uuid_mutex);
  460. if (fs_devices->opened) {
  461. fs_devices->opened++;
  462. ret = 0;
  463. } else {
  464. ret = __btrfs_open_devices(fs_devices, flags, holder);
  465. }
  466. mutex_unlock(&uuid_mutex);
  467. return ret;
  468. }
  469. int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
  470. struct btrfs_fs_devices **fs_devices_ret)
  471. {
  472. struct btrfs_super_block *disk_super;
  473. struct block_device *bdev;
  474. struct buffer_head *bh;
  475. int ret;
  476. u64 devid;
  477. u64 transid;
  478. mutex_lock(&uuid_mutex);
  479. bdev = open_bdev_exclusive(path, flags, holder);
  480. if (IS_ERR(bdev)) {
  481. ret = PTR_ERR(bdev);
  482. goto error;
  483. }
  484. ret = set_blocksize(bdev, 4096);
  485. if (ret)
  486. goto error_close;
  487. bh = btrfs_read_dev_super(bdev);
  488. if (!bh) {
  489. ret = -EIO;
  490. goto error_close;
  491. }
  492. disk_super = (struct btrfs_super_block *)bh->b_data;
  493. devid = le64_to_cpu(disk_super->dev_item.devid);
  494. transid = btrfs_super_generation(disk_super);
  495. if (disk_super->label[0])
  496. printk(KERN_INFO "device label %s ", disk_super->label);
  497. else {
  498. /* FIXME, make a readl uuid parser */
  499. printk(KERN_INFO "device fsid %llx-%llx ",
  500. *(unsigned long long *)disk_super->fsid,
  501. *(unsigned long long *)(disk_super->fsid + 8));
  502. }
  503. printk(KERN_CONT "devid %llu transid %llu %s\n",
  504. (unsigned long long)devid, (unsigned long long)transid, path);
  505. ret = device_list_add(path, disk_super, devid, fs_devices_ret);
  506. brelse(bh);
  507. error_close:
  508. close_bdev_exclusive(bdev, flags);
  509. error:
  510. mutex_unlock(&uuid_mutex);
  511. return ret;
  512. }
  513. /*
  514. * this uses a pretty simple search, the expectation is that it is
  515. * called very infrequently and that a given device has a small number
  516. * of extents
  517. */
  518. static noinline int find_free_dev_extent(struct btrfs_trans_handle *trans,
  519. struct btrfs_device *device,
  520. u64 num_bytes, u64 *start)
  521. {
  522. struct btrfs_key key;
  523. struct btrfs_root *root = device->dev_root;
  524. struct btrfs_dev_extent *dev_extent = NULL;
  525. struct btrfs_path *path;
  526. u64 hole_size = 0;
  527. u64 last_byte = 0;
  528. u64 search_start = 0;
  529. u64 search_end = device->total_bytes;
  530. int ret;
  531. int slot = 0;
  532. int start_found;
  533. struct extent_buffer *l;
  534. path = btrfs_alloc_path();
  535. if (!path)
  536. return -ENOMEM;
  537. path->reada = 2;
  538. start_found = 0;
  539. /* FIXME use last free of some kind */
  540. /* we don't want to overwrite the superblock on the drive,
  541. * so we make sure to start at an offset of at least 1MB
  542. */
  543. search_start = max((u64)1024 * 1024, search_start);
  544. if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
  545. search_start = max(root->fs_info->alloc_start, search_start);
  546. key.objectid = device->devid;
  547. key.offset = search_start;
  548. key.type = BTRFS_DEV_EXTENT_KEY;
  549. ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
  550. if (ret < 0)
  551. goto error;
  552. ret = btrfs_previous_item(root, path, 0, key.type);
  553. if (ret < 0)
  554. goto error;
  555. l = path->nodes[0];
  556. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  557. while (1) {
  558. l = path->nodes[0];
  559. slot = path->slots[0];
  560. if (slot >= btrfs_header_nritems(l)) {
  561. ret = btrfs_next_leaf(root, path);
  562. if (ret == 0)
  563. continue;
  564. if (ret < 0)
  565. goto error;
  566. no_more_items:
  567. if (!start_found) {
  568. if (search_start >= search_end) {
  569. ret = -ENOSPC;
  570. goto error;
  571. }
  572. *start = search_start;
  573. start_found = 1;
  574. goto check_pending;
  575. }
  576. *start = last_byte > search_start ?
  577. last_byte : search_start;
  578. if (search_end <= *start) {
  579. ret = -ENOSPC;
  580. goto error;
  581. }
  582. goto check_pending;
  583. }
  584. btrfs_item_key_to_cpu(l, &key, slot);
  585. if (key.objectid < device->devid)
  586. goto next;
  587. if (key.objectid > device->devid)
  588. goto no_more_items;
  589. if (key.offset >= search_start && key.offset > last_byte &&
  590. start_found) {
  591. if (last_byte < search_start)
  592. last_byte = search_start;
  593. hole_size = key.offset - last_byte;
  594. if (key.offset > last_byte &&
  595. hole_size >= num_bytes) {
  596. *start = last_byte;
  597. goto check_pending;
  598. }
  599. }
  600. if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
  601. goto next;
  602. start_found = 1;
  603. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  604. last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
  605. next:
  606. path->slots[0]++;
  607. cond_resched();
  608. }
  609. check_pending:
  610. /* we have to make sure we didn't find an extent that has already
  611. * been allocated by the map tree or the original allocation
  612. */
  613. BUG_ON(*start < search_start);
  614. if (*start + num_bytes > search_end) {
  615. ret = -ENOSPC;
  616. goto error;
  617. }
  618. /* check for pending inserts here */
  619. ret = 0;
  620. error:
  621. btrfs_free_path(path);
  622. return ret;
  623. }
  624. static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
  625. struct btrfs_device *device,
  626. u64 start)
  627. {
  628. int ret;
  629. struct btrfs_path *path;
  630. struct btrfs_root *root = device->dev_root;
  631. struct btrfs_key key;
  632. struct btrfs_key found_key;
  633. struct extent_buffer *leaf = NULL;
  634. struct btrfs_dev_extent *extent = NULL;
  635. path = btrfs_alloc_path();
  636. if (!path)
  637. return -ENOMEM;
  638. key.objectid = device->devid;
  639. key.offset = start;
  640. key.type = BTRFS_DEV_EXTENT_KEY;
  641. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  642. if (ret > 0) {
  643. ret = btrfs_previous_item(root, path, key.objectid,
  644. BTRFS_DEV_EXTENT_KEY);
  645. BUG_ON(ret);
  646. leaf = path->nodes[0];
  647. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  648. extent = btrfs_item_ptr(leaf, path->slots[0],
  649. struct btrfs_dev_extent);
  650. BUG_ON(found_key.offset > start || found_key.offset +
  651. btrfs_dev_extent_length(leaf, extent) < start);
  652. ret = 0;
  653. } else if (ret == 0) {
  654. leaf = path->nodes[0];
  655. extent = btrfs_item_ptr(leaf, path->slots[0],
  656. struct btrfs_dev_extent);
  657. }
  658. BUG_ON(ret);
  659. if (device->bytes_used > 0)
  660. device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
  661. ret = btrfs_del_item(trans, root, path);
  662. BUG_ON(ret);
  663. btrfs_free_path(path);
  664. return ret;
  665. }
  666. int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
  667. struct btrfs_device *device,
  668. u64 chunk_tree, u64 chunk_objectid,
  669. u64 chunk_offset, u64 start, u64 num_bytes)
  670. {
  671. int ret;
  672. struct btrfs_path *path;
  673. struct btrfs_root *root = device->dev_root;
  674. struct btrfs_dev_extent *extent;
  675. struct extent_buffer *leaf;
  676. struct btrfs_key key;
  677. WARN_ON(!device->in_fs_metadata);
  678. path = btrfs_alloc_path();
  679. if (!path)
  680. return -ENOMEM;
  681. key.objectid = device->devid;
  682. key.offset = start;
  683. key.type = BTRFS_DEV_EXTENT_KEY;
  684. ret = btrfs_insert_empty_item(trans, root, path, &key,
  685. sizeof(*extent));
  686. BUG_ON(ret);
  687. leaf = path->nodes[0];
  688. extent = btrfs_item_ptr(leaf, path->slots[0],
  689. struct btrfs_dev_extent);
  690. btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
  691. btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
  692. btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
  693. write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
  694. (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
  695. BTRFS_UUID_SIZE);
  696. btrfs_set_dev_extent_length(leaf, extent, num_bytes);
  697. btrfs_mark_buffer_dirty(leaf);
  698. btrfs_free_path(path);
  699. return ret;
  700. }
  701. static noinline int find_next_chunk(struct btrfs_root *root,
  702. u64 objectid, u64 *offset)
  703. {
  704. struct btrfs_path *path;
  705. int ret;
  706. struct btrfs_key key;
  707. struct btrfs_chunk *chunk;
  708. struct btrfs_key found_key;
  709. path = btrfs_alloc_path();
  710. BUG_ON(!path);
  711. key.objectid = objectid;
  712. key.offset = (u64)-1;
  713. key.type = BTRFS_CHUNK_ITEM_KEY;
  714. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  715. if (ret < 0)
  716. goto error;
  717. BUG_ON(ret == 0);
  718. ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
  719. if (ret) {
  720. *offset = 0;
  721. } else {
  722. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  723. path->slots[0]);
  724. if (found_key.objectid != objectid)
  725. *offset = 0;
  726. else {
  727. chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
  728. struct btrfs_chunk);
  729. *offset = found_key.offset +
  730. btrfs_chunk_length(path->nodes[0], chunk);
  731. }
  732. }
  733. ret = 0;
  734. error:
  735. btrfs_free_path(path);
  736. return ret;
  737. }
  738. static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
  739. {
  740. int ret;
  741. struct btrfs_key key;
  742. struct btrfs_key found_key;
  743. struct btrfs_path *path;
  744. root = root->fs_info->chunk_root;
  745. path = btrfs_alloc_path();
  746. if (!path)
  747. return -ENOMEM;
  748. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  749. key.type = BTRFS_DEV_ITEM_KEY;
  750. key.offset = (u64)-1;
  751. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  752. if (ret < 0)
  753. goto error;
  754. BUG_ON(ret == 0);
  755. ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
  756. BTRFS_DEV_ITEM_KEY);
  757. if (ret) {
  758. *objectid = 1;
  759. } else {
  760. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  761. path->slots[0]);
  762. *objectid = found_key.offset + 1;
  763. }
  764. ret = 0;
  765. error:
  766. btrfs_free_path(path);
  767. return ret;
  768. }
  769. /*
  770. * the device information is stored in the chunk root
  771. * the btrfs_device struct should be fully filled in
  772. */
  773. int btrfs_add_device(struct btrfs_trans_handle *trans,
  774. struct btrfs_root *root,
  775. struct btrfs_device *device)
  776. {
  777. int ret;
  778. struct btrfs_path *path;
  779. struct btrfs_dev_item *dev_item;
  780. struct extent_buffer *leaf;
  781. struct btrfs_key key;
  782. unsigned long ptr;
  783. root = root->fs_info->chunk_root;
  784. path = btrfs_alloc_path();
  785. if (!path)
  786. return -ENOMEM;
  787. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  788. key.type = BTRFS_DEV_ITEM_KEY;
  789. key.offset = device->devid;
  790. ret = btrfs_insert_empty_item(trans, root, path, &key,
  791. sizeof(*dev_item));
  792. if (ret)
  793. goto out;
  794. leaf = path->nodes[0];
  795. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  796. btrfs_set_device_id(leaf, dev_item, device->devid);
  797. btrfs_set_device_generation(leaf, dev_item, 0);
  798. btrfs_set_device_type(leaf, dev_item, device->type);
  799. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  800. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  801. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  802. btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
  803. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  804. btrfs_set_device_group(leaf, dev_item, 0);
  805. btrfs_set_device_seek_speed(leaf, dev_item, 0);
  806. btrfs_set_device_bandwidth(leaf, dev_item, 0);
  807. btrfs_set_device_start_offset(leaf, dev_item, 0);
  808. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  809. write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  810. ptr = (unsigned long)btrfs_device_fsid(dev_item);
  811. write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
  812. btrfs_mark_buffer_dirty(leaf);
  813. ret = 0;
  814. out:
  815. btrfs_free_path(path);
  816. return ret;
  817. }
  818. static int btrfs_rm_dev_item(struct btrfs_root *root,
  819. struct btrfs_device *device)
  820. {
  821. int ret;
  822. struct btrfs_path *path;
  823. struct btrfs_key key;
  824. struct btrfs_trans_handle *trans;
  825. root = root->fs_info->chunk_root;
  826. path = btrfs_alloc_path();
  827. if (!path)
  828. return -ENOMEM;
  829. trans = btrfs_start_transaction(root, 1);
  830. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  831. key.type = BTRFS_DEV_ITEM_KEY;
  832. key.offset = device->devid;
  833. lock_chunks(root);
  834. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  835. if (ret < 0)
  836. goto out;
  837. if (ret > 0) {
  838. ret = -ENOENT;
  839. goto out;
  840. }
  841. ret = btrfs_del_item(trans, root, path);
  842. if (ret)
  843. goto out;
  844. out:
  845. btrfs_free_path(path);
  846. unlock_chunks(root);
  847. btrfs_commit_transaction(trans, root);
  848. return ret;
  849. }
  850. int btrfs_rm_device(struct btrfs_root *root, char *device_path)
  851. {
  852. struct btrfs_device *device;
  853. struct btrfs_device *next_device;
  854. struct block_device *bdev;
  855. struct buffer_head *bh = NULL;
  856. struct btrfs_super_block *disk_super;
  857. u64 all_avail;
  858. u64 devid;
  859. u64 num_devices;
  860. u8 *dev_uuid;
  861. int ret = 0;
  862. mutex_lock(&uuid_mutex);
  863. mutex_lock(&root->fs_info->volume_mutex);
  864. all_avail = root->fs_info->avail_data_alloc_bits |
  865. root->fs_info->avail_system_alloc_bits |
  866. root->fs_info->avail_metadata_alloc_bits;
  867. if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
  868. root->fs_info->fs_devices->rw_devices <= 4) {
  869. printk(KERN_ERR "btrfs: unable to go below four devices "
  870. "on raid10\n");
  871. ret = -EINVAL;
  872. goto out;
  873. }
  874. if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
  875. root->fs_info->fs_devices->rw_devices <= 2) {
  876. printk(KERN_ERR "btrfs: unable to go below two "
  877. "devices on raid1\n");
  878. ret = -EINVAL;
  879. goto out;
  880. }
  881. if (strcmp(device_path, "missing") == 0) {
  882. struct list_head *devices;
  883. struct btrfs_device *tmp;
  884. device = NULL;
  885. devices = &root->fs_info->fs_devices->devices;
  886. list_for_each_entry(tmp, devices, dev_list) {
  887. if (tmp->in_fs_metadata && !tmp->bdev) {
  888. device = tmp;
  889. break;
  890. }
  891. }
  892. bdev = NULL;
  893. bh = NULL;
  894. disk_super = NULL;
  895. if (!device) {
  896. printk(KERN_ERR "btrfs: no missing devices found to "
  897. "remove\n");
  898. goto out;
  899. }
  900. } else {
  901. bdev = open_bdev_exclusive(device_path, FMODE_READ,
  902. root->fs_info->bdev_holder);
  903. if (IS_ERR(bdev)) {
  904. ret = PTR_ERR(bdev);
  905. goto out;
  906. }
  907. set_blocksize(bdev, 4096);
  908. bh = btrfs_read_dev_super(bdev);
  909. if (!bh) {
  910. ret = -EIO;
  911. goto error_close;
  912. }
  913. disk_super = (struct btrfs_super_block *)bh->b_data;
  914. devid = le64_to_cpu(disk_super->dev_item.devid);
  915. dev_uuid = disk_super->dev_item.uuid;
  916. device = btrfs_find_device(root, devid, dev_uuid,
  917. disk_super->fsid);
  918. if (!device) {
  919. ret = -ENOENT;
  920. goto error_brelse;
  921. }
  922. }
  923. if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
  924. printk(KERN_ERR "btrfs: unable to remove the only writeable "
  925. "device\n");
  926. ret = -EINVAL;
  927. goto error_brelse;
  928. }
  929. if (device->writeable) {
  930. list_del_init(&device->dev_alloc_list);
  931. root->fs_info->fs_devices->rw_devices--;
  932. }
  933. ret = btrfs_shrink_device(device, 0);
  934. if (ret)
  935. goto error_brelse;
  936. ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
  937. if (ret)
  938. goto error_brelse;
  939. device->in_fs_metadata = 0;
  940. list_del_init(&device->dev_list);
  941. device->fs_devices->num_devices--;
  942. next_device = list_entry(root->fs_info->fs_devices->devices.next,
  943. struct btrfs_device, dev_list);
  944. if (device->bdev == root->fs_info->sb->s_bdev)
  945. root->fs_info->sb->s_bdev = next_device->bdev;
  946. if (device->bdev == root->fs_info->fs_devices->latest_bdev)
  947. root->fs_info->fs_devices->latest_bdev = next_device->bdev;
  948. if (device->bdev) {
  949. close_bdev_exclusive(device->bdev, device->mode);
  950. device->bdev = NULL;
  951. device->fs_devices->open_devices--;
  952. }
  953. num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
  954. btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
  955. if (device->fs_devices->open_devices == 0) {
  956. struct btrfs_fs_devices *fs_devices;
  957. fs_devices = root->fs_info->fs_devices;
  958. while (fs_devices) {
  959. if (fs_devices->seed == device->fs_devices)
  960. break;
  961. fs_devices = fs_devices->seed;
  962. }
  963. fs_devices->seed = device->fs_devices->seed;
  964. device->fs_devices->seed = NULL;
  965. __btrfs_close_devices(device->fs_devices);
  966. free_fs_devices(device->fs_devices);
  967. }
  968. /*
  969. * at this point, the device is zero sized. We want to
  970. * remove it from the devices list and zero out the old super
  971. */
  972. if (device->writeable) {
  973. /* make sure this device isn't detected as part of
  974. * the FS anymore
  975. */
  976. memset(&disk_super->magic, 0, sizeof(disk_super->magic));
  977. set_buffer_dirty(bh);
  978. sync_dirty_buffer(bh);
  979. }
  980. kfree(device->name);
  981. kfree(device);
  982. ret = 0;
  983. error_brelse:
  984. brelse(bh);
  985. error_close:
  986. if (bdev)
  987. close_bdev_exclusive(bdev, FMODE_READ);
  988. out:
  989. mutex_unlock(&root->fs_info->volume_mutex);
  990. mutex_unlock(&uuid_mutex);
  991. return ret;
  992. }
  993. /*
  994. * does all the dirty work required for changing file system's UUID.
  995. */
  996. static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
  997. struct btrfs_root *root)
  998. {
  999. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  1000. struct btrfs_fs_devices *old_devices;
  1001. struct btrfs_fs_devices *seed_devices;
  1002. struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
  1003. struct btrfs_device *device;
  1004. u64 super_flags;
  1005. BUG_ON(!mutex_is_locked(&uuid_mutex));
  1006. if (!fs_devices->seeding)
  1007. return -EINVAL;
  1008. seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
  1009. if (!seed_devices)
  1010. return -ENOMEM;
  1011. old_devices = clone_fs_devices(fs_devices);
  1012. if (IS_ERR(old_devices)) {
  1013. kfree(seed_devices);
  1014. return PTR_ERR(old_devices);
  1015. }
  1016. list_add(&old_devices->list, &fs_uuids);
  1017. memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
  1018. seed_devices->opened = 1;
  1019. INIT_LIST_HEAD(&seed_devices->devices);
  1020. INIT_LIST_HEAD(&seed_devices->alloc_list);
  1021. list_splice_init(&fs_devices->devices, &seed_devices->devices);
  1022. list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
  1023. list_for_each_entry(device, &seed_devices->devices, dev_list) {
  1024. device->fs_devices = seed_devices;
  1025. }
  1026. fs_devices->seeding = 0;
  1027. fs_devices->num_devices = 0;
  1028. fs_devices->open_devices = 0;
  1029. fs_devices->seed = seed_devices;
  1030. generate_random_uuid(fs_devices->fsid);
  1031. memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
  1032. memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
  1033. super_flags = btrfs_super_flags(disk_super) &
  1034. ~BTRFS_SUPER_FLAG_SEEDING;
  1035. btrfs_set_super_flags(disk_super, super_flags);
  1036. return 0;
  1037. }
  1038. /*
  1039. * strore the expected generation for seed devices in device items.
  1040. */
  1041. static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
  1042. struct btrfs_root *root)
  1043. {
  1044. struct btrfs_path *path;
  1045. struct extent_buffer *leaf;
  1046. struct btrfs_dev_item *dev_item;
  1047. struct btrfs_device *device;
  1048. struct btrfs_key key;
  1049. u8 fs_uuid[BTRFS_UUID_SIZE];
  1050. u8 dev_uuid[BTRFS_UUID_SIZE];
  1051. u64 devid;
  1052. int ret;
  1053. path = btrfs_alloc_path();
  1054. if (!path)
  1055. return -ENOMEM;
  1056. root = root->fs_info->chunk_root;
  1057. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1058. key.offset = 0;
  1059. key.type = BTRFS_DEV_ITEM_KEY;
  1060. while (1) {
  1061. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1062. if (ret < 0)
  1063. goto error;
  1064. leaf = path->nodes[0];
  1065. next_slot:
  1066. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  1067. ret = btrfs_next_leaf(root, path);
  1068. if (ret > 0)
  1069. break;
  1070. if (ret < 0)
  1071. goto error;
  1072. leaf = path->nodes[0];
  1073. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1074. btrfs_release_path(root, path);
  1075. continue;
  1076. }
  1077. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1078. if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
  1079. key.type != BTRFS_DEV_ITEM_KEY)
  1080. break;
  1081. dev_item = btrfs_item_ptr(leaf, path->slots[0],
  1082. struct btrfs_dev_item);
  1083. devid = btrfs_device_id(leaf, dev_item);
  1084. read_extent_buffer(leaf, dev_uuid,
  1085. (unsigned long)btrfs_device_uuid(dev_item),
  1086. BTRFS_UUID_SIZE);
  1087. read_extent_buffer(leaf, fs_uuid,
  1088. (unsigned long)btrfs_device_fsid(dev_item),
  1089. BTRFS_UUID_SIZE);
  1090. device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
  1091. BUG_ON(!device);
  1092. if (device->fs_devices->seeding) {
  1093. btrfs_set_device_generation(leaf, dev_item,
  1094. device->generation);
  1095. btrfs_mark_buffer_dirty(leaf);
  1096. }
  1097. path->slots[0]++;
  1098. goto next_slot;
  1099. }
  1100. ret = 0;
  1101. error:
  1102. btrfs_free_path(path);
  1103. return ret;
  1104. }
  1105. int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
  1106. {
  1107. struct btrfs_trans_handle *trans;
  1108. struct btrfs_device *device;
  1109. struct block_device *bdev;
  1110. struct list_head *devices;
  1111. struct super_block *sb = root->fs_info->sb;
  1112. u64 total_bytes;
  1113. int seeding_dev = 0;
  1114. int ret = 0;
  1115. if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
  1116. return -EINVAL;
  1117. bdev = open_bdev_exclusive(device_path, 0, root->fs_info->bdev_holder);
  1118. if (!bdev)
  1119. return -EIO;
  1120. if (root->fs_info->fs_devices->seeding) {
  1121. seeding_dev = 1;
  1122. down_write(&sb->s_umount);
  1123. mutex_lock(&uuid_mutex);
  1124. }
  1125. filemap_write_and_wait(bdev->bd_inode->i_mapping);
  1126. mutex_lock(&root->fs_info->volume_mutex);
  1127. devices = &root->fs_info->fs_devices->devices;
  1128. list_for_each_entry(device, devices, dev_list) {
  1129. if (device->bdev == bdev) {
  1130. ret = -EEXIST;
  1131. goto error;
  1132. }
  1133. }
  1134. device = kzalloc(sizeof(*device), GFP_NOFS);
  1135. if (!device) {
  1136. /* we can safely leave the fs_devices entry around */
  1137. ret = -ENOMEM;
  1138. goto error;
  1139. }
  1140. device->name = kstrdup(device_path, GFP_NOFS);
  1141. if (!device->name) {
  1142. kfree(device);
  1143. ret = -ENOMEM;
  1144. goto error;
  1145. }
  1146. ret = find_next_devid(root, &device->devid);
  1147. if (ret) {
  1148. kfree(device);
  1149. goto error;
  1150. }
  1151. trans = btrfs_start_transaction(root, 1);
  1152. lock_chunks(root);
  1153. device->barriers = 1;
  1154. device->writeable = 1;
  1155. device->work.func = pending_bios_fn;
  1156. generate_random_uuid(device->uuid);
  1157. spin_lock_init(&device->io_lock);
  1158. device->generation = trans->transid;
  1159. device->io_width = root->sectorsize;
  1160. device->io_align = root->sectorsize;
  1161. device->sector_size = root->sectorsize;
  1162. device->total_bytes = i_size_read(bdev->bd_inode);
  1163. device->dev_root = root->fs_info->dev_root;
  1164. device->bdev = bdev;
  1165. device->in_fs_metadata = 1;
  1166. device->mode = 0;
  1167. set_blocksize(device->bdev, 4096);
  1168. if (seeding_dev) {
  1169. sb->s_flags &= ~MS_RDONLY;
  1170. ret = btrfs_prepare_sprout(trans, root);
  1171. BUG_ON(ret);
  1172. }
  1173. device->fs_devices = root->fs_info->fs_devices;
  1174. list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
  1175. list_add(&device->dev_alloc_list,
  1176. &root->fs_info->fs_devices->alloc_list);
  1177. root->fs_info->fs_devices->num_devices++;
  1178. root->fs_info->fs_devices->open_devices++;
  1179. root->fs_info->fs_devices->rw_devices++;
  1180. root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
  1181. total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
  1182. btrfs_set_super_total_bytes(&root->fs_info->super_copy,
  1183. total_bytes + device->total_bytes);
  1184. total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
  1185. btrfs_set_super_num_devices(&root->fs_info->super_copy,
  1186. total_bytes + 1);
  1187. if (seeding_dev) {
  1188. ret = init_first_rw_device(trans, root, device);
  1189. BUG_ON(ret);
  1190. ret = btrfs_finish_sprout(trans, root);
  1191. BUG_ON(ret);
  1192. } else {
  1193. ret = btrfs_add_device(trans, root, device);
  1194. }
  1195. unlock_chunks(root);
  1196. btrfs_commit_transaction(trans, root);
  1197. if (seeding_dev) {
  1198. mutex_unlock(&uuid_mutex);
  1199. up_write(&sb->s_umount);
  1200. ret = btrfs_relocate_sys_chunks(root);
  1201. BUG_ON(ret);
  1202. }
  1203. out:
  1204. mutex_unlock(&root->fs_info->volume_mutex);
  1205. return ret;
  1206. error:
  1207. close_bdev_exclusive(bdev, 0);
  1208. if (seeding_dev) {
  1209. mutex_unlock(&uuid_mutex);
  1210. up_write(&sb->s_umount);
  1211. }
  1212. goto out;
  1213. }
  1214. static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
  1215. struct btrfs_device *device)
  1216. {
  1217. int ret;
  1218. struct btrfs_path *path;
  1219. struct btrfs_root *root;
  1220. struct btrfs_dev_item *dev_item;
  1221. struct extent_buffer *leaf;
  1222. struct btrfs_key key;
  1223. root = device->dev_root->fs_info->chunk_root;
  1224. path = btrfs_alloc_path();
  1225. if (!path)
  1226. return -ENOMEM;
  1227. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1228. key.type = BTRFS_DEV_ITEM_KEY;
  1229. key.offset = device->devid;
  1230. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1231. if (ret < 0)
  1232. goto out;
  1233. if (ret > 0) {
  1234. ret = -ENOENT;
  1235. goto out;
  1236. }
  1237. leaf = path->nodes[0];
  1238. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  1239. btrfs_set_device_id(leaf, dev_item, device->devid);
  1240. btrfs_set_device_type(leaf, dev_item, device->type);
  1241. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  1242. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  1243. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  1244. btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
  1245. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  1246. btrfs_mark_buffer_dirty(leaf);
  1247. out:
  1248. btrfs_free_path(path);
  1249. return ret;
  1250. }
  1251. static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
  1252. struct btrfs_device *device, u64 new_size)
  1253. {
  1254. struct btrfs_super_block *super_copy =
  1255. &device->dev_root->fs_info->super_copy;
  1256. u64 old_total = btrfs_super_total_bytes(super_copy);
  1257. u64 diff = new_size - device->total_bytes;
  1258. if (!device->writeable)
  1259. return -EACCES;
  1260. if (new_size <= device->total_bytes)
  1261. return -EINVAL;
  1262. btrfs_set_super_total_bytes(super_copy, old_total + diff);
  1263. device->fs_devices->total_rw_bytes += diff;
  1264. device->total_bytes = new_size;
  1265. return btrfs_update_device(trans, device);
  1266. }
  1267. int btrfs_grow_device(struct btrfs_trans_handle *trans,
  1268. struct btrfs_device *device, u64 new_size)
  1269. {
  1270. int ret;
  1271. lock_chunks(device->dev_root);
  1272. ret = __btrfs_grow_device(trans, device, new_size);
  1273. unlock_chunks(device->dev_root);
  1274. return ret;
  1275. }
  1276. static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
  1277. struct btrfs_root *root,
  1278. u64 chunk_tree, u64 chunk_objectid,
  1279. u64 chunk_offset)
  1280. {
  1281. int ret;
  1282. struct btrfs_path *path;
  1283. struct btrfs_key key;
  1284. root = root->fs_info->chunk_root;
  1285. path = btrfs_alloc_path();
  1286. if (!path)
  1287. return -ENOMEM;
  1288. key.objectid = chunk_objectid;
  1289. key.offset = chunk_offset;
  1290. key.type = BTRFS_CHUNK_ITEM_KEY;
  1291. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1292. BUG_ON(ret);
  1293. ret = btrfs_del_item(trans, root, path);
  1294. BUG_ON(ret);
  1295. btrfs_free_path(path);
  1296. return 0;
  1297. }
  1298. static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
  1299. chunk_offset)
  1300. {
  1301. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  1302. struct btrfs_disk_key *disk_key;
  1303. struct btrfs_chunk *chunk;
  1304. u8 *ptr;
  1305. int ret = 0;
  1306. u32 num_stripes;
  1307. u32 array_size;
  1308. u32 len = 0;
  1309. u32 cur;
  1310. struct btrfs_key key;
  1311. array_size = btrfs_super_sys_array_size(super_copy);
  1312. ptr = super_copy->sys_chunk_array;
  1313. cur = 0;
  1314. while (cur < array_size) {
  1315. disk_key = (struct btrfs_disk_key *)ptr;
  1316. btrfs_disk_key_to_cpu(&key, disk_key);
  1317. len = sizeof(*disk_key);
  1318. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  1319. chunk = (struct btrfs_chunk *)(ptr + len);
  1320. num_stripes = btrfs_stack_chunk_num_stripes(chunk);
  1321. len += btrfs_chunk_item_size(num_stripes);
  1322. } else {
  1323. ret = -EIO;
  1324. break;
  1325. }
  1326. if (key.objectid == chunk_objectid &&
  1327. key.offset == chunk_offset) {
  1328. memmove(ptr, ptr + len, array_size - (cur + len));
  1329. array_size -= len;
  1330. btrfs_set_super_sys_array_size(super_copy, array_size);
  1331. } else {
  1332. ptr += len;
  1333. cur += len;
  1334. }
  1335. }
  1336. return ret;
  1337. }
  1338. static int btrfs_relocate_chunk(struct btrfs_root *root,
  1339. u64 chunk_tree, u64 chunk_objectid,
  1340. u64 chunk_offset)
  1341. {
  1342. struct extent_map_tree *em_tree;
  1343. struct btrfs_root *extent_root;
  1344. struct btrfs_trans_handle *trans;
  1345. struct extent_map *em;
  1346. struct map_lookup *map;
  1347. int ret;
  1348. int i;
  1349. printk(KERN_INFO "btrfs relocating chunk %llu\n",
  1350. (unsigned long long)chunk_offset);
  1351. root = root->fs_info->chunk_root;
  1352. extent_root = root->fs_info->extent_root;
  1353. em_tree = &root->fs_info->mapping_tree.map_tree;
  1354. /* step one, relocate all the extents inside this chunk */
  1355. ret = btrfs_relocate_block_group(extent_root, chunk_offset);
  1356. BUG_ON(ret);
  1357. trans = btrfs_start_transaction(root, 1);
  1358. BUG_ON(!trans);
  1359. lock_chunks(root);
  1360. /*
  1361. * step two, delete the device extents and the
  1362. * chunk tree entries
  1363. */
  1364. spin_lock(&em_tree->lock);
  1365. em = lookup_extent_mapping(em_tree, chunk_offset, 1);
  1366. spin_unlock(&em_tree->lock);
  1367. BUG_ON(em->start > chunk_offset ||
  1368. em->start + em->len < chunk_offset);
  1369. map = (struct map_lookup *)em->bdev;
  1370. for (i = 0; i < map->num_stripes; i++) {
  1371. ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
  1372. map->stripes[i].physical);
  1373. BUG_ON(ret);
  1374. if (map->stripes[i].dev) {
  1375. ret = btrfs_update_device(trans, map->stripes[i].dev);
  1376. BUG_ON(ret);
  1377. }
  1378. }
  1379. ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
  1380. chunk_offset);
  1381. BUG_ON(ret);
  1382. if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1383. ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
  1384. BUG_ON(ret);
  1385. }
  1386. ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
  1387. BUG_ON(ret);
  1388. spin_lock(&em_tree->lock);
  1389. remove_extent_mapping(em_tree, em);
  1390. spin_unlock(&em_tree->lock);
  1391. kfree(map);
  1392. em->bdev = NULL;
  1393. /* once for the tree */
  1394. free_extent_map(em);
  1395. /* once for us */
  1396. free_extent_map(em);
  1397. unlock_chunks(root);
  1398. btrfs_end_transaction(trans, root);
  1399. return 0;
  1400. }
  1401. static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
  1402. {
  1403. struct btrfs_root *chunk_root = root->fs_info->chunk_root;
  1404. struct btrfs_path *path;
  1405. struct extent_buffer *leaf;
  1406. struct btrfs_chunk *chunk;
  1407. struct btrfs_key key;
  1408. struct btrfs_key found_key;
  1409. u64 chunk_tree = chunk_root->root_key.objectid;
  1410. u64 chunk_type;
  1411. int ret;
  1412. path = btrfs_alloc_path();
  1413. if (!path)
  1414. return -ENOMEM;
  1415. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  1416. key.offset = (u64)-1;
  1417. key.type = BTRFS_CHUNK_ITEM_KEY;
  1418. while (1) {
  1419. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  1420. if (ret < 0)
  1421. goto error;
  1422. BUG_ON(ret == 0);
  1423. ret = btrfs_previous_item(chunk_root, path, key.objectid,
  1424. key.type);
  1425. if (ret < 0)
  1426. goto error;
  1427. if (ret > 0)
  1428. break;
  1429. leaf = path->nodes[0];
  1430. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1431. chunk = btrfs_item_ptr(leaf, path->slots[0],
  1432. struct btrfs_chunk);
  1433. chunk_type = btrfs_chunk_type(leaf, chunk);
  1434. btrfs_release_path(chunk_root, path);
  1435. if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1436. ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
  1437. found_key.objectid,
  1438. found_key.offset);
  1439. BUG_ON(ret);
  1440. }
  1441. if (found_key.offset == 0)
  1442. break;
  1443. key.offset = found_key.offset - 1;
  1444. }
  1445. ret = 0;
  1446. error:
  1447. btrfs_free_path(path);
  1448. return ret;
  1449. }
  1450. static u64 div_factor(u64 num, int factor)
  1451. {
  1452. if (factor == 10)
  1453. return num;
  1454. num *= factor;
  1455. do_div(num, 10);
  1456. return num;
  1457. }
  1458. int btrfs_balance(struct btrfs_root *dev_root)
  1459. {
  1460. int ret;
  1461. struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
  1462. struct btrfs_device *device;
  1463. u64 old_size;
  1464. u64 size_to_free;
  1465. struct btrfs_path *path;
  1466. struct btrfs_key key;
  1467. struct btrfs_chunk *chunk;
  1468. struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
  1469. struct btrfs_trans_handle *trans;
  1470. struct btrfs_key found_key;
  1471. if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
  1472. return -EROFS;
  1473. mutex_lock(&dev_root->fs_info->volume_mutex);
  1474. dev_root = dev_root->fs_info->dev_root;
  1475. /* step one make some room on all the devices */
  1476. list_for_each_entry(device, devices, dev_list) {
  1477. old_size = device->total_bytes;
  1478. size_to_free = div_factor(old_size, 1);
  1479. size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
  1480. if (!device->writeable ||
  1481. device->total_bytes - device->bytes_used > size_to_free)
  1482. continue;
  1483. ret = btrfs_shrink_device(device, old_size - size_to_free);
  1484. BUG_ON(ret);
  1485. trans = btrfs_start_transaction(dev_root, 1);
  1486. BUG_ON(!trans);
  1487. ret = btrfs_grow_device(trans, device, old_size);
  1488. BUG_ON(ret);
  1489. btrfs_end_transaction(trans, dev_root);
  1490. }
  1491. /* step two, relocate all the chunks */
  1492. path = btrfs_alloc_path();
  1493. BUG_ON(!path);
  1494. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  1495. key.offset = (u64)-1;
  1496. key.type = BTRFS_CHUNK_ITEM_KEY;
  1497. while (1) {
  1498. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  1499. if (ret < 0)
  1500. goto error;
  1501. /*
  1502. * this shouldn't happen, it means the last relocate
  1503. * failed
  1504. */
  1505. if (ret == 0)
  1506. break;
  1507. ret = btrfs_previous_item(chunk_root, path, 0,
  1508. BTRFS_CHUNK_ITEM_KEY);
  1509. if (ret)
  1510. break;
  1511. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1512. path->slots[0]);
  1513. if (found_key.objectid != key.objectid)
  1514. break;
  1515. chunk = btrfs_item_ptr(path->nodes[0],
  1516. path->slots[0],
  1517. struct btrfs_chunk);
  1518. key.offset = found_key.offset;
  1519. /* chunk zero is special */
  1520. if (key.offset == 0)
  1521. break;
  1522. btrfs_release_path(chunk_root, path);
  1523. ret = btrfs_relocate_chunk(chunk_root,
  1524. chunk_root->root_key.objectid,
  1525. found_key.objectid,
  1526. found_key.offset);
  1527. BUG_ON(ret);
  1528. }
  1529. ret = 0;
  1530. error:
  1531. btrfs_free_path(path);
  1532. mutex_unlock(&dev_root->fs_info->volume_mutex);
  1533. return ret;
  1534. }
  1535. /*
  1536. * shrinking a device means finding all of the device extents past
  1537. * the new size, and then following the back refs to the chunks.
  1538. * The chunk relocation code actually frees the device extent
  1539. */
  1540. int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
  1541. {
  1542. struct btrfs_trans_handle *trans;
  1543. struct btrfs_root *root = device->dev_root;
  1544. struct btrfs_dev_extent *dev_extent = NULL;
  1545. struct btrfs_path *path;
  1546. u64 length;
  1547. u64 chunk_tree;
  1548. u64 chunk_objectid;
  1549. u64 chunk_offset;
  1550. int ret;
  1551. int slot;
  1552. struct extent_buffer *l;
  1553. struct btrfs_key key;
  1554. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  1555. u64 old_total = btrfs_super_total_bytes(super_copy);
  1556. u64 diff = device->total_bytes - new_size;
  1557. if (new_size >= device->total_bytes)
  1558. return -EINVAL;
  1559. path = btrfs_alloc_path();
  1560. if (!path)
  1561. return -ENOMEM;
  1562. trans = btrfs_start_transaction(root, 1);
  1563. if (!trans) {
  1564. ret = -ENOMEM;
  1565. goto done;
  1566. }
  1567. path->reada = 2;
  1568. lock_chunks(root);
  1569. device->total_bytes = new_size;
  1570. if (device->writeable)
  1571. device->fs_devices->total_rw_bytes -= diff;
  1572. ret = btrfs_update_device(trans, device);
  1573. if (ret) {
  1574. unlock_chunks(root);
  1575. btrfs_end_transaction(trans, root);
  1576. goto done;
  1577. }
  1578. WARN_ON(diff > old_total);
  1579. btrfs_set_super_total_bytes(super_copy, old_total - diff);
  1580. unlock_chunks(root);
  1581. btrfs_end_transaction(trans, root);
  1582. key.objectid = device->devid;
  1583. key.offset = (u64)-1;
  1584. key.type = BTRFS_DEV_EXTENT_KEY;
  1585. while (1) {
  1586. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1587. if (ret < 0)
  1588. goto done;
  1589. ret = btrfs_previous_item(root, path, 0, key.type);
  1590. if (ret < 0)
  1591. goto done;
  1592. if (ret) {
  1593. ret = 0;
  1594. goto done;
  1595. }
  1596. l = path->nodes[0];
  1597. slot = path->slots[0];
  1598. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  1599. if (key.objectid != device->devid)
  1600. goto done;
  1601. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  1602. length = btrfs_dev_extent_length(l, dev_extent);
  1603. if (key.offset + length <= new_size)
  1604. goto done;
  1605. chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
  1606. chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
  1607. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  1608. btrfs_release_path(root, path);
  1609. ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
  1610. chunk_offset);
  1611. if (ret)
  1612. goto done;
  1613. }
  1614. done:
  1615. btrfs_free_path(path);
  1616. return ret;
  1617. }
  1618. static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
  1619. struct btrfs_root *root,
  1620. struct btrfs_key *key,
  1621. struct btrfs_chunk *chunk, int item_size)
  1622. {
  1623. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  1624. struct btrfs_disk_key disk_key;
  1625. u32 array_size;
  1626. u8 *ptr;
  1627. array_size = btrfs_super_sys_array_size(super_copy);
  1628. if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
  1629. return -EFBIG;
  1630. ptr = super_copy->sys_chunk_array + array_size;
  1631. btrfs_cpu_key_to_disk(&disk_key, key);
  1632. memcpy(ptr, &disk_key, sizeof(disk_key));
  1633. ptr += sizeof(disk_key);
  1634. memcpy(ptr, chunk, item_size);
  1635. item_size += sizeof(disk_key);
  1636. btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
  1637. return 0;
  1638. }
  1639. static noinline u64 chunk_bytes_by_type(u64 type, u64 calc_size,
  1640. int num_stripes, int sub_stripes)
  1641. {
  1642. if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
  1643. return calc_size;
  1644. else if (type & BTRFS_BLOCK_GROUP_RAID10)
  1645. return calc_size * (num_stripes / sub_stripes);
  1646. else
  1647. return calc_size * num_stripes;
  1648. }
  1649. static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  1650. struct btrfs_root *extent_root,
  1651. struct map_lookup **map_ret,
  1652. u64 *num_bytes, u64 *stripe_size,
  1653. u64 start, u64 type)
  1654. {
  1655. struct btrfs_fs_info *info = extent_root->fs_info;
  1656. struct btrfs_device *device = NULL;
  1657. struct btrfs_fs_devices *fs_devices = info->fs_devices;
  1658. struct list_head *cur;
  1659. struct map_lookup *map = NULL;
  1660. struct extent_map_tree *em_tree;
  1661. struct extent_map *em;
  1662. struct list_head private_devs;
  1663. int min_stripe_size = 1 * 1024 * 1024;
  1664. u64 calc_size = 1024 * 1024 * 1024;
  1665. u64 max_chunk_size = calc_size;
  1666. u64 min_free;
  1667. u64 avail;
  1668. u64 max_avail = 0;
  1669. u64 dev_offset;
  1670. int num_stripes = 1;
  1671. int min_stripes = 1;
  1672. int sub_stripes = 0;
  1673. int looped = 0;
  1674. int ret;
  1675. int index;
  1676. int stripe_len = 64 * 1024;
  1677. if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
  1678. (type & BTRFS_BLOCK_GROUP_DUP)) {
  1679. WARN_ON(1);
  1680. type &= ~BTRFS_BLOCK_GROUP_DUP;
  1681. }
  1682. if (list_empty(&fs_devices->alloc_list))
  1683. return -ENOSPC;
  1684. if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
  1685. num_stripes = fs_devices->rw_devices;
  1686. min_stripes = 2;
  1687. }
  1688. if (type & (BTRFS_BLOCK_GROUP_DUP)) {
  1689. num_stripes = 2;
  1690. min_stripes = 2;
  1691. }
  1692. if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
  1693. num_stripes = min_t(u64, 2, fs_devices->rw_devices);
  1694. if (num_stripes < 2)
  1695. return -ENOSPC;
  1696. min_stripes = 2;
  1697. }
  1698. if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
  1699. num_stripes = fs_devices->rw_devices;
  1700. if (num_stripes < 4)
  1701. return -ENOSPC;
  1702. num_stripes &= ~(u32)1;
  1703. sub_stripes = 2;
  1704. min_stripes = 4;
  1705. }
  1706. if (type & BTRFS_BLOCK_GROUP_DATA) {
  1707. max_chunk_size = 10 * calc_size;
  1708. min_stripe_size = 64 * 1024 * 1024;
  1709. } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
  1710. max_chunk_size = 4 * calc_size;
  1711. min_stripe_size = 32 * 1024 * 1024;
  1712. } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1713. calc_size = 8 * 1024 * 1024;
  1714. max_chunk_size = calc_size * 2;
  1715. min_stripe_size = 1 * 1024 * 1024;
  1716. }
  1717. /* we don't want a chunk larger than 10% of writeable space */
  1718. max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
  1719. max_chunk_size);
  1720. again:
  1721. if (!map || map->num_stripes != num_stripes) {
  1722. kfree(map);
  1723. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  1724. if (!map)
  1725. return -ENOMEM;
  1726. map->num_stripes = num_stripes;
  1727. }
  1728. if (calc_size * num_stripes > max_chunk_size) {
  1729. calc_size = max_chunk_size;
  1730. do_div(calc_size, num_stripes);
  1731. do_div(calc_size, stripe_len);
  1732. calc_size *= stripe_len;
  1733. }
  1734. /* we don't want tiny stripes */
  1735. calc_size = max_t(u64, min_stripe_size, calc_size);
  1736. do_div(calc_size, stripe_len);
  1737. calc_size *= stripe_len;
  1738. cur = fs_devices->alloc_list.next;
  1739. index = 0;
  1740. if (type & BTRFS_BLOCK_GROUP_DUP)
  1741. min_free = calc_size * 2;
  1742. else
  1743. min_free = calc_size;
  1744. /*
  1745. * we add 1MB because we never use the first 1MB of the device, unless
  1746. * we've looped, then we are likely allocating the maximum amount of
  1747. * space left already
  1748. */
  1749. if (!looped)
  1750. min_free += 1024 * 1024;
  1751. INIT_LIST_HEAD(&private_devs);
  1752. while (index < num_stripes) {
  1753. device = list_entry(cur, struct btrfs_device, dev_alloc_list);
  1754. BUG_ON(!device->writeable);
  1755. if (device->total_bytes > device->bytes_used)
  1756. avail = device->total_bytes - device->bytes_used;
  1757. else
  1758. avail = 0;
  1759. cur = cur->next;
  1760. if (device->in_fs_metadata && avail >= min_free) {
  1761. ret = find_free_dev_extent(trans, device,
  1762. min_free, &dev_offset);
  1763. if (ret == 0) {
  1764. list_move_tail(&device->dev_alloc_list,
  1765. &private_devs);
  1766. map->stripes[index].dev = device;
  1767. map->stripes[index].physical = dev_offset;
  1768. index++;
  1769. if (type & BTRFS_BLOCK_GROUP_DUP) {
  1770. map->stripes[index].dev = device;
  1771. map->stripes[index].physical =
  1772. dev_offset + calc_size;
  1773. index++;
  1774. }
  1775. }
  1776. } else if (device->in_fs_metadata && avail > max_avail)
  1777. max_avail = avail;
  1778. if (cur == &fs_devices->alloc_list)
  1779. break;
  1780. }
  1781. list_splice(&private_devs, &fs_devices->alloc_list);
  1782. if (index < num_stripes) {
  1783. if (index >= min_stripes) {
  1784. num_stripes = index;
  1785. if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
  1786. num_stripes /= sub_stripes;
  1787. num_stripes *= sub_stripes;
  1788. }
  1789. looped = 1;
  1790. goto again;
  1791. }
  1792. if (!looped && max_avail > 0) {
  1793. looped = 1;
  1794. calc_size = max_avail;
  1795. goto again;
  1796. }
  1797. kfree(map);
  1798. return -ENOSPC;
  1799. }
  1800. map->sector_size = extent_root->sectorsize;
  1801. map->stripe_len = stripe_len;
  1802. map->io_align = stripe_len;
  1803. map->io_width = stripe_len;
  1804. map->type = type;
  1805. map->num_stripes = num_stripes;
  1806. map->sub_stripes = sub_stripes;
  1807. *map_ret = map;
  1808. *stripe_size = calc_size;
  1809. *num_bytes = chunk_bytes_by_type(type, calc_size,
  1810. num_stripes, sub_stripes);
  1811. em = alloc_extent_map(GFP_NOFS);
  1812. if (!em) {
  1813. kfree(map);
  1814. return -ENOMEM;
  1815. }
  1816. em->bdev = (struct block_device *)map;
  1817. em->start = start;
  1818. em->len = *num_bytes;
  1819. em->block_start = 0;
  1820. em->block_len = em->len;
  1821. em_tree = &extent_root->fs_info->mapping_tree.map_tree;
  1822. spin_lock(&em_tree->lock);
  1823. ret = add_extent_mapping(em_tree, em);
  1824. spin_unlock(&em_tree->lock);
  1825. BUG_ON(ret);
  1826. free_extent_map(em);
  1827. ret = btrfs_make_block_group(trans, extent_root, 0, type,
  1828. BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  1829. start, *num_bytes);
  1830. BUG_ON(ret);
  1831. index = 0;
  1832. while (index < map->num_stripes) {
  1833. device = map->stripes[index].dev;
  1834. dev_offset = map->stripes[index].physical;
  1835. ret = btrfs_alloc_dev_extent(trans, device,
  1836. info->chunk_root->root_key.objectid,
  1837. BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  1838. start, dev_offset, calc_size);
  1839. BUG_ON(ret);
  1840. index++;
  1841. }
  1842. return 0;
  1843. }
  1844. static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
  1845. struct btrfs_root *extent_root,
  1846. struct map_lookup *map, u64 chunk_offset,
  1847. u64 chunk_size, u64 stripe_size)
  1848. {
  1849. u64 dev_offset;
  1850. struct btrfs_key key;
  1851. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  1852. struct btrfs_device *device;
  1853. struct btrfs_chunk *chunk;
  1854. struct btrfs_stripe *stripe;
  1855. size_t item_size = btrfs_chunk_item_size(map->num_stripes);
  1856. int index = 0;
  1857. int ret;
  1858. chunk = kzalloc(item_size, GFP_NOFS);
  1859. if (!chunk)
  1860. return -ENOMEM;
  1861. index = 0;
  1862. while (index < map->num_stripes) {
  1863. device = map->stripes[index].dev;
  1864. device->bytes_used += stripe_size;
  1865. ret = btrfs_update_device(trans, device);
  1866. BUG_ON(ret);
  1867. index++;
  1868. }
  1869. index = 0;
  1870. stripe = &chunk->stripe;
  1871. while (index < map->num_stripes) {
  1872. device = map->stripes[index].dev;
  1873. dev_offset = map->stripes[index].physical;
  1874. btrfs_set_stack_stripe_devid(stripe, device->devid);
  1875. btrfs_set_stack_stripe_offset(stripe, dev_offset);
  1876. memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
  1877. stripe++;
  1878. index++;
  1879. }
  1880. btrfs_set_stack_chunk_length(chunk, chunk_size);
  1881. btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
  1882. btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
  1883. btrfs_set_stack_chunk_type(chunk, map->type);
  1884. btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
  1885. btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
  1886. btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
  1887. btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
  1888. btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
  1889. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  1890. key.type = BTRFS_CHUNK_ITEM_KEY;
  1891. key.offset = chunk_offset;
  1892. ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
  1893. BUG_ON(ret);
  1894. if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1895. ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
  1896. item_size);
  1897. BUG_ON(ret);
  1898. }
  1899. kfree(chunk);
  1900. return 0;
  1901. }
  1902. /*
  1903. * Chunk allocation falls into two parts. The first part does works
  1904. * that make the new allocated chunk useable, but not do any operation
  1905. * that modifies the chunk tree. The second part does the works that
  1906. * require modifying the chunk tree. This division is important for the
  1907. * bootstrap process of adding storage to a seed btrfs.
  1908. */
  1909. int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  1910. struct btrfs_root *extent_root, u64 type)
  1911. {
  1912. u64 chunk_offset;
  1913. u64 chunk_size;
  1914. u64 stripe_size;
  1915. struct map_lookup *map;
  1916. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  1917. int ret;
  1918. ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  1919. &chunk_offset);
  1920. if (ret)
  1921. return ret;
  1922. ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
  1923. &stripe_size, chunk_offset, type);
  1924. if (ret)
  1925. return ret;
  1926. ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
  1927. chunk_size, stripe_size);
  1928. BUG_ON(ret);
  1929. return 0;
  1930. }
  1931. static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
  1932. struct btrfs_root *root,
  1933. struct btrfs_device *device)
  1934. {
  1935. u64 chunk_offset;
  1936. u64 sys_chunk_offset;
  1937. u64 chunk_size;
  1938. u64 sys_chunk_size;
  1939. u64 stripe_size;
  1940. u64 sys_stripe_size;
  1941. u64 alloc_profile;
  1942. struct map_lookup *map;
  1943. struct map_lookup *sys_map;
  1944. struct btrfs_fs_info *fs_info = root->fs_info;
  1945. struct btrfs_root *extent_root = fs_info->extent_root;
  1946. int ret;
  1947. ret = find_next_chunk(fs_info->chunk_root,
  1948. BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
  1949. BUG_ON(ret);
  1950. alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
  1951. (fs_info->metadata_alloc_profile &
  1952. fs_info->avail_metadata_alloc_bits);
  1953. alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
  1954. ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
  1955. &stripe_size, chunk_offset, alloc_profile);
  1956. BUG_ON(ret);
  1957. sys_chunk_offset = chunk_offset + chunk_size;
  1958. alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
  1959. (fs_info->system_alloc_profile &
  1960. fs_info->avail_system_alloc_bits);
  1961. alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
  1962. ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
  1963. &sys_chunk_size, &sys_stripe_size,
  1964. sys_chunk_offset, alloc_profile);
  1965. BUG_ON(ret);
  1966. ret = btrfs_add_device(trans, fs_info->chunk_root, device);
  1967. BUG_ON(ret);
  1968. /*
  1969. * Modifying chunk tree needs allocating new blocks from both
  1970. * system block group and metadata block group. So we only can
  1971. * do operations require modifying the chunk tree after both
  1972. * block groups were created.
  1973. */
  1974. ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
  1975. chunk_size, stripe_size);
  1976. BUG_ON(ret);
  1977. ret = __finish_chunk_alloc(trans, extent_root, sys_map,
  1978. sys_chunk_offset, sys_chunk_size,
  1979. sys_stripe_size);
  1980. BUG_ON(ret);
  1981. return 0;
  1982. }
  1983. int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
  1984. {
  1985. struct extent_map *em;
  1986. struct map_lookup *map;
  1987. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  1988. int readonly = 0;
  1989. int i;
  1990. spin_lock(&map_tree->map_tree.lock);
  1991. em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
  1992. spin_unlock(&map_tree->map_tree.lock);
  1993. if (!em)
  1994. return 1;
  1995. map = (struct map_lookup *)em->bdev;
  1996. for (i = 0; i < map->num_stripes; i++) {
  1997. if (!map->stripes[i].dev->writeable) {
  1998. readonly = 1;
  1999. break;
  2000. }
  2001. }
  2002. free_extent_map(em);
  2003. return readonly;
  2004. }
  2005. void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
  2006. {
  2007. extent_map_tree_init(&tree->map_tree, GFP_NOFS);
  2008. }
  2009. void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
  2010. {
  2011. struct extent_map *em;
  2012. while (1) {
  2013. spin_lock(&tree->map_tree.lock);
  2014. em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
  2015. if (em)
  2016. remove_extent_mapping(&tree->map_tree, em);
  2017. spin_unlock(&tree->map_tree.lock);
  2018. if (!em)
  2019. break;
  2020. kfree(em->bdev);
  2021. /* once for us */
  2022. free_extent_map(em);
  2023. /* once for the tree */
  2024. free_extent_map(em);
  2025. }
  2026. }
  2027. int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
  2028. {
  2029. struct extent_map *em;
  2030. struct map_lookup *map;
  2031. struct extent_map_tree *em_tree = &map_tree->map_tree;
  2032. int ret;
  2033. spin_lock(&em_tree->lock);
  2034. em = lookup_extent_mapping(em_tree, logical, len);
  2035. spin_unlock(&em_tree->lock);
  2036. BUG_ON(!em);
  2037. BUG_ON(em->start > logical || em->start + em->len < logical);
  2038. map = (struct map_lookup *)em->bdev;
  2039. if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
  2040. ret = map->num_stripes;
  2041. else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  2042. ret = map->sub_stripes;
  2043. else
  2044. ret = 1;
  2045. free_extent_map(em);
  2046. return ret;
  2047. }
  2048. static int find_live_mirror(struct map_lookup *map, int first, int num,
  2049. int optimal)
  2050. {
  2051. int i;
  2052. if (map->stripes[optimal].dev->bdev)
  2053. return optimal;
  2054. for (i = first; i < first + num; i++) {
  2055. if (map->stripes[i].dev->bdev)
  2056. return i;
  2057. }
  2058. /* we couldn't find one that doesn't fail. Just return something
  2059. * and the io error handling code will clean up eventually
  2060. */
  2061. return optimal;
  2062. }
  2063. static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  2064. u64 logical, u64 *length,
  2065. struct btrfs_multi_bio **multi_ret,
  2066. int mirror_num, struct page *unplug_page)
  2067. {
  2068. struct extent_map *em;
  2069. struct map_lookup *map;
  2070. struct extent_map_tree *em_tree = &map_tree->map_tree;
  2071. u64 offset;
  2072. u64 stripe_offset;
  2073. u64 stripe_nr;
  2074. int stripes_allocated = 8;
  2075. int stripes_required = 1;
  2076. int stripe_index;
  2077. int i;
  2078. int num_stripes;
  2079. int max_errors = 0;
  2080. struct btrfs_multi_bio *multi = NULL;
  2081. if (multi_ret && !(rw & (1 << BIO_RW)))
  2082. stripes_allocated = 1;
  2083. again:
  2084. if (multi_ret) {
  2085. multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
  2086. GFP_NOFS);
  2087. if (!multi)
  2088. return -ENOMEM;
  2089. atomic_set(&multi->error, 0);
  2090. }
  2091. spin_lock(&em_tree->lock);
  2092. em = lookup_extent_mapping(em_tree, logical, *length);
  2093. spin_unlock(&em_tree->lock);
  2094. if (!em && unplug_page)
  2095. return 0;
  2096. if (!em) {
  2097. printk(KERN_CRIT "unable to find logical %llu len %llu\n",
  2098. (unsigned long long)logical,
  2099. (unsigned long long)*length);
  2100. BUG();
  2101. }
  2102. BUG_ON(em->start > logical || em->start + em->len < logical);
  2103. map = (struct map_lookup *)em->bdev;
  2104. offset = logical - em->start;
  2105. if (mirror_num > map->num_stripes)
  2106. mirror_num = 0;
  2107. /* if our multi bio struct is too small, back off and try again */
  2108. if (rw & (1 << BIO_RW)) {
  2109. if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
  2110. BTRFS_BLOCK_GROUP_DUP)) {
  2111. stripes_required = map->num_stripes;
  2112. max_errors = 1;
  2113. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  2114. stripes_required = map->sub_stripes;
  2115. max_errors = 1;
  2116. }
  2117. }
  2118. if (multi_ret && rw == WRITE &&
  2119. stripes_allocated < stripes_required) {
  2120. stripes_allocated = map->num_stripes;
  2121. free_extent_map(em);
  2122. kfree(multi);
  2123. goto again;
  2124. }
  2125. stripe_nr = offset;
  2126. /*
  2127. * stripe_nr counts the total number of stripes we have to stride
  2128. * to get to this block
  2129. */
  2130. do_div(stripe_nr, map->stripe_len);
  2131. stripe_offset = stripe_nr * map->stripe_len;
  2132. BUG_ON(offset < stripe_offset);
  2133. /* stripe_offset is the offset of this block in its stripe*/
  2134. stripe_offset = offset - stripe_offset;
  2135. if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
  2136. BTRFS_BLOCK_GROUP_RAID10 |
  2137. BTRFS_BLOCK_GROUP_DUP)) {
  2138. /* we limit the length of each bio to what fits in a stripe */
  2139. *length = min_t(u64, em->len - offset,
  2140. map->stripe_len - stripe_offset);
  2141. } else {
  2142. *length = em->len - offset;
  2143. }
  2144. if (!multi_ret && !unplug_page)
  2145. goto out;
  2146. num_stripes = 1;
  2147. stripe_index = 0;
  2148. if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  2149. if (unplug_page || (rw & (1 << BIO_RW)))
  2150. num_stripes = map->num_stripes;
  2151. else if (mirror_num)
  2152. stripe_index = mirror_num - 1;
  2153. else {
  2154. stripe_index = find_live_mirror(map, 0,
  2155. map->num_stripes,
  2156. current->pid % map->num_stripes);
  2157. }
  2158. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  2159. if (rw & (1 << BIO_RW))
  2160. num_stripes = map->num_stripes;
  2161. else if (mirror_num)
  2162. stripe_index = mirror_num - 1;
  2163. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  2164. int factor = map->num_stripes / map->sub_stripes;
  2165. stripe_index = do_div(stripe_nr, factor);
  2166. stripe_index *= map->sub_stripes;
  2167. if (unplug_page || (rw & (1 << BIO_RW)))
  2168. num_stripes = map->sub_stripes;
  2169. else if (mirror_num)
  2170. stripe_index += mirror_num - 1;
  2171. else {
  2172. stripe_index = find_live_mirror(map, stripe_index,
  2173. map->sub_stripes, stripe_index +
  2174. current->pid % map->sub_stripes);
  2175. }
  2176. } else {
  2177. /*
  2178. * after this do_div call, stripe_nr is the number of stripes
  2179. * on this device we have to walk to find the data, and
  2180. * stripe_index is the number of our device in the stripe array
  2181. */
  2182. stripe_index = do_div(stripe_nr, map->num_stripes);
  2183. }
  2184. BUG_ON(stripe_index >= map->num_stripes);
  2185. for (i = 0; i < num_stripes; i++) {
  2186. if (unplug_page) {
  2187. struct btrfs_device *device;
  2188. struct backing_dev_info *bdi;
  2189. device = map->stripes[stripe_index].dev;
  2190. if (device->bdev) {
  2191. bdi = blk_get_backing_dev_info(device->bdev);
  2192. if (bdi->unplug_io_fn)
  2193. bdi->unplug_io_fn(bdi, unplug_page);
  2194. }
  2195. } else {
  2196. multi->stripes[i].physical =
  2197. map->stripes[stripe_index].physical +
  2198. stripe_offset + stripe_nr * map->stripe_len;
  2199. multi->stripes[i].dev = map->stripes[stripe_index].dev;
  2200. }
  2201. stripe_index++;
  2202. }
  2203. if (multi_ret) {
  2204. *multi_ret = multi;
  2205. multi->num_stripes = num_stripes;
  2206. multi->max_errors = max_errors;
  2207. }
  2208. out:
  2209. free_extent_map(em);
  2210. return 0;
  2211. }
  2212. int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  2213. u64 logical, u64 *length,
  2214. struct btrfs_multi_bio **multi_ret, int mirror_num)
  2215. {
  2216. return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
  2217. mirror_num, NULL);
  2218. }
  2219. int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
  2220. u64 chunk_start, u64 physical, u64 devid,
  2221. u64 **logical, int *naddrs, int *stripe_len)
  2222. {
  2223. struct extent_map_tree *em_tree = &map_tree->map_tree;
  2224. struct extent_map *em;
  2225. struct map_lookup *map;
  2226. u64 *buf;
  2227. u64 bytenr;
  2228. u64 length;
  2229. u64 stripe_nr;
  2230. int i, j, nr = 0;
  2231. spin_lock(&em_tree->lock);
  2232. em = lookup_extent_mapping(em_tree, chunk_start, 1);
  2233. spin_unlock(&em_tree->lock);
  2234. BUG_ON(!em || em->start != chunk_start);
  2235. map = (struct map_lookup *)em->bdev;
  2236. length = em->len;
  2237. if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  2238. do_div(length, map->num_stripes / map->sub_stripes);
  2239. else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
  2240. do_div(length, map->num_stripes);
  2241. buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
  2242. BUG_ON(!buf);
  2243. for (i = 0; i < map->num_stripes; i++) {
  2244. if (devid && map->stripes[i].dev->devid != devid)
  2245. continue;
  2246. if (map->stripes[i].physical > physical ||
  2247. map->stripes[i].physical + length <= physical)
  2248. continue;
  2249. stripe_nr = physical - map->stripes[i].physical;
  2250. do_div(stripe_nr, map->stripe_len);
  2251. if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  2252. stripe_nr = stripe_nr * map->num_stripes + i;
  2253. do_div(stripe_nr, map->sub_stripes);
  2254. } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  2255. stripe_nr = stripe_nr * map->num_stripes + i;
  2256. }
  2257. bytenr = chunk_start + stripe_nr * map->stripe_len;
  2258. WARN_ON(nr >= map->num_stripes);
  2259. for (j = 0; j < nr; j++) {
  2260. if (buf[j] == bytenr)
  2261. break;
  2262. }
  2263. if (j == nr) {
  2264. WARN_ON(nr >= map->num_stripes);
  2265. buf[nr++] = bytenr;
  2266. }
  2267. }
  2268. for (i = 0; i > nr; i++) {
  2269. struct btrfs_multi_bio *multi;
  2270. struct btrfs_bio_stripe *stripe;
  2271. int ret;
  2272. length = 1;
  2273. ret = btrfs_map_block(map_tree, WRITE, buf[i],
  2274. &length, &multi, 0);
  2275. BUG_ON(ret);
  2276. stripe = multi->stripes;
  2277. for (j = 0; j < multi->num_stripes; j++) {
  2278. if (stripe->physical >= physical &&
  2279. physical < stripe->physical + length)
  2280. break;
  2281. }
  2282. BUG_ON(j >= multi->num_stripes);
  2283. kfree(multi);
  2284. }
  2285. *logical = buf;
  2286. *naddrs = nr;
  2287. *stripe_len = map->stripe_len;
  2288. free_extent_map(em);
  2289. return 0;
  2290. }
  2291. int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
  2292. u64 logical, struct page *page)
  2293. {
  2294. u64 length = PAGE_CACHE_SIZE;
  2295. return __btrfs_map_block(map_tree, READ, logical, &length,
  2296. NULL, 0, page);
  2297. }
  2298. static void end_bio_multi_stripe(struct bio *bio, int err)
  2299. {
  2300. struct btrfs_multi_bio *multi = bio->bi_private;
  2301. int is_orig_bio = 0;
  2302. if (err)
  2303. atomic_inc(&multi->error);
  2304. if (bio == multi->orig_bio)
  2305. is_orig_bio = 1;
  2306. if (atomic_dec_and_test(&multi->stripes_pending)) {
  2307. if (!is_orig_bio) {
  2308. bio_put(bio);
  2309. bio = multi->orig_bio;
  2310. }
  2311. bio->bi_private = multi->private;
  2312. bio->bi_end_io = multi->end_io;
  2313. /* only send an error to the higher layers if it is
  2314. * beyond the tolerance of the multi-bio
  2315. */
  2316. if (atomic_read(&multi->error) > multi->max_errors) {
  2317. err = -EIO;
  2318. } else if (err) {
  2319. /*
  2320. * this bio is actually up to date, we didn't
  2321. * go over the max number of errors
  2322. */
  2323. set_bit(BIO_UPTODATE, &bio->bi_flags);
  2324. err = 0;
  2325. }
  2326. kfree(multi);
  2327. bio_endio(bio, err);
  2328. } else if (!is_orig_bio) {
  2329. bio_put(bio);
  2330. }
  2331. }
  2332. struct async_sched {
  2333. struct bio *bio;
  2334. int rw;
  2335. struct btrfs_fs_info *info;
  2336. struct btrfs_work work;
  2337. };
  2338. /*
  2339. * see run_scheduled_bios for a description of why bios are collected for
  2340. * async submit.
  2341. *
  2342. * This will add one bio to the pending list for a device and make sure
  2343. * the work struct is scheduled.
  2344. */
  2345. static noinline int schedule_bio(struct btrfs_root *root,
  2346. struct btrfs_device *device,
  2347. int rw, struct bio *bio)
  2348. {
  2349. int should_queue = 1;
  2350. /* don't bother with additional async steps for reads, right now */
  2351. if (!(rw & (1 << BIO_RW))) {
  2352. bio_get(bio);
  2353. submit_bio(rw, bio);
  2354. bio_put(bio);
  2355. return 0;
  2356. }
  2357. /*
  2358. * nr_async_bios allows us to reliably return congestion to the
  2359. * higher layers. Otherwise, the async bio makes it appear we have
  2360. * made progress against dirty pages when we've really just put it
  2361. * on a queue for later
  2362. */
  2363. atomic_inc(&root->fs_info->nr_async_bios);
  2364. WARN_ON(bio->bi_next);
  2365. bio->bi_next = NULL;
  2366. bio->bi_rw |= rw;
  2367. spin_lock(&device->io_lock);
  2368. if (device->pending_bio_tail)
  2369. device->pending_bio_tail->bi_next = bio;
  2370. device->pending_bio_tail = bio;
  2371. if (!device->pending_bios)
  2372. device->pending_bios = bio;
  2373. if (device->running_pending)
  2374. should_queue = 0;
  2375. spin_unlock(&device->io_lock);
  2376. if (should_queue)
  2377. btrfs_queue_worker(&root->fs_info->submit_workers,
  2378. &device->work);
  2379. return 0;
  2380. }
  2381. int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
  2382. int mirror_num, int async_submit)
  2383. {
  2384. struct btrfs_mapping_tree *map_tree;
  2385. struct btrfs_device *dev;
  2386. struct bio *first_bio = bio;
  2387. u64 logical = (u64)bio->bi_sector << 9;
  2388. u64 length = 0;
  2389. u64 map_length;
  2390. struct btrfs_multi_bio *multi = NULL;
  2391. int ret;
  2392. int dev_nr = 0;
  2393. int total_devs = 1;
  2394. length = bio->bi_size;
  2395. map_tree = &root->fs_info->mapping_tree;
  2396. map_length = length;
  2397. ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
  2398. mirror_num);
  2399. BUG_ON(ret);
  2400. total_devs = multi->num_stripes;
  2401. if (map_length < length) {
  2402. printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
  2403. "len %llu\n", (unsigned long long)logical,
  2404. (unsigned long long)length,
  2405. (unsigned long long)map_length);
  2406. BUG();
  2407. }
  2408. multi->end_io = first_bio->bi_end_io;
  2409. multi->private = first_bio->bi_private;
  2410. multi->orig_bio = first_bio;
  2411. atomic_set(&multi->stripes_pending, multi->num_stripes);
  2412. while (dev_nr < total_devs) {
  2413. if (total_devs > 1) {
  2414. if (dev_nr < total_devs - 1) {
  2415. bio = bio_clone(first_bio, GFP_NOFS);
  2416. BUG_ON(!bio);
  2417. } else {
  2418. bio = first_bio;
  2419. }
  2420. bio->bi_private = multi;
  2421. bio->bi_end_io = end_bio_multi_stripe;
  2422. }
  2423. bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
  2424. dev = multi->stripes[dev_nr].dev;
  2425. BUG_ON(rw == WRITE && !dev->writeable);
  2426. if (dev && dev->bdev) {
  2427. bio->bi_bdev = dev->bdev;
  2428. if (async_submit)
  2429. schedule_bio(root, dev, rw, bio);
  2430. else
  2431. submit_bio(rw, bio);
  2432. } else {
  2433. bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
  2434. bio->bi_sector = logical >> 9;
  2435. bio_endio(bio, -EIO);
  2436. }
  2437. dev_nr++;
  2438. }
  2439. if (total_devs == 1)
  2440. kfree(multi);
  2441. return 0;
  2442. }
  2443. struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
  2444. u8 *uuid, u8 *fsid)
  2445. {
  2446. struct btrfs_device *device;
  2447. struct btrfs_fs_devices *cur_devices;
  2448. cur_devices = root->fs_info->fs_devices;
  2449. while (cur_devices) {
  2450. if (!fsid ||
  2451. !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
  2452. device = __find_device(&cur_devices->devices,
  2453. devid, uuid);
  2454. if (device)
  2455. return device;
  2456. }
  2457. cur_devices = cur_devices->seed;
  2458. }
  2459. return NULL;
  2460. }
  2461. static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
  2462. u64 devid, u8 *dev_uuid)
  2463. {
  2464. struct btrfs_device *device;
  2465. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  2466. device = kzalloc(sizeof(*device), GFP_NOFS);
  2467. if (!device)
  2468. return NULL;
  2469. list_add(&device->dev_list,
  2470. &fs_devices->devices);
  2471. device->barriers = 1;
  2472. device->dev_root = root->fs_info->dev_root;
  2473. device->devid = devid;
  2474. device->work.func = pending_bios_fn;
  2475. device->fs_devices = fs_devices;
  2476. fs_devices->num_devices++;
  2477. spin_lock_init(&device->io_lock);
  2478. INIT_LIST_HEAD(&device->dev_alloc_list);
  2479. memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
  2480. return device;
  2481. }
  2482. static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
  2483. struct extent_buffer *leaf,
  2484. struct btrfs_chunk *chunk)
  2485. {
  2486. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  2487. struct map_lookup *map;
  2488. struct extent_map *em;
  2489. u64 logical;
  2490. u64 length;
  2491. u64 devid;
  2492. u8 uuid[BTRFS_UUID_SIZE];
  2493. int num_stripes;
  2494. int ret;
  2495. int i;
  2496. logical = key->offset;
  2497. length = btrfs_chunk_length(leaf, chunk);
  2498. spin_lock(&map_tree->map_tree.lock);
  2499. em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
  2500. spin_unlock(&map_tree->map_tree.lock);
  2501. /* already mapped? */
  2502. if (em && em->start <= logical && em->start + em->len > logical) {
  2503. free_extent_map(em);
  2504. return 0;
  2505. } else if (em) {
  2506. free_extent_map(em);
  2507. }
  2508. em = alloc_extent_map(GFP_NOFS);
  2509. if (!em)
  2510. return -ENOMEM;
  2511. num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  2512. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  2513. if (!map) {
  2514. free_extent_map(em);
  2515. return -ENOMEM;
  2516. }
  2517. em->bdev = (struct block_device *)map;
  2518. em->start = logical;
  2519. em->len = length;
  2520. em->block_start = 0;
  2521. em->block_len = em->len;
  2522. map->num_stripes = num_stripes;
  2523. map->io_width = btrfs_chunk_io_width(leaf, chunk);
  2524. map->io_align = btrfs_chunk_io_align(leaf, chunk);
  2525. map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
  2526. map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
  2527. map->type = btrfs_chunk_type(leaf, chunk);
  2528. map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
  2529. for (i = 0; i < num_stripes; i++) {
  2530. map->stripes[i].physical =
  2531. btrfs_stripe_offset_nr(leaf, chunk, i);
  2532. devid = btrfs_stripe_devid_nr(leaf, chunk, i);
  2533. read_extent_buffer(leaf, uuid, (unsigned long)
  2534. btrfs_stripe_dev_uuid_nr(chunk, i),
  2535. BTRFS_UUID_SIZE);
  2536. map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
  2537. NULL);
  2538. if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
  2539. kfree(map);
  2540. free_extent_map(em);
  2541. return -EIO;
  2542. }
  2543. if (!map->stripes[i].dev) {
  2544. map->stripes[i].dev =
  2545. add_missing_dev(root, devid, uuid);
  2546. if (!map->stripes[i].dev) {
  2547. kfree(map);
  2548. free_extent_map(em);
  2549. return -EIO;
  2550. }
  2551. }
  2552. map->stripes[i].dev->in_fs_metadata = 1;
  2553. }
  2554. spin_lock(&map_tree->map_tree.lock);
  2555. ret = add_extent_mapping(&map_tree->map_tree, em);
  2556. spin_unlock(&map_tree->map_tree.lock);
  2557. BUG_ON(ret);
  2558. free_extent_map(em);
  2559. return 0;
  2560. }
  2561. static int fill_device_from_item(struct extent_buffer *leaf,
  2562. struct btrfs_dev_item *dev_item,
  2563. struct btrfs_device *device)
  2564. {
  2565. unsigned long ptr;
  2566. device->devid = btrfs_device_id(leaf, dev_item);
  2567. device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
  2568. device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
  2569. device->type = btrfs_device_type(leaf, dev_item);
  2570. device->io_align = btrfs_device_io_align(leaf, dev_item);
  2571. device->io_width = btrfs_device_io_width(leaf, dev_item);
  2572. device->sector_size = btrfs_device_sector_size(leaf, dev_item);
  2573. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  2574. read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  2575. return 0;
  2576. }
  2577. static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
  2578. {
  2579. struct btrfs_fs_devices *fs_devices;
  2580. int ret;
  2581. mutex_lock(&uuid_mutex);
  2582. fs_devices = root->fs_info->fs_devices->seed;
  2583. while (fs_devices) {
  2584. if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
  2585. ret = 0;
  2586. goto out;
  2587. }
  2588. fs_devices = fs_devices->seed;
  2589. }
  2590. fs_devices = find_fsid(fsid);
  2591. if (!fs_devices) {
  2592. ret = -ENOENT;
  2593. goto out;
  2594. }
  2595. fs_devices = clone_fs_devices(fs_devices);
  2596. if (IS_ERR(fs_devices)) {
  2597. ret = PTR_ERR(fs_devices);
  2598. goto out;
  2599. }
  2600. ret = __btrfs_open_devices(fs_devices, FMODE_READ,
  2601. root->fs_info->bdev_holder);
  2602. if (ret)
  2603. goto out;
  2604. if (!fs_devices->seeding) {
  2605. __btrfs_close_devices(fs_devices);
  2606. free_fs_devices(fs_devices);
  2607. ret = -EINVAL;
  2608. goto out;
  2609. }
  2610. fs_devices->seed = root->fs_info->fs_devices->seed;
  2611. root->fs_info->fs_devices->seed = fs_devices;
  2612. out:
  2613. mutex_unlock(&uuid_mutex);
  2614. return ret;
  2615. }
  2616. static int read_one_dev(struct btrfs_root *root,
  2617. struct extent_buffer *leaf,
  2618. struct btrfs_dev_item *dev_item)
  2619. {
  2620. struct btrfs_device *device;
  2621. u64 devid;
  2622. int ret;
  2623. u8 fs_uuid[BTRFS_UUID_SIZE];
  2624. u8 dev_uuid[BTRFS_UUID_SIZE];
  2625. devid = btrfs_device_id(leaf, dev_item);
  2626. read_extent_buffer(leaf, dev_uuid,
  2627. (unsigned long)btrfs_device_uuid(dev_item),
  2628. BTRFS_UUID_SIZE);
  2629. read_extent_buffer(leaf, fs_uuid,
  2630. (unsigned long)btrfs_device_fsid(dev_item),
  2631. BTRFS_UUID_SIZE);
  2632. if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
  2633. ret = open_seed_devices(root, fs_uuid);
  2634. if (ret && !btrfs_test_opt(root, DEGRADED))
  2635. return ret;
  2636. }
  2637. device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
  2638. if (!device || !device->bdev) {
  2639. if (!btrfs_test_opt(root, DEGRADED))
  2640. return -EIO;
  2641. if (!device) {
  2642. printk(KERN_WARNING "warning devid %llu missing\n",
  2643. (unsigned long long)devid);
  2644. device = add_missing_dev(root, devid, dev_uuid);
  2645. if (!device)
  2646. return -ENOMEM;
  2647. }
  2648. }
  2649. if (device->fs_devices != root->fs_info->fs_devices) {
  2650. BUG_ON(device->writeable);
  2651. if (device->generation !=
  2652. btrfs_device_generation(leaf, dev_item))
  2653. return -EINVAL;
  2654. }
  2655. fill_device_from_item(leaf, dev_item, device);
  2656. device->dev_root = root->fs_info->dev_root;
  2657. device->in_fs_metadata = 1;
  2658. if (device->writeable)
  2659. device->fs_devices->total_rw_bytes += device->total_bytes;
  2660. ret = 0;
  2661. return ret;
  2662. }
  2663. int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
  2664. {
  2665. struct btrfs_dev_item *dev_item;
  2666. dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
  2667. dev_item);
  2668. return read_one_dev(root, buf, dev_item);
  2669. }
  2670. int btrfs_read_sys_array(struct btrfs_root *root)
  2671. {
  2672. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  2673. struct extent_buffer *sb;
  2674. struct btrfs_disk_key *disk_key;
  2675. struct btrfs_chunk *chunk;
  2676. u8 *ptr;
  2677. unsigned long sb_ptr;
  2678. int ret = 0;
  2679. u32 num_stripes;
  2680. u32 array_size;
  2681. u32 len = 0;
  2682. u32 cur;
  2683. struct btrfs_key key;
  2684. sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
  2685. BTRFS_SUPER_INFO_SIZE);
  2686. if (!sb)
  2687. return -ENOMEM;
  2688. btrfs_set_buffer_uptodate(sb);
  2689. btrfs_set_buffer_lockdep_class(sb, 0);
  2690. write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
  2691. array_size = btrfs_super_sys_array_size(super_copy);
  2692. ptr = super_copy->sys_chunk_array;
  2693. sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
  2694. cur = 0;
  2695. while (cur < array_size) {
  2696. disk_key = (struct btrfs_disk_key *)ptr;
  2697. btrfs_disk_key_to_cpu(&key, disk_key);
  2698. len = sizeof(*disk_key); ptr += len;
  2699. sb_ptr += len;
  2700. cur += len;
  2701. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  2702. chunk = (struct btrfs_chunk *)sb_ptr;
  2703. ret = read_one_chunk(root, &key, sb, chunk);
  2704. if (ret)
  2705. break;
  2706. num_stripes = btrfs_chunk_num_stripes(sb, chunk);
  2707. len = btrfs_chunk_item_size(num_stripes);
  2708. } else {
  2709. ret = -EIO;
  2710. break;
  2711. }
  2712. ptr += len;
  2713. sb_ptr += len;
  2714. cur += len;
  2715. }
  2716. free_extent_buffer(sb);
  2717. return ret;
  2718. }
  2719. int btrfs_read_chunk_tree(struct btrfs_root *root)
  2720. {
  2721. struct btrfs_path *path;
  2722. struct extent_buffer *leaf;
  2723. struct btrfs_key key;
  2724. struct btrfs_key found_key;
  2725. int ret;
  2726. int slot;
  2727. root = root->fs_info->chunk_root;
  2728. path = btrfs_alloc_path();
  2729. if (!path)
  2730. return -ENOMEM;
  2731. /* first we search for all of the device items, and then we
  2732. * read in all of the chunk items. This way we can create chunk
  2733. * mappings that reference all of the devices that are afound
  2734. */
  2735. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  2736. key.offset = 0;
  2737. key.type = 0;
  2738. again:
  2739. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2740. while (1) {
  2741. leaf = path->nodes[0];
  2742. slot = path->slots[0];
  2743. if (slot >= btrfs_header_nritems(leaf)) {
  2744. ret = btrfs_next_leaf(root, path);
  2745. if (ret == 0)
  2746. continue;
  2747. if (ret < 0)
  2748. goto error;
  2749. break;
  2750. }
  2751. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  2752. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  2753. if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
  2754. break;
  2755. if (found_key.type == BTRFS_DEV_ITEM_KEY) {
  2756. struct btrfs_dev_item *dev_item;
  2757. dev_item = btrfs_item_ptr(leaf, slot,
  2758. struct btrfs_dev_item);
  2759. ret = read_one_dev(root, leaf, dev_item);
  2760. if (ret)
  2761. goto error;
  2762. }
  2763. } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
  2764. struct btrfs_chunk *chunk;
  2765. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  2766. ret = read_one_chunk(root, &found_key, leaf, chunk);
  2767. if (ret)
  2768. goto error;
  2769. }
  2770. path->slots[0]++;
  2771. }
  2772. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  2773. key.objectid = 0;
  2774. btrfs_release_path(root, path);
  2775. goto again;
  2776. }
  2777. ret = 0;
  2778. error:
  2779. btrfs_free_path(path);
  2780. return ret;
  2781. }