volumes.c 80 KB

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