volumes.c 80 KB

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