volumes.c 80 KB

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