volumes.c 80 KB

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