inode.c 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434
  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/bio.h>
  19. #include <linux/buffer_head.h>
  20. #include <linux/fs.h>
  21. #include <linux/pagemap.h>
  22. #include <linux/highmem.h>
  23. #include <linux/time.h>
  24. #include <linux/init.h>
  25. #include <linux/string.h>
  26. #include <linux/smp_lock.h>
  27. #include <linux/backing-dev.h>
  28. #include <linux/mpage.h>
  29. #include <linux/swap.h>
  30. #include <linux/writeback.h>
  31. #include <linux/statfs.h>
  32. #include <linux/compat.h>
  33. #include <linux/bit_spinlock.h>
  34. #include <linux/version.h>
  35. #include <linux/xattr.h>
  36. #include "ctree.h"
  37. #include "disk-io.h"
  38. #include "transaction.h"
  39. #include "btrfs_inode.h"
  40. #include "ioctl.h"
  41. #include "print-tree.h"
  42. #include "volumes.h"
  43. struct btrfs_iget_args {
  44. u64 ino;
  45. struct btrfs_root *root;
  46. };
  47. static struct inode_operations btrfs_dir_inode_operations;
  48. static struct inode_operations btrfs_symlink_inode_operations;
  49. static struct inode_operations btrfs_dir_ro_inode_operations;
  50. static struct inode_operations btrfs_special_inode_operations;
  51. static struct inode_operations btrfs_file_inode_operations;
  52. static struct address_space_operations btrfs_aops;
  53. static struct address_space_operations btrfs_symlink_aops;
  54. static struct file_operations btrfs_dir_file_operations;
  55. static struct extent_io_ops btrfs_extent_io_ops;
  56. static struct kmem_cache *btrfs_inode_cachep;
  57. struct kmem_cache *btrfs_trans_handle_cachep;
  58. struct kmem_cache *btrfs_transaction_cachep;
  59. struct kmem_cache *btrfs_bit_radix_cachep;
  60. struct kmem_cache *btrfs_path_cachep;
  61. #define S_SHIFT 12
  62. static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
  63. [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
  64. [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
  65. [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
  66. [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
  67. [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
  68. [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
  69. [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
  70. };
  71. int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
  72. int for_del)
  73. {
  74. u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
  75. u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
  76. u64 thresh;
  77. int ret = 0;
  78. if (for_del)
  79. thresh = total * 90;
  80. else
  81. thresh = total * 85;
  82. do_div(thresh, 100);
  83. spin_lock(&root->fs_info->delalloc_lock);
  84. if (used + root->fs_info->delalloc_bytes + num_required > thresh)
  85. ret = -ENOSPC;
  86. spin_unlock(&root->fs_info->delalloc_lock);
  87. return ret;
  88. }
  89. static int cow_file_range(struct inode *inode, u64 start, u64 end)
  90. {
  91. struct btrfs_root *root = BTRFS_I(inode)->root;
  92. struct btrfs_trans_handle *trans;
  93. u64 alloc_hint = 0;
  94. u64 num_bytes;
  95. u64 cur_alloc_size;
  96. u64 blocksize = root->sectorsize;
  97. u64 orig_start = start;
  98. u64 orig_num_bytes;
  99. struct btrfs_key ins;
  100. int ret;
  101. trans = btrfs_start_transaction(root, 1);
  102. BUG_ON(!trans);
  103. btrfs_set_trans_block_group(trans, inode);
  104. num_bytes = (end - start + blocksize) & ~(blocksize - 1);
  105. num_bytes = max(blocksize, num_bytes);
  106. ret = btrfs_drop_extents(trans, root, inode,
  107. start, start + num_bytes, start, &alloc_hint);
  108. orig_num_bytes = num_bytes;
  109. if (alloc_hint == EXTENT_MAP_INLINE)
  110. goto out;
  111. BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
  112. while(num_bytes > 0) {
  113. cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
  114. ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
  115. root->sectorsize,
  116. root->root_key.objectid,
  117. trans->transid,
  118. inode->i_ino, start, 0,
  119. alloc_hint, (u64)-1, &ins, 1);
  120. if (ret) {
  121. WARN_ON(1);
  122. goto out;
  123. }
  124. cur_alloc_size = ins.offset;
  125. ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
  126. start, ins.objectid, ins.offset,
  127. ins.offset);
  128. inode->i_blocks += ins.offset >> 9;
  129. btrfs_check_file(root, inode);
  130. if (num_bytes < cur_alloc_size) {
  131. printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
  132. cur_alloc_size);
  133. break;
  134. }
  135. num_bytes -= cur_alloc_size;
  136. alloc_hint = ins.objectid + ins.offset;
  137. start += cur_alloc_size;
  138. }
  139. btrfs_drop_extent_cache(inode, orig_start,
  140. orig_start + orig_num_bytes - 1);
  141. btrfs_add_ordered_inode(inode);
  142. btrfs_update_inode(trans, root, inode);
  143. out:
  144. btrfs_end_transaction(trans, root);
  145. return ret;
  146. }
  147. static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
  148. {
  149. u64 extent_start;
  150. u64 extent_end;
  151. u64 bytenr;
  152. u64 cow_end;
  153. u64 loops = 0;
  154. u64 total_fs_bytes;
  155. struct btrfs_root *root = BTRFS_I(inode)->root;
  156. struct extent_buffer *leaf;
  157. int found_type;
  158. struct btrfs_path *path;
  159. struct btrfs_file_extent_item *item;
  160. int ret;
  161. int err;
  162. struct btrfs_key found_key;
  163. total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
  164. path = btrfs_alloc_path();
  165. BUG_ON(!path);
  166. again:
  167. ret = btrfs_lookup_file_extent(NULL, root, path,
  168. inode->i_ino, start, 0);
  169. if (ret < 0) {
  170. btrfs_free_path(path);
  171. return ret;
  172. }
  173. cow_end = end;
  174. if (ret != 0) {
  175. if (path->slots[0] == 0)
  176. goto not_found;
  177. path->slots[0]--;
  178. }
  179. leaf = path->nodes[0];
  180. item = btrfs_item_ptr(leaf, path->slots[0],
  181. struct btrfs_file_extent_item);
  182. /* are we inside the extent that was found? */
  183. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  184. found_type = btrfs_key_type(&found_key);
  185. if (found_key.objectid != inode->i_ino ||
  186. found_type != BTRFS_EXTENT_DATA_KEY) {
  187. goto not_found;
  188. }
  189. found_type = btrfs_file_extent_type(leaf, item);
  190. extent_start = found_key.offset;
  191. if (found_type == BTRFS_FILE_EXTENT_REG) {
  192. u64 extent_num_bytes;
  193. extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
  194. extent_end = extent_start + extent_num_bytes;
  195. err = 0;
  196. if (loops && start != extent_start)
  197. goto not_found;
  198. if (start < extent_start || start >= extent_end)
  199. goto not_found;
  200. cow_end = min(end, extent_end - 1);
  201. bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
  202. if (bytenr == 0)
  203. goto not_found;
  204. /*
  205. * we may be called by the resizer, make sure we're inside
  206. * the limits of the FS
  207. */
  208. if (bytenr + extent_num_bytes > total_fs_bytes)
  209. goto not_found;
  210. if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
  211. goto not_found;
  212. }
  213. start = extent_end;
  214. } else {
  215. goto not_found;
  216. }
  217. loop:
  218. if (start > end) {
  219. btrfs_free_path(path);
  220. return 0;
  221. }
  222. btrfs_release_path(root, path);
  223. loops++;
  224. goto again;
  225. not_found:
  226. cow_file_range(inode, start, cow_end);
  227. start = cow_end + 1;
  228. goto loop;
  229. }
  230. static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
  231. {
  232. struct btrfs_root *root = BTRFS_I(inode)->root;
  233. int ret;
  234. mutex_lock(&root->fs_info->fs_mutex);
  235. if (btrfs_test_opt(root, NODATACOW) ||
  236. btrfs_test_flag(inode, NODATACOW))
  237. ret = run_delalloc_nocow(inode, start, end);
  238. else
  239. ret = cow_file_range(inode, start, end);
  240. mutex_unlock(&root->fs_info->fs_mutex);
  241. return ret;
  242. }
  243. int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
  244. unsigned long old, unsigned long bits)
  245. {
  246. if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
  247. struct btrfs_root *root = BTRFS_I(inode)->root;
  248. spin_lock(&root->fs_info->delalloc_lock);
  249. BTRFS_I(inode)->delalloc_bytes += end - start + 1;
  250. root->fs_info->delalloc_bytes += end - start + 1;
  251. spin_unlock(&root->fs_info->delalloc_lock);
  252. }
  253. return 0;
  254. }
  255. int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
  256. unsigned long old, unsigned long bits)
  257. {
  258. if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
  259. struct btrfs_root *root = BTRFS_I(inode)->root;
  260. spin_lock(&root->fs_info->delalloc_lock);
  261. if (end - start + 1 > root->fs_info->delalloc_bytes) {
  262. printk("warning: delalloc account %Lu %Lu\n",
  263. end - start + 1, root->fs_info->delalloc_bytes);
  264. root->fs_info->delalloc_bytes = 0;
  265. BTRFS_I(inode)->delalloc_bytes = 0;
  266. } else {
  267. root->fs_info->delalloc_bytes -= end - start + 1;
  268. BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
  269. }
  270. spin_unlock(&root->fs_info->delalloc_lock);
  271. }
  272. return 0;
  273. }
  274. int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
  275. size_t size, struct bio *bio)
  276. {
  277. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  278. struct btrfs_mapping_tree *map_tree;
  279. u64 logical = bio->bi_sector << 9;
  280. u64 length = 0;
  281. u64 map_length;
  282. int ret;
  283. length = bio->bi_size;
  284. map_tree = &root->fs_info->mapping_tree;
  285. map_length = length;
  286. ret = btrfs_map_block(map_tree, READ, logical,
  287. &map_length, NULL, 0);
  288. if (map_length < length + size) {
  289. return 1;
  290. }
  291. return 0;
  292. }
  293. int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
  294. int mirror_num)
  295. {
  296. struct btrfs_root *root = BTRFS_I(inode)->root;
  297. struct btrfs_trans_handle *trans;
  298. int ret = 0;
  299. char *sums = NULL;
  300. ret = btrfs_csum_one_bio(root, bio, &sums);
  301. BUG_ON(ret);
  302. mutex_lock(&root->fs_info->fs_mutex);
  303. trans = btrfs_start_transaction(root, 1);
  304. btrfs_set_trans_block_group(trans, inode);
  305. btrfs_csum_file_blocks(trans, root, inode, bio, sums);
  306. ret = btrfs_end_transaction(trans, root);
  307. BUG_ON(ret);
  308. mutex_unlock(&root->fs_info->fs_mutex);
  309. kfree(sums);
  310. return btrfs_map_bio(root, rw, bio, mirror_num);
  311. }
  312. int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
  313. int mirror_num)
  314. {
  315. struct btrfs_root *root = BTRFS_I(inode)->root;
  316. int ret = 0;
  317. if (!(rw & (1 << BIO_RW))) {
  318. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  319. BUG_ON(ret);
  320. goto mapit;
  321. }
  322. if (btrfs_test_opt(root, NODATASUM) ||
  323. btrfs_test_flag(inode, NODATASUM)) {
  324. goto mapit;
  325. }
  326. return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
  327. inode, rw, bio, mirror_num,
  328. __btrfs_submit_bio_hook);
  329. mapit:
  330. return btrfs_map_bio(root, rw, bio, mirror_num);
  331. }
  332. int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
  333. {
  334. int ret = 0;
  335. struct inode *inode = page->mapping->host;
  336. struct btrfs_root *root = BTRFS_I(inode)->root;
  337. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  338. struct btrfs_csum_item *item;
  339. struct btrfs_path *path = NULL;
  340. u32 csum;
  341. if (btrfs_test_opt(root, NODATASUM) ||
  342. btrfs_test_flag(inode, NODATASUM))
  343. return 0;
  344. mutex_lock(&root->fs_info->fs_mutex);
  345. path = btrfs_alloc_path();
  346. item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
  347. if (IS_ERR(item)) {
  348. ret = PTR_ERR(item);
  349. /* a csum that isn't present is a preallocated region. */
  350. if (ret == -ENOENT || ret == -EFBIG)
  351. ret = 0;
  352. csum = 0;
  353. printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
  354. goto out;
  355. }
  356. read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
  357. BTRFS_CRC32_SIZE);
  358. set_state_private(io_tree, start, csum);
  359. out:
  360. if (path)
  361. btrfs_free_path(path);
  362. mutex_unlock(&root->fs_info->fs_mutex);
  363. return ret;
  364. }
  365. struct io_failure_record {
  366. struct page *page;
  367. u64 start;
  368. u64 len;
  369. u64 logical;
  370. int last_mirror;
  371. };
  372. int btrfs_readpage_io_failed_hook(struct bio *failed_bio,
  373. struct page *page, u64 start, u64 end,
  374. struct extent_state *state)
  375. {
  376. struct io_failure_record *failrec = NULL;
  377. u64 private;
  378. struct extent_map *em;
  379. struct inode *inode = page->mapping->host;
  380. struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
  381. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  382. struct bio *bio;
  383. int num_copies;
  384. int ret;
  385. u64 logical;
  386. ret = get_state_private(failure_tree, start, &private);
  387. if (ret) {
  388. failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
  389. if (!failrec)
  390. return -ENOMEM;
  391. failrec->start = start;
  392. failrec->len = end - start + 1;
  393. failrec->last_mirror = 0;
  394. spin_lock(&em_tree->lock);
  395. em = lookup_extent_mapping(em_tree, start, failrec->len);
  396. if (em->start > start || em->start + em->len < start) {
  397. free_extent_map(em);
  398. em = NULL;
  399. }
  400. spin_unlock(&em_tree->lock);
  401. if (!em || IS_ERR(em)) {
  402. kfree(failrec);
  403. return -EIO;
  404. }
  405. logical = start - em->start;
  406. logical = em->block_start + logical;
  407. failrec->logical = logical;
  408. free_extent_map(em);
  409. set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
  410. EXTENT_DIRTY, GFP_NOFS);
  411. set_state_private(failure_tree, start,
  412. (u64)(unsigned long)failrec);
  413. } else {
  414. failrec = (struct io_failure_record *)(unsigned long)private;
  415. }
  416. num_copies = btrfs_num_copies(
  417. &BTRFS_I(inode)->root->fs_info->mapping_tree,
  418. failrec->logical, failrec->len);
  419. failrec->last_mirror++;
  420. if (!state) {
  421. spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
  422. state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
  423. failrec->start,
  424. EXTENT_LOCKED);
  425. if (state && state->start != failrec->start)
  426. state = NULL;
  427. spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
  428. }
  429. if (!state || failrec->last_mirror > num_copies) {
  430. set_state_private(failure_tree, failrec->start, 0);
  431. clear_extent_bits(failure_tree, failrec->start,
  432. failrec->start + failrec->len - 1,
  433. EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
  434. kfree(failrec);
  435. return -EIO;
  436. }
  437. bio = bio_alloc(GFP_NOFS, 1);
  438. bio->bi_private = state;
  439. bio->bi_end_io = failed_bio->bi_end_io;
  440. bio->bi_sector = failrec->logical >> 9;
  441. bio->bi_bdev = failed_bio->bi_bdev;
  442. bio->bi_size = 0;
  443. bio_add_page(bio, page, failrec->len, start - page_offset(page));
  444. btrfs_submit_bio_hook(inode, READ, bio, failrec->last_mirror);
  445. return 0;
  446. }
  447. int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
  448. struct extent_state *state)
  449. {
  450. size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
  451. struct inode *inode = page->mapping->host;
  452. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  453. char *kaddr;
  454. u64 private = ~(u32)0;
  455. int ret;
  456. struct btrfs_root *root = BTRFS_I(inode)->root;
  457. u32 csum = ~(u32)0;
  458. unsigned long flags;
  459. if (btrfs_test_opt(root, NODATASUM) ||
  460. btrfs_test_flag(inode, NODATASUM))
  461. return 0;
  462. if (state && state->start == start) {
  463. private = state->private;
  464. ret = 0;
  465. } else {
  466. ret = get_state_private(io_tree, start, &private);
  467. }
  468. local_irq_save(flags);
  469. kaddr = kmap_atomic(page, KM_IRQ0);
  470. if (ret) {
  471. goto zeroit;
  472. }
  473. csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
  474. btrfs_csum_final(csum, (char *)&csum);
  475. if (csum != private) {
  476. goto zeroit;
  477. }
  478. kunmap_atomic(kaddr, KM_IRQ0);
  479. local_irq_restore(flags);
  480. /* if the io failure tree for this inode is non-empty,
  481. * check to see if we've recovered from a failed IO
  482. */
  483. private = 0;
  484. if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
  485. (u64)-1, 1, EXTENT_DIRTY)) {
  486. u64 private_failure;
  487. struct io_failure_record *failure;
  488. ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
  489. start, &private_failure);
  490. if (ret == 0) {
  491. failure = (struct io_failure_record *)(unsigned long)
  492. private_failure;
  493. set_state_private(&BTRFS_I(inode)->io_failure_tree,
  494. failure->start, 0);
  495. clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
  496. failure->start,
  497. failure->start + failure->len - 1,
  498. EXTENT_DIRTY | EXTENT_LOCKED,
  499. GFP_NOFS);
  500. kfree(failure);
  501. }
  502. }
  503. return 0;
  504. zeroit:
  505. printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
  506. page->mapping->host->i_ino, (unsigned long long)start, csum,
  507. private);
  508. memset(kaddr + offset, 1, end - start + 1);
  509. flush_dcache_page(page);
  510. kunmap_atomic(kaddr, KM_IRQ0);
  511. local_irq_restore(flags);
  512. if (private == 0)
  513. return 0;
  514. return -EIO;
  515. }
  516. void btrfs_read_locked_inode(struct inode *inode)
  517. {
  518. struct btrfs_path *path;
  519. struct extent_buffer *leaf;
  520. struct btrfs_inode_item *inode_item;
  521. struct btrfs_timespec *tspec;
  522. struct btrfs_root *root = BTRFS_I(inode)->root;
  523. struct btrfs_key location;
  524. u64 alloc_group_block;
  525. u32 rdev;
  526. int ret;
  527. path = btrfs_alloc_path();
  528. BUG_ON(!path);
  529. mutex_lock(&root->fs_info->fs_mutex);
  530. memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
  531. ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
  532. if (ret)
  533. goto make_bad;
  534. leaf = path->nodes[0];
  535. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  536. struct btrfs_inode_item);
  537. inode->i_mode = btrfs_inode_mode(leaf, inode_item);
  538. inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
  539. inode->i_uid = btrfs_inode_uid(leaf, inode_item);
  540. inode->i_gid = btrfs_inode_gid(leaf, inode_item);
  541. inode->i_size = btrfs_inode_size(leaf, inode_item);
  542. tspec = btrfs_inode_atime(inode_item);
  543. inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  544. inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  545. tspec = btrfs_inode_mtime(inode_item);
  546. inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  547. inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  548. tspec = btrfs_inode_ctime(inode_item);
  549. inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  550. inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  551. inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
  552. inode->i_generation = btrfs_inode_generation(leaf, inode_item);
  553. inode->i_rdev = 0;
  554. rdev = btrfs_inode_rdev(leaf, inode_item);
  555. alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
  556. BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
  557. alloc_group_block);
  558. BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
  559. if (!BTRFS_I(inode)->block_group) {
  560. BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
  561. NULL, 0,
  562. BTRFS_BLOCK_GROUP_METADATA, 0);
  563. }
  564. btrfs_free_path(path);
  565. inode_item = NULL;
  566. mutex_unlock(&root->fs_info->fs_mutex);
  567. switch (inode->i_mode & S_IFMT) {
  568. case S_IFREG:
  569. inode->i_mapping->a_ops = &btrfs_aops;
  570. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  571. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  572. inode->i_fop = &btrfs_file_operations;
  573. inode->i_op = &btrfs_file_inode_operations;
  574. break;
  575. case S_IFDIR:
  576. inode->i_fop = &btrfs_dir_file_operations;
  577. if (root == root->fs_info->tree_root)
  578. inode->i_op = &btrfs_dir_ro_inode_operations;
  579. else
  580. inode->i_op = &btrfs_dir_inode_operations;
  581. break;
  582. case S_IFLNK:
  583. inode->i_op = &btrfs_symlink_inode_operations;
  584. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  585. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  586. break;
  587. default:
  588. init_special_inode(inode, inode->i_mode, rdev);
  589. break;
  590. }
  591. return;
  592. make_bad:
  593. btrfs_release_path(root, path);
  594. btrfs_free_path(path);
  595. mutex_unlock(&root->fs_info->fs_mutex);
  596. make_bad_inode(inode);
  597. }
  598. static void fill_inode_item(struct extent_buffer *leaf,
  599. struct btrfs_inode_item *item,
  600. struct inode *inode)
  601. {
  602. btrfs_set_inode_uid(leaf, item, inode->i_uid);
  603. btrfs_set_inode_gid(leaf, item, inode->i_gid);
  604. btrfs_set_inode_size(leaf, item, inode->i_size);
  605. btrfs_set_inode_mode(leaf, item, inode->i_mode);
  606. btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
  607. btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
  608. inode->i_atime.tv_sec);
  609. btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
  610. inode->i_atime.tv_nsec);
  611. btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
  612. inode->i_mtime.tv_sec);
  613. btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
  614. inode->i_mtime.tv_nsec);
  615. btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
  616. inode->i_ctime.tv_sec);
  617. btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
  618. inode->i_ctime.tv_nsec);
  619. btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
  620. btrfs_set_inode_generation(leaf, item, inode->i_generation);
  621. btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
  622. btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
  623. btrfs_set_inode_block_group(leaf, item,
  624. BTRFS_I(inode)->block_group->key.objectid);
  625. }
  626. int btrfs_update_inode(struct btrfs_trans_handle *trans,
  627. struct btrfs_root *root,
  628. struct inode *inode)
  629. {
  630. struct btrfs_inode_item *inode_item;
  631. struct btrfs_path *path;
  632. struct extent_buffer *leaf;
  633. int ret;
  634. path = btrfs_alloc_path();
  635. BUG_ON(!path);
  636. ret = btrfs_lookup_inode(trans, root, path,
  637. &BTRFS_I(inode)->location, 1);
  638. if (ret) {
  639. if (ret > 0)
  640. ret = -ENOENT;
  641. goto failed;
  642. }
  643. leaf = path->nodes[0];
  644. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  645. struct btrfs_inode_item);
  646. fill_inode_item(leaf, inode_item, inode);
  647. btrfs_mark_buffer_dirty(leaf);
  648. btrfs_set_inode_last_trans(trans, inode);
  649. ret = 0;
  650. failed:
  651. btrfs_release_path(root, path);
  652. btrfs_free_path(path);
  653. return ret;
  654. }
  655. static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
  656. struct btrfs_root *root,
  657. struct inode *dir,
  658. struct dentry *dentry)
  659. {
  660. struct btrfs_path *path;
  661. const char *name = dentry->d_name.name;
  662. int name_len = dentry->d_name.len;
  663. int ret = 0;
  664. struct extent_buffer *leaf;
  665. struct btrfs_dir_item *di;
  666. struct btrfs_key key;
  667. path = btrfs_alloc_path();
  668. if (!path) {
  669. ret = -ENOMEM;
  670. goto err;
  671. }
  672. di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  673. name, name_len, -1);
  674. if (IS_ERR(di)) {
  675. ret = PTR_ERR(di);
  676. goto err;
  677. }
  678. if (!di) {
  679. ret = -ENOENT;
  680. goto err;
  681. }
  682. leaf = path->nodes[0];
  683. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  684. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  685. if (ret)
  686. goto err;
  687. btrfs_release_path(root, path);
  688. di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
  689. key.objectid, name, name_len, -1);
  690. if (IS_ERR(di)) {
  691. ret = PTR_ERR(di);
  692. goto err;
  693. }
  694. if (!di) {
  695. ret = -ENOENT;
  696. goto err;
  697. }
  698. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  699. dentry->d_inode->i_ctime = dir->i_ctime;
  700. ret = btrfs_del_inode_ref(trans, root, name, name_len,
  701. dentry->d_inode->i_ino,
  702. dentry->d_parent->d_inode->i_ino);
  703. if (ret) {
  704. printk("failed to delete reference to %.*s, "
  705. "inode %lu parent %lu\n", name_len, name,
  706. dentry->d_inode->i_ino,
  707. dentry->d_parent->d_inode->i_ino);
  708. }
  709. err:
  710. btrfs_free_path(path);
  711. if (!ret) {
  712. dir->i_size -= name_len * 2;
  713. dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  714. btrfs_update_inode(trans, root, dir);
  715. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  716. dentry->d_inode->i_nlink--;
  717. #else
  718. drop_nlink(dentry->d_inode);
  719. #endif
  720. ret = btrfs_update_inode(trans, root, dentry->d_inode);
  721. dir->i_sb->s_dirt = 1;
  722. }
  723. return ret;
  724. }
  725. static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
  726. {
  727. struct btrfs_root *root;
  728. struct btrfs_trans_handle *trans;
  729. struct inode *inode = dentry->d_inode;
  730. int ret;
  731. unsigned long nr = 0;
  732. root = BTRFS_I(dir)->root;
  733. mutex_lock(&root->fs_info->fs_mutex);
  734. ret = btrfs_check_free_space(root, 1, 1);
  735. if (ret)
  736. goto fail;
  737. trans = btrfs_start_transaction(root, 1);
  738. btrfs_set_trans_block_group(trans, dir);
  739. ret = btrfs_unlink_trans(trans, root, dir, dentry);
  740. nr = trans->blocks_used;
  741. if (inode->i_nlink == 0) {
  742. int found;
  743. /* if the inode isn't linked anywhere,
  744. * we don't need to worry about
  745. * data=ordered
  746. */
  747. found = btrfs_del_ordered_inode(inode);
  748. if (found == 1) {
  749. atomic_dec(&inode->i_count);
  750. }
  751. }
  752. btrfs_end_transaction(trans, root);
  753. fail:
  754. mutex_unlock(&root->fs_info->fs_mutex);
  755. btrfs_btree_balance_dirty(root, nr);
  756. btrfs_throttle(root);
  757. return ret;
  758. }
  759. static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
  760. {
  761. struct inode *inode = dentry->d_inode;
  762. int err = 0;
  763. int ret;
  764. struct btrfs_root *root = BTRFS_I(dir)->root;
  765. struct btrfs_trans_handle *trans;
  766. unsigned long nr = 0;
  767. if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
  768. return -ENOTEMPTY;
  769. mutex_lock(&root->fs_info->fs_mutex);
  770. ret = btrfs_check_free_space(root, 1, 1);
  771. if (ret)
  772. goto fail;
  773. trans = btrfs_start_transaction(root, 1);
  774. btrfs_set_trans_block_group(trans, dir);
  775. /* now the directory is empty */
  776. err = btrfs_unlink_trans(trans, root, dir, dentry);
  777. if (!err) {
  778. inode->i_size = 0;
  779. }
  780. nr = trans->blocks_used;
  781. ret = btrfs_end_transaction(trans, root);
  782. fail:
  783. mutex_unlock(&root->fs_info->fs_mutex);
  784. btrfs_btree_balance_dirty(root, nr);
  785. btrfs_throttle(root);
  786. if (ret && !err)
  787. err = ret;
  788. return err;
  789. }
  790. /*
  791. * this can truncate away extent items, csum items and directory items.
  792. * It starts at a high offset and removes keys until it can't find
  793. * any higher than i_size.
  794. *
  795. * csum items that cross the new i_size are truncated to the new size
  796. * as well.
  797. */
  798. static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
  799. struct btrfs_root *root,
  800. struct inode *inode,
  801. u32 min_type)
  802. {
  803. int ret;
  804. struct btrfs_path *path;
  805. struct btrfs_key key;
  806. struct btrfs_key found_key;
  807. u32 found_type;
  808. struct extent_buffer *leaf;
  809. struct btrfs_file_extent_item *fi;
  810. u64 extent_start = 0;
  811. u64 extent_num_bytes = 0;
  812. u64 item_end = 0;
  813. u64 root_gen = 0;
  814. u64 root_owner = 0;
  815. int found_extent;
  816. int del_item;
  817. int pending_del_nr = 0;
  818. int pending_del_slot = 0;
  819. int extent_type = -1;
  820. u64 mask = root->sectorsize - 1;
  821. btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
  822. path = btrfs_alloc_path();
  823. path->reada = -1;
  824. BUG_ON(!path);
  825. /* FIXME, add redo link to tree so we don't leak on crash */
  826. key.objectid = inode->i_ino;
  827. key.offset = (u64)-1;
  828. key.type = (u8)-1;
  829. btrfs_init_path(path);
  830. search_again:
  831. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  832. if (ret < 0) {
  833. goto error;
  834. }
  835. if (ret > 0) {
  836. BUG_ON(path->slots[0] == 0);
  837. path->slots[0]--;
  838. }
  839. while(1) {
  840. fi = NULL;
  841. leaf = path->nodes[0];
  842. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  843. found_type = btrfs_key_type(&found_key);
  844. if (found_key.objectid != inode->i_ino)
  845. break;
  846. if (found_type < min_type)
  847. break;
  848. item_end = found_key.offset;
  849. if (found_type == BTRFS_EXTENT_DATA_KEY) {
  850. fi = btrfs_item_ptr(leaf, path->slots[0],
  851. struct btrfs_file_extent_item);
  852. extent_type = btrfs_file_extent_type(leaf, fi);
  853. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  854. item_end +=
  855. btrfs_file_extent_num_bytes(leaf, fi);
  856. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  857. struct btrfs_item *item = btrfs_item_nr(leaf,
  858. path->slots[0]);
  859. item_end += btrfs_file_extent_inline_len(leaf,
  860. item);
  861. }
  862. item_end--;
  863. }
  864. if (found_type == BTRFS_CSUM_ITEM_KEY) {
  865. ret = btrfs_csum_truncate(trans, root, path,
  866. inode->i_size);
  867. BUG_ON(ret);
  868. }
  869. if (item_end < inode->i_size) {
  870. if (found_type == BTRFS_DIR_ITEM_KEY) {
  871. found_type = BTRFS_INODE_ITEM_KEY;
  872. } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
  873. found_type = BTRFS_CSUM_ITEM_KEY;
  874. } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
  875. found_type = BTRFS_XATTR_ITEM_KEY;
  876. } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
  877. found_type = BTRFS_INODE_REF_KEY;
  878. } else if (found_type) {
  879. found_type--;
  880. } else {
  881. break;
  882. }
  883. btrfs_set_key_type(&key, found_type);
  884. goto next;
  885. }
  886. if (found_key.offset >= inode->i_size)
  887. del_item = 1;
  888. else
  889. del_item = 0;
  890. found_extent = 0;
  891. /* FIXME, shrink the extent if the ref count is only 1 */
  892. if (found_type != BTRFS_EXTENT_DATA_KEY)
  893. goto delete;
  894. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  895. u64 num_dec;
  896. extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
  897. if (!del_item) {
  898. u64 orig_num_bytes =
  899. btrfs_file_extent_num_bytes(leaf, fi);
  900. extent_num_bytes = inode->i_size -
  901. found_key.offset + root->sectorsize - 1;
  902. extent_num_bytes = extent_num_bytes &
  903. ~((u64)root->sectorsize - 1);
  904. btrfs_set_file_extent_num_bytes(leaf, fi,
  905. extent_num_bytes);
  906. num_dec = (orig_num_bytes -
  907. extent_num_bytes);
  908. if (extent_start != 0)
  909. dec_i_blocks(inode, num_dec);
  910. btrfs_mark_buffer_dirty(leaf);
  911. } else {
  912. extent_num_bytes =
  913. btrfs_file_extent_disk_num_bytes(leaf,
  914. fi);
  915. /* FIXME blocksize != 4096 */
  916. num_dec = btrfs_file_extent_num_bytes(leaf, fi);
  917. if (extent_start != 0) {
  918. found_extent = 1;
  919. dec_i_blocks(inode, num_dec);
  920. }
  921. root_gen = btrfs_header_generation(leaf);
  922. root_owner = btrfs_header_owner(leaf);
  923. }
  924. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  925. if (!del_item) {
  926. u32 newsize = inode->i_size - found_key.offset;
  927. dec_i_blocks(inode, item_end + 1 -
  928. found_key.offset - newsize);
  929. newsize =
  930. btrfs_file_extent_calc_inline_size(newsize);
  931. ret = btrfs_truncate_item(trans, root, path,
  932. newsize, 1);
  933. BUG_ON(ret);
  934. } else {
  935. dec_i_blocks(inode, item_end + 1 -
  936. found_key.offset);
  937. }
  938. }
  939. delete:
  940. if (del_item) {
  941. if (!pending_del_nr) {
  942. /* no pending yet, add ourselves */
  943. pending_del_slot = path->slots[0];
  944. pending_del_nr = 1;
  945. } else if (pending_del_nr &&
  946. path->slots[0] + 1 == pending_del_slot) {
  947. /* hop on the pending chunk */
  948. pending_del_nr++;
  949. pending_del_slot = path->slots[0];
  950. } else {
  951. printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
  952. }
  953. } else {
  954. break;
  955. }
  956. if (found_extent) {
  957. ret = btrfs_free_extent(trans, root, extent_start,
  958. extent_num_bytes,
  959. root_owner,
  960. root_gen, inode->i_ino,
  961. found_key.offset, 0);
  962. BUG_ON(ret);
  963. }
  964. next:
  965. if (path->slots[0] == 0) {
  966. if (pending_del_nr)
  967. goto del_pending;
  968. btrfs_release_path(root, path);
  969. goto search_again;
  970. }
  971. path->slots[0]--;
  972. if (pending_del_nr &&
  973. path->slots[0] + 1 != pending_del_slot) {
  974. struct btrfs_key debug;
  975. del_pending:
  976. btrfs_item_key_to_cpu(path->nodes[0], &debug,
  977. pending_del_slot);
  978. ret = btrfs_del_items(trans, root, path,
  979. pending_del_slot,
  980. pending_del_nr);
  981. BUG_ON(ret);
  982. pending_del_nr = 0;
  983. btrfs_release_path(root, path);
  984. goto search_again;
  985. }
  986. }
  987. ret = 0;
  988. error:
  989. if (pending_del_nr) {
  990. ret = btrfs_del_items(trans, root, path, pending_del_slot,
  991. pending_del_nr);
  992. }
  993. btrfs_release_path(root, path);
  994. btrfs_free_path(path);
  995. inode->i_sb->s_dirt = 1;
  996. return ret;
  997. }
  998. static int btrfs_cow_one_page(struct inode *inode, struct page *page,
  999. size_t zero_start)
  1000. {
  1001. char *kaddr;
  1002. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  1003. u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  1004. u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
  1005. int ret = 0;
  1006. WARN_ON(!PageLocked(page));
  1007. set_page_extent_mapped(page);
  1008. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  1009. set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
  1010. page_end, GFP_NOFS);
  1011. if (zero_start != PAGE_CACHE_SIZE) {
  1012. kaddr = kmap(page);
  1013. memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
  1014. flush_dcache_page(page);
  1015. kunmap(page);
  1016. }
  1017. set_page_dirty(page);
  1018. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  1019. return ret;
  1020. }
  1021. /*
  1022. * taken from block_truncate_page, but does cow as it zeros out
  1023. * any bytes left in the last page in the file.
  1024. */
  1025. static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
  1026. {
  1027. struct inode *inode = mapping->host;
  1028. struct btrfs_root *root = BTRFS_I(inode)->root;
  1029. u32 blocksize = root->sectorsize;
  1030. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  1031. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  1032. struct page *page;
  1033. int ret = 0;
  1034. u64 page_start;
  1035. if ((offset & (blocksize - 1)) == 0)
  1036. goto out;
  1037. ret = -ENOMEM;
  1038. page = grab_cache_page(mapping, index);
  1039. if (!page)
  1040. goto out;
  1041. if (!PageUptodate(page)) {
  1042. ret = btrfs_readpage(NULL, page);
  1043. lock_page(page);
  1044. if (!PageUptodate(page)) {
  1045. ret = -EIO;
  1046. goto out;
  1047. }
  1048. }
  1049. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  1050. ret = btrfs_cow_one_page(inode, page, offset);
  1051. unlock_page(page);
  1052. page_cache_release(page);
  1053. out:
  1054. return ret;
  1055. }
  1056. static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
  1057. {
  1058. struct inode *inode = dentry->d_inode;
  1059. int err;
  1060. err = inode_change_ok(inode, attr);
  1061. if (err)
  1062. return err;
  1063. if (S_ISREG(inode->i_mode) &&
  1064. attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
  1065. struct btrfs_trans_handle *trans;
  1066. struct btrfs_root *root = BTRFS_I(inode)->root;
  1067. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  1068. u64 mask = root->sectorsize - 1;
  1069. u64 hole_start = (inode->i_size + mask) & ~mask;
  1070. u64 block_end = (attr->ia_size + mask) & ~mask;
  1071. u64 hole_size;
  1072. u64 alloc_hint = 0;
  1073. if (attr->ia_size <= hole_start)
  1074. goto out;
  1075. mutex_lock(&root->fs_info->fs_mutex);
  1076. err = btrfs_check_free_space(root, 1, 0);
  1077. mutex_unlock(&root->fs_info->fs_mutex);
  1078. if (err)
  1079. goto fail;
  1080. btrfs_truncate_page(inode->i_mapping, inode->i_size);
  1081. lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
  1082. hole_size = block_end - hole_start;
  1083. mutex_lock(&root->fs_info->fs_mutex);
  1084. trans = btrfs_start_transaction(root, 1);
  1085. btrfs_set_trans_block_group(trans, inode);
  1086. err = btrfs_drop_extents(trans, root, inode,
  1087. hole_start, block_end, hole_start,
  1088. &alloc_hint);
  1089. if (alloc_hint != EXTENT_MAP_INLINE) {
  1090. err = btrfs_insert_file_extent(trans, root,
  1091. inode->i_ino,
  1092. hole_start, 0, 0,
  1093. hole_size);
  1094. btrfs_drop_extent_cache(inode, hole_start,
  1095. (u64)-1);
  1096. btrfs_check_file(root, inode);
  1097. }
  1098. btrfs_end_transaction(trans, root);
  1099. mutex_unlock(&root->fs_info->fs_mutex);
  1100. unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
  1101. if (err)
  1102. return err;
  1103. }
  1104. out:
  1105. err = inode_setattr(inode, attr);
  1106. fail:
  1107. return err;
  1108. }
  1109. void btrfs_put_inode(struct inode *inode)
  1110. {
  1111. int ret;
  1112. if (!BTRFS_I(inode)->ordered_trans) {
  1113. return;
  1114. }
  1115. if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
  1116. mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
  1117. return;
  1118. ret = btrfs_del_ordered_inode(inode);
  1119. if (ret == 1) {
  1120. atomic_dec(&inode->i_count);
  1121. }
  1122. }
  1123. void btrfs_delete_inode(struct inode *inode)
  1124. {
  1125. struct btrfs_trans_handle *trans;
  1126. struct btrfs_root *root = BTRFS_I(inode)->root;
  1127. unsigned long nr;
  1128. int ret;
  1129. truncate_inode_pages(&inode->i_data, 0);
  1130. if (is_bad_inode(inode)) {
  1131. goto no_delete;
  1132. }
  1133. inode->i_size = 0;
  1134. mutex_lock(&root->fs_info->fs_mutex);
  1135. trans = btrfs_start_transaction(root, 1);
  1136. btrfs_set_trans_block_group(trans, inode);
  1137. ret = btrfs_truncate_in_trans(trans, root, inode, 0);
  1138. if (ret)
  1139. goto no_delete_lock;
  1140. nr = trans->blocks_used;
  1141. clear_inode(inode);
  1142. btrfs_end_transaction(trans, root);
  1143. mutex_unlock(&root->fs_info->fs_mutex);
  1144. btrfs_btree_balance_dirty(root, nr);
  1145. btrfs_throttle(root);
  1146. return;
  1147. no_delete_lock:
  1148. nr = trans->blocks_used;
  1149. btrfs_end_transaction(trans, root);
  1150. mutex_unlock(&root->fs_info->fs_mutex);
  1151. btrfs_btree_balance_dirty(root, nr);
  1152. btrfs_throttle(root);
  1153. no_delete:
  1154. clear_inode(inode);
  1155. }
  1156. /*
  1157. * this returns the key found in the dir entry in the location pointer.
  1158. * If no dir entries were found, location->objectid is 0.
  1159. */
  1160. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  1161. struct btrfs_key *location)
  1162. {
  1163. const char *name = dentry->d_name.name;
  1164. int namelen = dentry->d_name.len;
  1165. struct btrfs_dir_item *di;
  1166. struct btrfs_path *path;
  1167. struct btrfs_root *root = BTRFS_I(dir)->root;
  1168. int ret = 0;
  1169. if (namelen == 1 && strcmp(name, ".") == 0) {
  1170. location->objectid = dir->i_ino;
  1171. location->type = BTRFS_INODE_ITEM_KEY;
  1172. location->offset = 0;
  1173. return 0;
  1174. }
  1175. path = btrfs_alloc_path();
  1176. BUG_ON(!path);
  1177. if (namelen == 2 && strcmp(name, "..") == 0) {
  1178. struct btrfs_key key;
  1179. struct extent_buffer *leaf;
  1180. u32 nritems;
  1181. int slot;
  1182. key.objectid = dir->i_ino;
  1183. btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
  1184. key.offset = 0;
  1185. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1186. BUG_ON(ret == 0);
  1187. ret = 0;
  1188. leaf = path->nodes[0];
  1189. slot = path->slots[0];
  1190. nritems = btrfs_header_nritems(leaf);
  1191. if (slot >= nritems)
  1192. goto out_err;
  1193. btrfs_item_key_to_cpu(leaf, &key, slot);
  1194. if (key.objectid != dir->i_ino ||
  1195. key.type != BTRFS_INODE_REF_KEY) {
  1196. goto out_err;
  1197. }
  1198. location->objectid = key.offset;
  1199. location->type = BTRFS_INODE_ITEM_KEY;
  1200. location->offset = 0;
  1201. goto out;
  1202. }
  1203. di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
  1204. namelen, 0);
  1205. if (IS_ERR(di))
  1206. ret = PTR_ERR(di);
  1207. if (!di || IS_ERR(di)) {
  1208. goto out_err;
  1209. }
  1210. btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
  1211. out:
  1212. btrfs_free_path(path);
  1213. return ret;
  1214. out_err:
  1215. location->objectid = 0;
  1216. goto out;
  1217. }
  1218. /*
  1219. * when we hit a tree root in a directory, the btrfs part of the inode
  1220. * needs to be changed to reflect the root directory of the tree root. This
  1221. * is kind of like crossing a mount point.
  1222. */
  1223. static int fixup_tree_root_location(struct btrfs_root *root,
  1224. struct btrfs_key *location,
  1225. struct btrfs_root **sub_root,
  1226. struct dentry *dentry)
  1227. {
  1228. struct btrfs_path *path;
  1229. struct btrfs_root_item *ri;
  1230. if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
  1231. return 0;
  1232. if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
  1233. return 0;
  1234. path = btrfs_alloc_path();
  1235. BUG_ON(!path);
  1236. mutex_lock(&root->fs_info->fs_mutex);
  1237. *sub_root = btrfs_read_fs_root(root->fs_info, location,
  1238. dentry->d_name.name,
  1239. dentry->d_name.len);
  1240. if (IS_ERR(*sub_root))
  1241. return PTR_ERR(*sub_root);
  1242. ri = &(*sub_root)->root_item;
  1243. location->objectid = btrfs_root_dirid(ri);
  1244. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  1245. location->offset = 0;
  1246. btrfs_free_path(path);
  1247. mutex_unlock(&root->fs_info->fs_mutex);
  1248. return 0;
  1249. }
  1250. static int btrfs_init_locked_inode(struct inode *inode, void *p)
  1251. {
  1252. struct btrfs_iget_args *args = p;
  1253. inode->i_ino = args->ino;
  1254. BTRFS_I(inode)->root = args->root;
  1255. BTRFS_I(inode)->delalloc_bytes = 0;
  1256. extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
  1257. extent_io_tree_init(&BTRFS_I(inode)->io_tree,
  1258. inode->i_mapping, GFP_NOFS);
  1259. extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
  1260. inode->i_mapping, GFP_NOFS);
  1261. return 0;
  1262. }
  1263. static int btrfs_find_actor(struct inode *inode, void *opaque)
  1264. {
  1265. struct btrfs_iget_args *args = opaque;
  1266. return (args->ino == inode->i_ino &&
  1267. args->root == BTRFS_I(inode)->root);
  1268. }
  1269. struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
  1270. u64 root_objectid)
  1271. {
  1272. struct btrfs_iget_args args;
  1273. args.ino = objectid;
  1274. args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
  1275. if (!args.root)
  1276. return NULL;
  1277. return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
  1278. }
  1279. struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
  1280. struct btrfs_root *root)
  1281. {
  1282. struct inode *inode;
  1283. struct btrfs_iget_args args;
  1284. args.ino = objectid;
  1285. args.root = root;
  1286. inode = iget5_locked(s, objectid, btrfs_find_actor,
  1287. btrfs_init_locked_inode,
  1288. (void *)&args);
  1289. return inode;
  1290. }
  1291. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  1292. struct nameidata *nd)
  1293. {
  1294. struct inode * inode;
  1295. struct btrfs_inode *bi = BTRFS_I(dir);
  1296. struct btrfs_root *root = bi->root;
  1297. struct btrfs_root *sub_root = root;
  1298. struct btrfs_key location;
  1299. int ret;
  1300. if (dentry->d_name.len > BTRFS_NAME_LEN)
  1301. return ERR_PTR(-ENAMETOOLONG);
  1302. mutex_lock(&root->fs_info->fs_mutex);
  1303. ret = btrfs_inode_by_name(dir, dentry, &location);
  1304. mutex_unlock(&root->fs_info->fs_mutex);
  1305. if (ret < 0)
  1306. return ERR_PTR(ret);
  1307. inode = NULL;
  1308. if (location.objectid) {
  1309. ret = fixup_tree_root_location(root, &location, &sub_root,
  1310. dentry);
  1311. if (ret < 0)
  1312. return ERR_PTR(ret);
  1313. if (ret > 0)
  1314. return ERR_PTR(-ENOENT);
  1315. inode = btrfs_iget_locked(dir->i_sb, location.objectid,
  1316. sub_root);
  1317. if (!inode)
  1318. return ERR_PTR(-EACCES);
  1319. if (inode->i_state & I_NEW) {
  1320. /* the inode and parent dir are two different roots */
  1321. if (sub_root != root) {
  1322. igrab(inode);
  1323. sub_root->inode = inode;
  1324. }
  1325. BTRFS_I(inode)->root = sub_root;
  1326. memcpy(&BTRFS_I(inode)->location, &location,
  1327. sizeof(location));
  1328. btrfs_read_locked_inode(inode);
  1329. unlock_new_inode(inode);
  1330. }
  1331. }
  1332. return d_splice_alias(inode, dentry);
  1333. }
  1334. static unsigned char btrfs_filetype_table[] = {
  1335. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  1336. };
  1337. static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
  1338. {
  1339. struct inode *inode = filp->f_dentry->d_inode;
  1340. struct btrfs_root *root = BTRFS_I(inode)->root;
  1341. struct btrfs_item *item;
  1342. struct btrfs_dir_item *di;
  1343. struct btrfs_key key;
  1344. struct btrfs_key found_key;
  1345. struct btrfs_path *path;
  1346. int ret;
  1347. u32 nritems;
  1348. struct extent_buffer *leaf;
  1349. int slot;
  1350. int advance;
  1351. unsigned char d_type;
  1352. int over = 0;
  1353. u32 di_cur;
  1354. u32 di_total;
  1355. u32 di_len;
  1356. int key_type = BTRFS_DIR_INDEX_KEY;
  1357. char tmp_name[32];
  1358. char *name_ptr;
  1359. int name_len;
  1360. /* FIXME, use a real flag for deciding about the key type */
  1361. if (root->fs_info->tree_root == root)
  1362. key_type = BTRFS_DIR_ITEM_KEY;
  1363. /* special case for "." */
  1364. if (filp->f_pos == 0) {
  1365. over = filldir(dirent, ".", 1,
  1366. 1, inode->i_ino,
  1367. DT_DIR);
  1368. if (over)
  1369. return 0;
  1370. filp->f_pos = 1;
  1371. }
  1372. mutex_lock(&root->fs_info->fs_mutex);
  1373. key.objectid = inode->i_ino;
  1374. path = btrfs_alloc_path();
  1375. path->reada = 2;
  1376. /* special case for .., just use the back ref */
  1377. if (filp->f_pos == 1) {
  1378. btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
  1379. key.offset = 0;
  1380. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1381. BUG_ON(ret == 0);
  1382. leaf = path->nodes[0];
  1383. slot = path->slots[0];
  1384. nritems = btrfs_header_nritems(leaf);
  1385. if (slot >= nritems) {
  1386. btrfs_release_path(root, path);
  1387. goto read_dir_items;
  1388. }
  1389. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  1390. btrfs_release_path(root, path);
  1391. if (found_key.objectid != key.objectid ||
  1392. found_key.type != BTRFS_INODE_REF_KEY)
  1393. goto read_dir_items;
  1394. over = filldir(dirent, "..", 2,
  1395. 2, found_key.offset, DT_DIR);
  1396. if (over)
  1397. goto nopos;
  1398. filp->f_pos = 2;
  1399. }
  1400. read_dir_items:
  1401. btrfs_set_key_type(&key, key_type);
  1402. key.offset = filp->f_pos;
  1403. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1404. if (ret < 0)
  1405. goto err;
  1406. advance = 0;
  1407. while(1) {
  1408. leaf = path->nodes[0];
  1409. nritems = btrfs_header_nritems(leaf);
  1410. slot = path->slots[0];
  1411. if (advance || slot >= nritems) {
  1412. if (slot >= nritems -1) {
  1413. ret = btrfs_next_leaf(root, path);
  1414. if (ret)
  1415. break;
  1416. leaf = path->nodes[0];
  1417. nritems = btrfs_header_nritems(leaf);
  1418. slot = path->slots[0];
  1419. } else {
  1420. slot++;
  1421. path->slots[0]++;
  1422. }
  1423. }
  1424. advance = 1;
  1425. item = btrfs_item_nr(leaf, slot);
  1426. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  1427. if (found_key.objectid != key.objectid)
  1428. break;
  1429. if (btrfs_key_type(&found_key) != key_type)
  1430. break;
  1431. if (found_key.offset < filp->f_pos)
  1432. continue;
  1433. filp->f_pos = found_key.offset;
  1434. advance = 1;
  1435. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  1436. di_cur = 0;
  1437. di_total = btrfs_item_size(leaf, item);
  1438. while(di_cur < di_total) {
  1439. struct btrfs_key location;
  1440. name_len = btrfs_dir_name_len(leaf, di);
  1441. if (name_len < 32) {
  1442. name_ptr = tmp_name;
  1443. } else {
  1444. name_ptr = kmalloc(name_len, GFP_NOFS);
  1445. BUG_ON(!name_ptr);
  1446. }
  1447. read_extent_buffer(leaf, name_ptr,
  1448. (unsigned long)(di + 1), name_len);
  1449. d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
  1450. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  1451. over = filldir(dirent, name_ptr, name_len,
  1452. found_key.offset,
  1453. location.objectid,
  1454. d_type);
  1455. if (name_ptr != tmp_name)
  1456. kfree(name_ptr);
  1457. if (over)
  1458. goto nopos;
  1459. di_len = btrfs_dir_name_len(leaf, di) +
  1460. btrfs_dir_data_len(leaf, di) +sizeof(*di);
  1461. di_cur += di_len;
  1462. di = (struct btrfs_dir_item *)((char *)di + di_len);
  1463. }
  1464. }
  1465. if (key_type == BTRFS_DIR_INDEX_KEY)
  1466. filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
  1467. else
  1468. filp->f_pos++;
  1469. nopos:
  1470. ret = 0;
  1471. err:
  1472. btrfs_release_path(root, path);
  1473. btrfs_free_path(path);
  1474. mutex_unlock(&root->fs_info->fs_mutex);
  1475. return ret;
  1476. }
  1477. int btrfs_write_inode(struct inode *inode, int wait)
  1478. {
  1479. struct btrfs_root *root = BTRFS_I(inode)->root;
  1480. struct btrfs_trans_handle *trans;
  1481. int ret = 0;
  1482. if (wait) {
  1483. mutex_lock(&root->fs_info->fs_mutex);
  1484. trans = btrfs_start_transaction(root, 1);
  1485. btrfs_set_trans_block_group(trans, inode);
  1486. ret = btrfs_commit_transaction(trans, root);
  1487. mutex_unlock(&root->fs_info->fs_mutex);
  1488. }
  1489. return ret;
  1490. }
  1491. /*
  1492. * This is somewhat expensive, updating the tree every time the
  1493. * inode changes. But, it is most likely to find the inode in cache.
  1494. * FIXME, needs more benchmarking...there are no reasons other than performance
  1495. * to keep or drop this code.
  1496. */
  1497. void btrfs_dirty_inode(struct inode *inode)
  1498. {
  1499. struct btrfs_root *root = BTRFS_I(inode)->root;
  1500. struct btrfs_trans_handle *trans;
  1501. mutex_lock(&root->fs_info->fs_mutex);
  1502. trans = btrfs_start_transaction(root, 1);
  1503. btrfs_set_trans_block_group(trans, inode);
  1504. btrfs_update_inode(trans, root, inode);
  1505. btrfs_end_transaction(trans, root);
  1506. mutex_unlock(&root->fs_info->fs_mutex);
  1507. }
  1508. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  1509. struct btrfs_root *root,
  1510. const char *name, int name_len,
  1511. u64 ref_objectid,
  1512. u64 objectid,
  1513. struct btrfs_block_group_cache *group,
  1514. int mode)
  1515. {
  1516. struct inode *inode;
  1517. struct btrfs_inode_item *inode_item;
  1518. struct btrfs_block_group_cache *new_inode_group;
  1519. struct btrfs_key *location;
  1520. struct btrfs_path *path;
  1521. struct btrfs_inode_ref *ref;
  1522. struct btrfs_key key[2];
  1523. u32 sizes[2];
  1524. unsigned long ptr;
  1525. int ret;
  1526. int owner;
  1527. path = btrfs_alloc_path();
  1528. BUG_ON(!path);
  1529. inode = new_inode(root->fs_info->sb);
  1530. if (!inode)
  1531. return ERR_PTR(-ENOMEM);
  1532. extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
  1533. extent_io_tree_init(&BTRFS_I(inode)->io_tree,
  1534. inode->i_mapping, GFP_NOFS);
  1535. extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
  1536. inode->i_mapping, GFP_NOFS);
  1537. BTRFS_I(inode)->delalloc_bytes = 0;
  1538. BTRFS_I(inode)->root = root;
  1539. if (mode & S_IFDIR)
  1540. owner = 0;
  1541. else
  1542. owner = 1;
  1543. new_inode_group = btrfs_find_block_group(root, group, 0,
  1544. BTRFS_BLOCK_GROUP_METADATA, owner);
  1545. if (!new_inode_group) {
  1546. printk("find_block group failed\n");
  1547. new_inode_group = group;
  1548. }
  1549. BTRFS_I(inode)->block_group = new_inode_group;
  1550. BTRFS_I(inode)->flags = 0;
  1551. key[0].objectid = objectid;
  1552. btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
  1553. key[0].offset = 0;
  1554. key[1].objectid = objectid;
  1555. btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
  1556. key[1].offset = ref_objectid;
  1557. sizes[0] = sizeof(struct btrfs_inode_item);
  1558. sizes[1] = name_len + sizeof(*ref);
  1559. ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
  1560. if (ret != 0)
  1561. goto fail;
  1562. if (objectid > root->highest_inode)
  1563. root->highest_inode = objectid;
  1564. inode->i_uid = current->fsuid;
  1565. inode->i_gid = current->fsgid;
  1566. inode->i_mode = mode;
  1567. inode->i_ino = objectid;
  1568. inode->i_blocks = 0;
  1569. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  1570. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1571. struct btrfs_inode_item);
  1572. fill_inode_item(path->nodes[0], inode_item, inode);
  1573. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  1574. struct btrfs_inode_ref);
  1575. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  1576. ptr = (unsigned long)(ref + 1);
  1577. write_extent_buffer(path->nodes[0], name, ptr, name_len);
  1578. btrfs_mark_buffer_dirty(path->nodes[0]);
  1579. btrfs_free_path(path);
  1580. location = &BTRFS_I(inode)->location;
  1581. location->objectid = objectid;
  1582. location->offset = 0;
  1583. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  1584. insert_inode_hash(inode);
  1585. return inode;
  1586. fail:
  1587. btrfs_free_path(path);
  1588. return ERR_PTR(ret);
  1589. }
  1590. static inline u8 btrfs_inode_type(struct inode *inode)
  1591. {
  1592. return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
  1593. }
  1594. static int btrfs_add_link(struct btrfs_trans_handle *trans,
  1595. struct dentry *dentry, struct inode *inode,
  1596. int add_backref)
  1597. {
  1598. int ret;
  1599. struct btrfs_key key;
  1600. struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
  1601. struct inode *parent_inode;
  1602. key.objectid = inode->i_ino;
  1603. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  1604. key.offset = 0;
  1605. ret = btrfs_insert_dir_item(trans, root,
  1606. dentry->d_name.name, dentry->d_name.len,
  1607. dentry->d_parent->d_inode->i_ino,
  1608. &key, btrfs_inode_type(inode));
  1609. if (ret == 0) {
  1610. if (add_backref) {
  1611. ret = btrfs_insert_inode_ref(trans, root,
  1612. dentry->d_name.name,
  1613. dentry->d_name.len,
  1614. inode->i_ino,
  1615. dentry->d_parent->d_inode->i_ino);
  1616. }
  1617. parent_inode = dentry->d_parent->d_inode;
  1618. parent_inode->i_size += dentry->d_name.len * 2;
  1619. parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
  1620. ret = btrfs_update_inode(trans, root,
  1621. dentry->d_parent->d_inode);
  1622. }
  1623. return ret;
  1624. }
  1625. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  1626. struct dentry *dentry, struct inode *inode,
  1627. int backref)
  1628. {
  1629. int err = btrfs_add_link(trans, dentry, inode, backref);
  1630. if (!err) {
  1631. d_instantiate(dentry, inode);
  1632. return 0;
  1633. }
  1634. if (err > 0)
  1635. err = -EEXIST;
  1636. return err;
  1637. }
  1638. static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
  1639. int mode, dev_t rdev)
  1640. {
  1641. struct btrfs_trans_handle *trans;
  1642. struct btrfs_root *root = BTRFS_I(dir)->root;
  1643. struct inode *inode = NULL;
  1644. int err;
  1645. int drop_inode = 0;
  1646. u64 objectid;
  1647. unsigned long nr = 0;
  1648. if (!new_valid_dev(rdev))
  1649. return -EINVAL;
  1650. mutex_lock(&root->fs_info->fs_mutex);
  1651. err = btrfs_check_free_space(root, 1, 0);
  1652. if (err)
  1653. goto fail;
  1654. trans = btrfs_start_transaction(root, 1);
  1655. btrfs_set_trans_block_group(trans, dir);
  1656. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  1657. if (err) {
  1658. err = -ENOSPC;
  1659. goto out_unlock;
  1660. }
  1661. inode = btrfs_new_inode(trans, root, dentry->d_name.name,
  1662. dentry->d_name.len,
  1663. dentry->d_parent->d_inode->i_ino, objectid,
  1664. BTRFS_I(dir)->block_group, mode);
  1665. err = PTR_ERR(inode);
  1666. if (IS_ERR(inode))
  1667. goto out_unlock;
  1668. btrfs_set_trans_block_group(trans, inode);
  1669. err = btrfs_add_nondir(trans, dentry, inode, 0);
  1670. if (err)
  1671. drop_inode = 1;
  1672. else {
  1673. inode->i_op = &btrfs_special_inode_operations;
  1674. init_special_inode(inode, inode->i_mode, rdev);
  1675. btrfs_update_inode(trans, root, inode);
  1676. }
  1677. dir->i_sb->s_dirt = 1;
  1678. btrfs_update_inode_block_group(trans, inode);
  1679. btrfs_update_inode_block_group(trans, dir);
  1680. out_unlock:
  1681. nr = trans->blocks_used;
  1682. btrfs_end_transaction(trans, root);
  1683. fail:
  1684. mutex_unlock(&root->fs_info->fs_mutex);
  1685. if (drop_inode) {
  1686. inode_dec_link_count(inode);
  1687. iput(inode);
  1688. }
  1689. btrfs_btree_balance_dirty(root, nr);
  1690. btrfs_throttle(root);
  1691. return err;
  1692. }
  1693. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  1694. int mode, struct nameidata *nd)
  1695. {
  1696. struct btrfs_trans_handle *trans;
  1697. struct btrfs_root *root = BTRFS_I(dir)->root;
  1698. struct inode *inode = NULL;
  1699. int err;
  1700. int drop_inode = 0;
  1701. unsigned long nr = 0;
  1702. u64 objectid;
  1703. mutex_lock(&root->fs_info->fs_mutex);
  1704. err = btrfs_check_free_space(root, 1, 0);
  1705. if (err)
  1706. goto fail;
  1707. trans = btrfs_start_transaction(root, 1);
  1708. btrfs_set_trans_block_group(trans, dir);
  1709. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  1710. if (err) {
  1711. err = -ENOSPC;
  1712. goto out_unlock;
  1713. }
  1714. inode = btrfs_new_inode(trans, root, dentry->d_name.name,
  1715. dentry->d_name.len,
  1716. dentry->d_parent->d_inode->i_ino,
  1717. objectid, BTRFS_I(dir)->block_group, mode);
  1718. err = PTR_ERR(inode);
  1719. if (IS_ERR(inode))
  1720. goto out_unlock;
  1721. btrfs_set_trans_block_group(trans, inode);
  1722. err = btrfs_add_nondir(trans, dentry, inode, 0);
  1723. if (err)
  1724. drop_inode = 1;
  1725. else {
  1726. inode->i_mapping->a_ops = &btrfs_aops;
  1727. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  1728. inode->i_fop = &btrfs_file_operations;
  1729. inode->i_op = &btrfs_file_inode_operations;
  1730. extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
  1731. extent_io_tree_init(&BTRFS_I(inode)->io_tree,
  1732. inode->i_mapping, GFP_NOFS);
  1733. extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
  1734. inode->i_mapping, GFP_NOFS);
  1735. BTRFS_I(inode)->delalloc_bytes = 0;
  1736. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  1737. }
  1738. dir->i_sb->s_dirt = 1;
  1739. btrfs_update_inode_block_group(trans, inode);
  1740. btrfs_update_inode_block_group(trans, dir);
  1741. out_unlock:
  1742. nr = trans->blocks_used;
  1743. btrfs_end_transaction(trans, root);
  1744. fail:
  1745. mutex_unlock(&root->fs_info->fs_mutex);
  1746. if (drop_inode) {
  1747. inode_dec_link_count(inode);
  1748. iput(inode);
  1749. }
  1750. btrfs_btree_balance_dirty(root, nr);
  1751. btrfs_throttle(root);
  1752. return err;
  1753. }
  1754. static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
  1755. struct dentry *dentry)
  1756. {
  1757. struct btrfs_trans_handle *trans;
  1758. struct btrfs_root *root = BTRFS_I(dir)->root;
  1759. struct inode *inode = old_dentry->d_inode;
  1760. unsigned long nr = 0;
  1761. int err;
  1762. int drop_inode = 0;
  1763. if (inode->i_nlink == 0)
  1764. return -ENOENT;
  1765. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  1766. inode->i_nlink++;
  1767. #else
  1768. inc_nlink(inode);
  1769. #endif
  1770. mutex_lock(&root->fs_info->fs_mutex);
  1771. err = btrfs_check_free_space(root, 1, 0);
  1772. if (err)
  1773. goto fail;
  1774. trans = btrfs_start_transaction(root, 1);
  1775. btrfs_set_trans_block_group(trans, dir);
  1776. atomic_inc(&inode->i_count);
  1777. err = btrfs_add_nondir(trans, dentry, inode, 1);
  1778. if (err)
  1779. drop_inode = 1;
  1780. dir->i_sb->s_dirt = 1;
  1781. btrfs_update_inode_block_group(trans, dir);
  1782. err = btrfs_update_inode(trans, root, inode);
  1783. if (err)
  1784. drop_inode = 1;
  1785. nr = trans->blocks_used;
  1786. btrfs_end_transaction(trans, root);
  1787. fail:
  1788. mutex_unlock(&root->fs_info->fs_mutex);
  1789. if (drop_inode) {
  1790. inode_dec_link_count(inode);
  1791. iput(inode);
  1792. }
  1793. btrfs_btree_balance_dirty(root, nr);
  1794. btrfs_throttle(root);
  1795. return err;
  1796. }
  1797. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1798. {
  1799. struct inode *inode;
  1800. struct btrfs_trans_handle *trans;
  1801. struct btrfs_root *root = BTRFS_I(dir)->root;
  1802. int err = 0;
  1803. int drop_on_err = 0;
  1804. u64 objectid;
  1805. unsigned long nr = 1;
  1806. mutex_lock(&root->fs_info->fs_mutex);
  1807. err = btrfs_check_free_space(root, 1, 0);
  1808. if (err)
  1809. goto out_unlock;
  1810. trans = btrfs_start_transaction(root, 1);
  1811. btrfs_set_trans_block_group(trans, dir);
  1812. if (IS_ERR(trans)) {
  1813. err = PTR_ERR(trans);
  1814. goto out_unlock;
  1815. }
  1816. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  1817. if (err) {
  1818. err = -ENOSPC;
  1819. goto out_unlock;
  1820. }
  1821. inode = btrfs_new_inode(trans, root, dentry->d_name.name,
  1822. dentry->d_name.len,
  1823. dentry->d_parent->d_inode->i_ino, objectid,
  1824. BTRFS_I(dir)->block_group, S_IFDIR | mode);
  1825. if (IS_ERR(inode)) {
  1826. err = PTR_ERR(inode);
  1827. goto out_fail;
  1828. }
  1829. drop_on_err = 1;
  1830. inode->i_op = &btrfs_dir_inode_operations;
  1831. inode->i_fop = &btrfs_dir_file_operations;
  1832. btrfs_set_trans_block_group(trans, inode);
  1833. inode->i_size = 0;
  1834. err = btrfs_update_inode(trans, root, inode);
  1835. if (err)
  1836. goto out_fail;
  1837. err = btrfs_add_link(trans, dentry, inode, 0);
  1838. if (err)
  1839. goto out_fail;
  1840. d_instantiate(dentry, inode);
  1841. drop_on_err = 0;
  1842. dir->i_sb->s_dirt = 1;
  1843. btrfs_update_inode_block_group(trans, inode);
  1844. btrfs_update_inode_block_group(trans, dir);
  1845. out_fail:
  1846. nr = trans->blocks_used;
  1847. btrfs_end_transaction(trans, root);
  1848. out_unlock:
  1849. mutex_unlock(&root->fs_info->fs_mutex);
  1850. if (drop_on_err)
  1851. iput(inode);
  1852. btrfs_btree_balance_dirty(root, nr);
  1853. btrfs_throttle(root);
  1854. return err;
  1855. }
  1856. static int merge_extent_mapping(struct extent_map_tree *em_tree,
  1857. struct extent_map *existing,
  1858. struct extent_map *em)
  1859. {
  1860. u64 start_diff;
  1861. u64 new_end;
  1862. int ret = 0;
  1863. int real_blocks = existing->block_start < EXTENT_MAP_LAST_BYTE;
  1864. if (real_blocks && em->block_start >= EXTENT_MAP_LAST_BYTE)
  1865. goto invalid;
  1866. if (!real_blocks && em->block_start != existing->block_start)
  1867. goto invalid;
  1868. new_end = max(existing->start + existing->len, em->start + em->len);
  1869. if (existing->start >= em->start) {
  1870. if (em->start + em->len < existing->start)
  1871. goto invalid;
  1872. start_diff = existing->start - em->start;
  1873. if (real_blocks && em->block_start + start_diff !=
  1874. existing->block_start)
  1875. goto invalid;
  1876. em->len = new_end - em->start;
  1877. remove_extent_mapping(em_tree, existing);
  1878. /* free for the tree */
  1879. free_extent_map(existing);
  1880. ret = add_extent_mapping(em_tree, em);
  1881. } else if (em->start > existing->start) {
  1882. if (existing->start + existing->len < em->start)
  1883. goto invalid;
  1884. start_diff = em->start - existing->start;
  1885. if (real_blocks && existing->block_start + start_diff !=
  1886. em->block_start)
  1887. goto invalid;
  1888. remove_extent_mapping(em_tree, existing);
  1889. em->block_start = existing->block_start;
  1890. em->start = existing->start;
  1891. em->len = new_end - existing->start;
  1892. free_extent_map(existing);
  1893. ret = add_extent_mapping(em_tree, em);
  1894. } else {
  1895. goto invalid;
  1896. }
  1897. return ret;
  1898. invalid:
  1899. printk("invalid extent map merge [%Lu %Lu %Lu] [%Lu %Lu %Lu]\n",
  1900. existing->start, existing->len, existing->block_start,
  1901. em->start, em->len, em->block_start);
  1902. return -EIO;
  1903. }
  1904. struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
  1905. size_t pg_offset, u64 start, u64 len,
  1906. int create)
  1907. {
  1908. int ret;
  1909. int err = 0;
  1910. u64 bytenr;
  1911. u64 extent_start = 0;
  1912. u64 extent_end = 0;
  1913. u64 objectid = inode->i_ino;
  1914. u32 found_type;
  1915. struct btrfs_path *path;
  1916. struct btrfs_root *root = BTRFS_I(inode)->root;
  1917. struct btrfs_file_extent_item *item;
  1918. struct extent_buffer *leaf;
  1919. struct btrfs_key found_key;
  1920. struct extent_map *em = NULL;
  1921. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  1922. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  1923. struct btrfs_trans_handle *trans = NULL;
  1924. path = btrfs_alloc_path();
  1925. BUG_ON(!path);
  1926. mutex_lock(&root->fs_info->fs_mutex);
  1927. again:
  1928. spin_lock(&em_tree->lock);
  1929. em = lookup_extent_mapping(em_tree, start, len);
  1930. spin_unlock(&em_tree->lock);
  1931. if (em) {
  1932. if (em->start > start || em->start + em->len <= start)
  1933. free_extent_map(em);
  1934. else if (em->block_start == EXTENT_MAP_INLINE && page)
  1935. free_extent_map(em);
  1936. else
  1937. goto out;
  1938. }
  1939. em = alloc_extent_map(GFP_NOFS);
  1940. if (!em) {
  1941. err = -ENOMEM;
  1942. goto out;
  1943. }
  1944. em->start = EXTENT_MAP_HOLE;
  1945. em->len = (u64)-1;
  1946. em->bdev = inode->i_sb->s_bdev;
  1947. ret = btrfs_lookup_file_extent(trans, root, path,
  1948. objectid, start, trans != NULL);
  1949. if (ret < 0) {
  1950. err = ret;
  1951. goto out;
  1952. }
  1953. if (ret != 0) {
  1954. if (path->slots[0] == 0)
  1955. goto not_found;
  1956. path->slots[0]--;
  1957. }
  1958. leaf = path->nodes[0];
  1959. item = btrfs_item_ptr(leaf, path->slots[0],
  1960. struct btrfs_file_extent_item);
  1961. /* are we inside the extent that was found? */
  1962. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1963. found_type = btrfs_key_type(&found_key);
  1964. if (found_key.objectid != objectid ||
  1965. found_type != BTRFS_EXTENT_DATA_KEY) {
  1966. goto not_found;
  1967. }
  1968. found_type = btrfs_file_extent_type(leaf, item);
  1969. extent_start = found_key.offset;
  1970. if (found_type == BTRFS_FILE_EXTENT_REG) {
  1971. extent_end = extent_start +
  1972. btrfs_file_extent_num_bytes(leaf, item);
  1973. err = 0;
  1974. if (start < extent_start || start >= extent_end) {
  1975. em->start = start;
  1976. if (start < extent_start) {
  1977. if (start + len <= extent_start)
  1978. goto not_found;
  1979. em->len = extent_end - extent_start;
  1980. } else {
  1981. em->len = len;
  1982. }
  1983. goto not_found_em;
  1984. }
  1985. bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
  1986. if (bytenr == 0) {
  1987. em->start = extent_start;
  1988. em->len = extent_end - extent_start;
  1989. em->block_start = EXTENT_MAP_HOLE;
  1990. goto insert;
  1991. }
  1992. bytenr += btrfs_file_extent_offset(leaf, item);
  1993. em->block_start = bytenr;
  1994. em->start = extent_start;
  1995. em->len = extent_end - extent_start;
  1996. goto insert;
  1997. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  1998. u64 page_start;
  1999. unsigned long ptr;
  2000. char *map;
  2001. size_t size;
  2002. size_t extent_offset;
  2003. size_t copy_size;
  2004. size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
  2005. path->slots[0]));
  2006. extent_end = (extent_start + size + root->sectorsize - 1) &
  2007. ~((u64)root->sectorsize - 1);
  2008. if (start < extent_start || start >= extent_end) {
  2009. em->start = start;
  2010. if (start < extent_start) {
  2011. if (start + len <= extent_start)
  2012. goto not_found;
  2013. em->len = extent_end - extent_start;
  2014. } else {
  2015. em->len = len;
  2016. }
  2017. goto not_found_em;
  2018. }
  2019. em->block_start = EXTENT_MAP_INLINE;
  2020. if (!page) {
  2021. em->start = extent_start;
  2022. em->len = size;
  2023. goto out;
  2024. }
  2025. page_start = page_offset(page) + pg_offset;
  2026. extent_offset = page_start - extent_start;
  2027. copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
  2028. size - extent_offset);
  2029. em->start = extent_start + extent_offset;
  2030. em->len = (copy_size + root->sectorsize - 1) &
  2031. ~((u64)root->sectorsize - 1);
  2032. map = kmap(page);
  2033. ptr = btrfs_file_extent_inline_start(item) + extent_offset;
  2034. if (create == 0 && !PageUptodate(page)) {
  2035. read_extent_buffer(leaf, map + pg_offset, ptr,
  2036. copy_size);
  2037. flush_dcache_page(page);
  2038. } else if (create && PageUptodate(page)) {
  2039. if (!trans) {
  2040. kunmap(page);
  2041. free_extent_map(em);
  2042. em = NULL;
  2043. btrfs_release_path(root, path);
  2044. trans = btrfs_start_transaction(root, 1);
  2045. goto again;
  2046. }
  2047. write_extent_buffer(leaf, map + pg_offset, ptr,
  2048. copy_size);
  2049. btrfs_mark_buffer_dirty(leaf);
  2050. }
  2051. kunmap(page);
  2052. set_extent_uptodate(io_tree, em->start,
  2053. extent_map_end(em) - 1, GFP_NOFS);
  2054. goto insert;
  2055. } else {
  2056. printk("unkknown found_type %d\n", found_type);
  2057. WARN_ON(1);
  2058. }
  2059. not_found:
  2060. em->start = start;
  2061. em->len = len;
  2062. not_found_em:
  2063. em->block_start = EXTENT_MAP_HOLE;
  2064. insert:
  2065. btrfs_release_path(root, path);
  2066. if (em->start > start || extent_map_end(em) <= start) {
  2067. printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
  2068. err = -EIO;
  2069. goto out;
  2070. }
  2071. err = 0;
  2072. spin_lock(&em_tree->lock);
  2073. ret = add_extent_mapping(em_tree, em);
  2074. /* it is possible that someone inserted the extent into the tree
  2075. * while we had the lock dropped. It is also possible that
  2076. * an overlapping map exists in the tree
  2077. */
  2078. if (ret == -EEXIST) {
  2079. struct extent_map *existing;
  2080. existing = lookup_extent_mapping(em_tree, start, len);
  2081. if (existing && (existing->start > start ||
  2082. existing->start + existing->len <= start)) {
  2083. free_extent_map(existing);
  2084. existing = NULL;
  2085. }
  2086. if (!existing) {
  2087. existing = lookup_extent_mapping(em_tree, em->start,
  2088. em->len);
  2089. if (existing) {
  2090. err = merge_extent_mapping(em_tree, existing,
  2091. em);
  2092. free_extent_map(existing);
  2093. if (err) {
  2094. free_extent_map(em);
  2095. em = NULL;
  2096. }
  2097. } else {
  2098. err = -EIO;
  2099. printk("failing to insert %Lu %Lu\n",
  2100. start, len);
  2101. free_extent_map(em);
  2102. em = NULL;
  2103. }
  2104. } else {
  2105. free_extent_map(em);
  2106. em = existing;
  2107. }
  2108. }
  2109. spin_unlock(&em_tree->lock);
  2110. out:
  2111. btrfs_free_path(path);
  2112. if (trans) {
  2113. ret = btrfs_end_transaction(trans, root);
  2114. if (!err)
  2115. err = ret;
  2116. }
  2117. mutex_unlock(&root->fs_info->fs_mutex);
  2118. if (err) {
  2119. free_extent_map(em);
  2120. WARN_ON(1);
  2121. return ERR_PTR(err);
  2122. }
  2123. return em;
  2124. }
  2125. #if 0 /* waiting for O_DIRECT reads */
  2126. static int btrfs_get_block(struct inode *inode, sector_t iblock,
  2127. struct buffer_head *bh_result, int create)
  2128. {
  2129. struct extent_map *em;
  2130. u64 start = (u64)iblock << inode->i_blkbits;
  2131. struct btrfs_multi_bio *multi = NULL;
  2132. struct btrfs_root *root = BTRFS_I(inode)->root;
  2133. u64 len;
  2134. u64 logical;
  2135. u64 map_length;
  2136. int ret = 0;
  2137. em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
  2138. if (!em || IS_ERR(em))
  2139. goto out;
  2140. if (em->start > start || em->start + em->len <= start) {
  2141. goto out;
  2142. }
  2143. if (em->block_start == EXTENT_MAP_INLINE) {
  2144. ret = -EINVAL;
  2145. goto out;
  2146. }
  2147. len = em->start + em->len - start;
  2148. len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
  2149. if (em->block_start == EXTENT_MAP_HOLE ||
  2150. em->block_start == EXTENT_MAP_DELALLOC) {
  2151. bh_result->b_size = len;
  2152. goto out;
  2153. }
  2154. logical = start - em->start;
  2155. logical = em->block_start + logical;
  2156. map_length = len;
  2157. ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
  2158. logical, &map_length, &multi, 0);
  2159. BUG_ON(ret);
  2160. bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
  2161. bh_result->b_size = min(map_length, len);
  2162. bh_result->b_bdev = multi->stripes[0].dev->bdev;
  2163. set_buffer_mapped(bh_result);
  2164. kfree(multi);
  2165. out:
  2166. free_extent_map(em);
  2167. return ret;
  2168. }
  2169. #endif
  2170. static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
  2171. const struct iovec *iov, loff_t offset,
  2172. unsigned long nr_segs)
  2173. {
  2174. return -EINVAL;
  2175. #if 0
  2176. struct file *file = iocb->ki_filp;
  2177. struct inode *inode = file->f_mapping->host;
  2178. if (rw == WRITE)
  2179. return -EINVAL;
  2180. return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
  2181. offset, nr_segs, btrfs_get_block, NULL);
  2182. #endif
  2183. }
  2184. static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
  2185. {
  2186. return extent_bmap(mapping, iblock, btrfs_get_extent);
  2187. }
  2188. int btrfs_readpage(struct file *file, struct page *page)
  2189. {
  2190. struct extent_io_tree *tree;
  2191. tree = &BTRFS_I(page->mapping->host)->io_tree;
  2192. return extent_read_full_page(tree, page, btrfs_get_extent);
  2193. }
  2194. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  2195. {
  2196. struct extent_io_tree *tree;
  2197. if (current->flags & PF_MEMALLOC) {
  2198. redirty_page_for_writepage(wbc, page);
  2199. unlock_page(page);
  2200. return 0;
  2201. }
  2202. tree = &BTRFS_I(page->mapping->host)->io_tree;
  2203. return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
  2204. }
  2205. static int btrfs_writepages(struct address_space *mapping,
  2206. struct writeback_control *wbc)
  2207. {
  2208. struct extent_io_tree *tree;
  2209. tree = &BTRFS_I(mapping->host)->io_tree;
  2210. return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
  2211. }
  2212. static int
  2213. btrfs_readpages(struct file *file, struct address_space *mapping,
  2214. struct list_head *pages, unsigned nr_pages)
  2215. {
  2216. struct extent_io_tree *tree;
  2217. tree = &BTRFS_I(mapping->host)->io_tree;
  2218. return extent_readpages(tree, mapping, pages, nr_pages,
  2219. btrfs_get_extent);
  2220. }
  2221. static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  2222. {
  2223. struct extent_io_tree *tree;
  2224. struct extent_map_tree *map;
  2225. int ret;
  2226. tree = &BTRFS_I(page->mapping->host)->io_tree;
  2227. map = &BTRFS_I(page->mapping->host)->extent_tree;
  2228. ret = try_release_extent_mapping(map, tree, page, gfp_flags);
  2229. if (ret == 1) {
  2230. invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
  2231. ClearPagePrivate(page);
  2232. set_page_private(page, 0);
  2233. page_cache_release(page);
  2234. }
  2235. return ret;
  2236. }
  2237. static void btrfs_invalidatepage(struct page *page, unsigned long offset)
  2238. {
  2239. struct extent_io_tree *tree;
  2240. tree = &BTRFS_I(page->mapping->host)->io_tree;
  2241. extent_invalidatepage(tree, page, offset);
  2242. btrfs_releasepage(page, GFP_NOFS);
  2243. if (PagePrivate(page)) {
  2244. invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
  2245. ClearPagePrivate(page);
  2246. set_page_private(page, 0);
  2247. page_cache_release(page);
  2248. }
  2249. }
  2250. /*
  2251. * btrfs_page_mkwrite() is not allowed to change the file size as it gets
  2252. * called from a page fault handler when a page is first dirtied. Hence we must
  2253. * be careful to check for EOF conditions here. We set the page up correctly
  2254. * for a written page which means we get ENOSPC checking when writing into
  2255. * holes and correct delalloc and unwritten extent mapping on filesystems that
  2256. * support these features.
  2257. *
  2258. * We are not allowed to take the i_mutex here so we have to play games to
  2259. * protect against truncate races as the page could now be beyond EOF. Because
  2260. * vmtruncate() writes the inode size before removing pages, once we have the
  2261. * page lock we can determine safely if the page is beyond EOF. If it is not
  2262. * beyond EOF, then the page is guaranteed safe against truncation until we
  2263. * unlock the page.
  2264. */
  2265. int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
  2266. {
  2267. struct inode *inode = fdentry(vma->vm_file)->d_inode;
  2268. struct btrfs_root *root = BTRFS_I(inode)->root;
  2269. unsigned long end;
  2270. loff_t size;
  2271. int ret;
  2272. u64 page_start;
  2273. mutex_lock(&root->fs_info->fs_mutex);
  2274. ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
  2275. mutex_unlock(&root->fs_info->fs_mutex);
  2276. if (ret)
  2277. goto out;
  2278. ret = -EINVAL;
  2279. lock_page(page);
  2280. wait_on_page_writeback(page);
  2281. size = i_size_read(inode);
  2282. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  2283. if ((page->mapping != inode->i_mapping) ||
  2284. (page_start > size)) {
  2285. /* page got truncated out from underneath us */
  2286. goto out_unlock;
  2287. }
  2288. /* page is wholly or partially inside EOF */
  2289. if (page_start + PAGE_CACHE_SIZE > size)
  2290. end = size & ~PAGE_CACHE_MASK;
  2291. else
  2292. end = PAGE_CACHE_SIZE;
  2293. ret = btrfs_cow_one_page(inode, page, end);
  2294. out_unlock:
  2295. unlock_page(page);
  2296. out:
  2297. return ret;
  2298. }
  2299. static void btrfs_truncate(struct inode *inode)
  2300. {
  2301. struct btrfs_root *root = BTRFS_I(inode)->root;
  2302. int ret;
  2303. struct btrfs_trans_handle *trans;
  2304. unsigned long nr;
  2305. if (!S_ISREG(inode->i_mode))
  2306. return;
  2307. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  2308. return;
  2309. btrfs_truncate_page(inode->i_mapping, inode->i_size);
  2310. mutex_lock(&root->fs_info->fs_mutex);
  2311. trans = btrfs_start_transaction(root, 1);
  2312. btrfs_set_trans_block_group(trans, inode);
  2313. /* FIXME, add redo link to tree so we don't leak on crash */
  2314. ret = btrfs_truncate_in_trans(trans, root, inode,
  2315. BTRFS_EXTENT_DATA_KEY);
  2316. btrfs_update_inode(trans, root, inode);
  2317. nr = trans->blocks_used;
  2318. ret = btrfs_end_transaction(trans, root);
  2319. BUG_ON(ret);
  2320. mutex_unlock(&root->fs_info->fs_mutex);
  2321. btrfs_btree_balance_dirty(root, nr);
  2322. btrfs_throttle(root);
  2323. }
  2324. static int noinline create_subvol(struct btrfs_root *root, char *name,
  2325. int namelen)
  2326. {
  2327. struct btrfs_trans_handle *trans;
  2328. struct btrfs_key key;
  2329. struct btrfs_root_item root_item;
  2330. struct btrfs_inode_item *inode_item;
  2331. struct extent_buffer *leaf;
  2332. struct btrfs_root *new_root = root;
  2333. struct inode *inode;
  2334. struct inode *dir;
  2335. int ret;
  2336. int err;
  2337. u64 objectid;
  2338. u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
  2339. unsigned long nr = 1;
  2340. mutex_lock(&root->fs_info->fs_mutex);
  2341. ret = btrfs_check_free_space(root, 1, 0);
  2342. if (ret)
  2343. goto fail_commit;
  2344. trans = btrfs_start_transaction(root, 1);
  2345. BUG_ON(!trans);
  2346. ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
  2347. 0, &objectid);
  2348. if (ret)
  2349. goto fail;
  2350. leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
  2351. objectid, trans->transid, 0, 0,
  2352. 0, 0);
  2353. if (IS_ERR(leaf))
  2354. return PTR_ERR(leaf);
  2355. btrfs_set_header_nritems(leaf, 0);
  2356. btrfs_set_header_level(leaf, 0);
  2357. btrfs_set_header_bytenr(leaf, leaf->start);
  2358. btrfs_set_header_generation(leaf, trans->transid);
  2359. btrfs_set_header_owner(leaf, objectid);
  2360. write_extent_buffer(leaf, root->fs_info->fsid,
  2361. (unsigned long)btrfs_header_fsid(leaf),
  2362. BTRFS_FSID_SIZE);
  2363. btrfs_mark_buffer_dirty(leaf);
  2364. inode_item = &root_item.inode;
  2365. memset(inode_item, 0, sizeof(*inode_item));
  2366. inode_item->generation = cpu_to_le64(1);
  2367. inode_item->size = cpu_to_le64(3);
  2368. inode_item->nlink = cpu_to_le32(1);
  2369. inode_item->nblocks = cpu_to_le64(1);
  2370. inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
  2371. btrfs_set_root_bytenr(&root_item, leaf->start);
  2372. btrfs_set_root_level(&root_item, 0);
  2373. btrfs_set_root_refs(&root_item, 1);
  2374. btrfs_set_root_used(&root_item, 0);
  2375. memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
  2376. root_item.drop_level = 0;
  2377. free_extent_buffer(leaf);
  2378. leaf = NULL;
  2379. btrfs_set_root_dirid(&root_item, new_dirid);
  2380. key.objectid = objectid;
  2381. key.offset = 1;
  2382. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  2383. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  2384. &root_item);
  2385. if (ret)
  2386. goto fail;
  2387. /*
  2388. * insert the directory item
  2389. */
  2390. key.offset = (u64)-1;
  2391. dir = root->fs_info->sb->s_root->d_inode;
  2392. ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
  2393. name, namelen, dir->i_ino, &key,
  2394. BTRFS_FT_DIR);
  2395. if (ret)
  2396. goto fail;
  2397. ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
  2398. name, namelen, objectid,
  2399. root->fs_info->sb->s_root->d_inode->i_ino);
  2400. if (ret)
  2401. goto fail;
  2402. ret = btrfs_commit_transaction(trans, root);
  2403. if (ret)
  2404. goto fail_commit;
  2405. new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
  2406. BUG_ON(!new_root);
  2407. trans = btrfs_start_transaction(new_root, 1);
  2408. BUG_ON(!trans);
  2409. inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
  2410. new_dirid,
  2411. BTRFS_I(dir)->block_group, S_IFDIR | 0700);
  2412. if (IS_ERR(inode))
  2413. goto fail;
  2414. inode->i_op = &btrfs_dir_inode_operations;
  2415. inode->i_fop = &btrfs_dir_file_operations;
  2416. new_root->inode = inode;
  2417. ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
  2418. new_dirid);
  2419. inode->i_nlink = 1;
  2420. inode->i_size = 0;
  2421. ret = btrfs_update_inode(trans, new_root, inode);
  2422. if (ret)
  2423. goto fail;
  2424. fail:
  2425. nr = trans->blocks_used;
  2426. err = btrfs_commit_transaction(trans, new_root);
  2427. if (err && !ret)
  2428. ret = err;
  2429. fail_commit:
  2430. mutex_unlock(&root->fs_info->fs_mutex);
  2431. btrfs_btree_balance_dirty(root, nr);
  2432. btrfs_throttle(root);
  2433. return ret;
  2434. }
  2435. static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
  2436. {
  2437. struct btrfs_pending_snapshot *pending_snapshot;
  2438. struct btrfs_trans_handle *trans;
  2439. int ret;
  2440. int err;
  2441. unsigned long nr = 0;
  2442. if (!root->ref_cows)
  2443. return -EINVAL;
  2444. mutex_lock(&root->fs_info->fs_mutex);
  2445. ret = btrfs_check_free_space(root, 1, 0);
  2446. if (ret)
  2447. goto fail_unlock;
  2448. pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
  2449. if (!pending_snapshot) {
  2450. ret = -ENOMEM;
  2451. goto fail_unlock;
  2452. }
  2453. pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
  2454. if (!pending_snapshot->name) {
  2455. ret = -ENOMEM;
  2456. kfree(pending_snapshot);
  2457. goto fail_unlock;
  2458. }
  2459. memcpy(pending_snapshot->name, name, namelen);
  2460. pending_snapshot->name[namelen] = '\0';
  2461. trans = btrfs_start_transaction(root, 1);
  2462. BUG_ON(!trans);
  2463. pending_snapshot->root = root;
  2464. list_add(&pending_snapshot->list,
  2465. &trans->transaction->pending_snapshots);
  2466. ret = btrfs_update_inode(trans, root, root->inode);
  2467. err = btrfs_commit_transaction(trans, root);
  2468. fail_unlock:
  2469. mutex_unlock(&root->fs_info->fs_mutex);
  2470. btrfs_btree_balance_dirty(root, nr);
  2471. btrfs_throttle(root);
  2472. return ret;
  2473. }
  2474. unsigned long btrfs_force_ra(struct address_space *mapping,
  2475. struct file_ra_state *ra, struct file *file,
  2476. pgoff_t offset, pgoff_t last_index)
  2477. {
  2478. pgoff_t req_size;
  2479. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
  2480. req_size = last_index - offset + 1;
  2481. offset = page_cache_readahead(mapping, ra, file, offset, req_size);
  2482. return offset;
  2483. #else
  2484. req_size = min(last_index - offset + 1, (pgoff_t)128);
  2485. page_cache_sync_readahead(mapping, ra, file, offset, req_size);
  2486. return offset + req_size;
  2487. #endif
  2488. }
  2489. int btrfs_defrag_file(struct file *file) {
  2490. struct inode *inode = fdentry(file)->d_inode;
  2491. struct btrfs_root *root = BTRFS_I(inode)->root;
  2492. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2493. struct page *page;
  2494. unsigned long last_index;
  2495. unsigned long ra_index = 0;
  2496. u64 page_start;
  2497. u64 page_end;
  2498. unsigned long i;
  2499. int ret;
  2500. mutex_lock(&root->fs_info->fs_mutex);
  2501. ret = btrfs_check_free_space(root, inode->i_size, 0);
  2502. mutex_unlock(&root->fs_info->fs_mutex);
  2503. if (ret)
  2504. return -ENOSPC;
  2505. mutex_lock(&inode->i_mutex);
  2506. last_index = inode->i_size >> PAGE_CACHE_SHIFT;
  2507. for (i = 0; i <= last_index; i++) {
  2508. if (i == ra_index) {
  2509. ra_index = btrfs_force_ra(inode->i_mapping,
  2510. &file->f_ra,
  2511. file, ra_index, last_index);
  2512. }
  2513. page = grab_cache_page(inode->i_mapping, i);
  2514. if (!page)
  2515. goto out_unlock;
  2516. if (!PageUptodate(page)) {
  2517. btrfs_readpage(NULL, page);
  2518. lock_page(page);
  2519. if (!PageUptodate(page)) {
  2520. unlock_page(page);
  2521. page_cache_release(page);
  2522. goto out_unlock;
  2523. }
  2524. }
  2525. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  2526. page_end = page_start + PAGE_CACHE_SIZE - 1;
  2527. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2528. set_extent_delalloc(io_tree, page_start,
  2529. page_end, GFP_NOFS);
  2530. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2531. set_page_dirty(page);
  2532. unlock_page(page);
  2533. page_cache_release(page);
  2534. balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
  2535. }
  2536. out_unlock:
  2537. mutex_unlock(&inode->i_mutex);
  2538. return 0;
  2539. }
  2540. static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
  2541. {
  2542. u64 new_size;
  2543. u64 old_size;
  2544. struct btrfs_ioctl_vol_args *vol_args;
  2545. struct btrfs_trans_handle *trans;
  2546. char *sizestr;
  2547. int ret = 0;
  2548. int namelen;
  2549. int mod = 0;
  2550. vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
  2551. if (!vol_args)
  2552. return -ENOMEM;
  2553. if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
  2554. ret = -EFAULT;
  2555. goto out;
  2556. }
  2557. namelen = strlen(vol_args->name);
  2558. if (namelen > BTRFS_VOL_NAME_MAX) {
  2559. ret = -EINVAL;
  2560. goto out;
  2561. }
  2562. sizestr = vol_args->name;
  2563. if (!strcmp(sizestr, "max"))
  2564. new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
  2565. else {
  2566. if (sizestr[0] == '-') {
  2567. mod = -1;
  2568. sizestr++;
  2569. } else if (sizestr[0] == '+') {
  2570. mod = 1;
  2571. sizestr++;
  2572. }
  2573. new_size = btrfs_parse_size(sizestr);
  2574. if (new_size == 0) {
  2575. ret = -EINVAL;
  2576. goto out;
  2577. }
  2578. }
  2579. mutex_lock(&root->fs_info->fs_mutex);
  2580. old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
  2581. if (mod < 0) {
  2582. if (new_size > old_size) {
  2583. ret = -EINVAL;
  2584. goto out_unlock;
  2585. }
  2586. new_size = old_size - new_size;
  2587. } else if (mod > 0) {
  2588. new_size = old_size + new_size;
  2589. }
  2590. if (new_size < 256 * 1024 * 1024) {
  2591. ret = -EINVAL;
  2592. goto out_unlock;
  2593. }
  2594. if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
  2595. ret = -EFBIG;
  2596. goto out_unlock;
  2597. }
  2598. do_div(new_size, root->sectorsize);
  2599. new_size *= root->sectorsize;
  2600. printk("new size is %Lu\n", new_size);
  2601. if (new_size > old_size) {
  2602. trans = btrfs_start_transaction(root, 1);
  2603. ret = btrfs_grow_extent_tree(trans, root, new_size);
  2604. btrfs_commit_transaction(trans, root);
  2605. } else {
  2606. ret = btrfs_shrink_extent_tree(root, new_size);
  2607. }
  2608. out_unlock:
  2609. mutex_unlock(&root->fs_info->fs_mutex);
  2610. out:
  2611. kfree(vol_args);
  2612. return ret;
  2613. }
  2614. static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
  2615. void __user *arg)
  2616. {
  2617. struct btrfs_ioctl_vol_args *vol_args;
  2618. struct btrfs_dir_item *di;
  2619. struct btrfs_path *path;
  2620. u64 root_dirid;
  2621. int namelen;
  2622. int ret;
  2623. vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
  2624. if (!vol_args)
  2625. return -ENOMEM;
  2626. if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
  2627. ret = -EFAULT;
  2628. goto out;
  2629. }
  2630. namelen = strlen(vol_args->name);
  2631. if (namelen > BTRFS_VOL_NAME_MAX) {
  2632. ret = -EINVAL;
  2633. goto out;
  2634. }
  2635. if (strchr(vol_args->name, '/')) {
  2636. ret = -EINVAL;
  2637. goto out;
  2638. }
  2639. path = btrfs_alloc_path();
  2640. if (!path) {
  2641. ret = -ENOMEM;
  2642. goto out;
  2643. }
  2644. root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
  2645. mutex_lock(&root->fs_info->fs_mutex);
  2646. di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
  2647. path, root_dirid,
  2648. vol_args->name, namelen, 0);
  2649. mutex_unlock(&root->fs_info->fs_mutex);
  2650. btrfs_free_path(path);
  2651. if (di && !IS_ERR(di)) {
  2652. ret = -EEXIST;
  2653. goto out;
  2654. }
  2655. if (IS_ERR(di)) {
  2656. ret = PTR_ERR(di);
  2657. goto out;
  2658. }
  2659. if (root == root->fs_info->tree_root)
  2660. ret = create_subvol(root, vol_args->name, namelen);
  2661. else
  2662. ret = create_snapshot(root, vol_args->name, namelen);
  2663. out:
  2664. kfree(vol_args);
  2665. return ret;
  2666. }
  2667. static int btrfs_ioctl_defrag(struct file *file)
  2668. {
  2669. struct inode *inode = fdentry(file)->d_inode;
  2670. struct btrfs_root *root = BTRFS_I(inode)->root;
  2671. switch (inode->i_mode & S_IFMT) {
  2672. case S_IFDIR:
  2673. mutex_lock(&root->fs_info->fs_mutex);
  2674. btrfs_defrag_root(root, 0);
  2675. btrfs_defrag_root(root->fs_info->extent_root, 0);
  2676. mutex_unlock(&root->fs_info->fs_mutex);
  2677. break;
  2678. case S_IFREG:
  2679. btrfs_defrag_file(file);
  2680. break;
  2681. }
  2682. return 0;
  2683. }
  2684. long btrfs_ioctl(struct file *file, unsigned int
  2685. cmd, unsigned long arg)
  2686. {
  2687. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  2688. switch (cmd) {
  2689. case BTRFS_IOC_SNAP_CREATE:
  2690. return btrfs_ioctl_snap_create(root, (void __user *)arg);
  2691. case BTRFS_IOC_DEFRAG:
  2692. return btrfs_ioctl_defrag(file);
  2693. case BTRFS_IOC_RESIZE:
  2694. return btrfs_ioctl_resize(root, (void __user *)arg);
  2695. }
  2696. return -ENOTTY;
  2697. }
  2698. /*
  2699. * Called inside transaction, so use GFP_NOFS
  2700. */
  2701. struct inode *btrfs_alloc_inode(struct super_block *sb)
  2702. {
  2703. struct btrfs_inode *ei;
  2704. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  2705. if (!ei)
  2706. return NULL;
  2707. ei->last_trans = 0;
  2708. ei->ordered_trans = 0;
  2709. return &ei->vfs_inode;
  2710. }
  2711. void btrfs_destroy_inode(struct inode *inode)
  2712. {
  2713. WARN_ON(!list_empty(&inode->i_dentry));
  2714. WARN_ON(inode->i_data.nrpages);
  2715. btrfs_drop_extent_cache(inode, 0, (u64)-1);
  2716. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  2717. }
  2718. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
  2719. static void init_once(struct kmem_cache * cachep, void *foo)
  2720. #else
  2721. static void init_once(void * foo, struct kmem_cache * cachep,
  2722. unsigned long flags)
  2723. #endif
  2724. {
  2725. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  2726. inode_init_once(&ei->vfs_inode);
  2727. }
  2728. void btrfs_destroy_cachep(void)
  2729. {
  2730. if (btrfs_inode_cachep)
  2731. kmem_cache_destroy(btrfs_inode_cachep);
  2732. if (btrfs_trans_handle_cachep)
  2733. kmem_cache_destroy(btrfs_trans_handle_cachep);
  2734. if (btrfs_transaction_cachep)
  2735. kmem_cache_destroy(btrfs_transaction_cachep);
  2736. if (btrfs_bit_radix_cachep)
  2737. kmem_cache_destroy(btrfs_bit_radix_cachep);
  2738. if (btrfs_path_cachep)
  2739. kmem_cache_destroy(btrfs_path_cachep);
  2740. }
  2741. struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
  2742. unsigned long extra_flags,
  2743. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
  2744. void (*ctor)(struct kmem_cache *, void *)
  2745. #else
  2746. void (*ctor)(void *, struct kmem_cache *,
  2747. unsigned long)
  2748. #endif
  2749. )
  2750. {
  2751. return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
  2752. SLAB_MEM_SPREAD | extra_flags), ctor
  2753. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
  2754. ,NULL
  2755. #endif
  2756. );
  2757. }
  2758. int btrfs_init_cachep(void)
  2759. {
  2760. btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
  2761. sizeof(struct btrfs_inode),
  2762. 0, init_once);
  2763. if (!btrfs_inode_cachep)
  2764. goto fail;
  2765. btrfs_trans_handle_cachep =
  2766. btrfs_cache_create("btrfs_trans_handle_cache",
  2767. sizeof(struct btrfs_trans_handle),
  2768. 0, NULL);
  2769. if (!btrfs_trans_handle_cachep)
  2770. goto fail;
  2771. btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
  2772. sizeof(struct btrfs_transaction),
  2773. 0, NULL);
  2774. if (!btrfs_transaction_cachep)
  2775. goto fail;
  2776. btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
  2777. sizeof(struct btrfs_path),
  2778. 0, NULL);
  2779. if (!btrfs_path_cachep)
  2780. goto fail;
  2781. btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
  2782. SLAB_DESTROY_BY_RCU, NULL);
  2783. if (!btrfs_bit_radix_cachep)
  2784. goto fail;
  2785. return 0;
  2786. fail:
  2787. btrfs_destroy_cachep();
  2788. return -ENOMEM;
  2789. }
  2790. static int btrfs_getattr(struct vfsmount *mnt,
  2791. struct dentry *dentry, struct kstat *stat)
  2792. {
  2793. struct inode *inode = dentry->d_inode;
  2794. generic_fillattr(inode, stat);
  2795. stat->blksize = PAGE_CACHE_SIZE;
  2796. stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
  2797. return 0;
  2798. }
  2799. static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
  2800. struct inode * new_dir,struct dentry *new_dentry)
  2801. {
  2802. struct btrfs_trans_handle *trans;
  2803. struct btrfs_root *root = BTRFS_I(old_dir)->root;
  2804. struct inode *new_inode = new_dentry->d_inode;
  2805. struct inode *old_inode = old_dentry->d_inode;
  2806. struct timespec ctime = CURRENT_TIME;
  2807. struct btrfs_path *path;
  2808. int ret;
  2809. if (S_ISDIR(old_inode->i_mode) && new_inode &&
  2810. new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
  2811. return -ENOTEMPTY;
  2812. }
  2813. mutex_lock(&root->fs_info->fs_mutex);
  2814. ret = btrfs_check_free_space(root, 1, 0);
  2815. if (ret)
  2816. goto out_unlock;
  2817. trans = btrfs_start_transaction(root, 1);
  2818. btrfs_set_trans_block_group(trans, new_dir);
  2819. path = btrfs_alloc_path();
  2820. if (!path) {
  2821. ret = -ENOMEM;
  2822. goto out_fail;
  2823. }
  2824. old_dentry->d_inode->i_nlink++;
  2825. old_dir->i_ctime = old_dir->i_mtime = ctime;
  2826. new_dir->i_ctime = new_dir->i_mtime = ctime;
  2827. old_inode->i_ctime = ctime;
  2828. ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
  2829. if (ret)
  2830. goto out_fail;
  2831. if (new_inode) {
  2832. new_inode->i_ctime = CURRENT_TIME;
  2833. ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
  2834. if (ret)
  2835. goto out_fail;
  2836. }
  2837. ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
  2838. if (ret)
  2839. goto out_fail;
  2840. out_fail:
  2841. btrfs_free_path(path);
  2842. btrfs_end_transaction(trans, root);
  2843. out_unlock:
  2844. mutex_unlock(&root->fs_info->fs_mutex);
  2845. return ret;
  2846. }
  2847. static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
  2848. const char *symname)
  2849. {
  2850. struct btrfs_trans_handle *trans;
  2851. struct btrfs_root *root = BTRFS_I(dir)->root;
  2852. struct btrfs_path *path;
  2853. struct btrfs_key key;
  2854. struct inode *inode = NULL;
  2855. int err;
  2856. int drop_inode = 0;
  2857. u64 objectid;
  2858. int name_len;
  2859. int datasize;
  2860. unsigned long ptr;
  2861. struct btrfs_file_extent_item *ei;
  2862. struct extent_buffer *leaf;
  2863. unsigned long nr = 0;
  2864. name_len = strlen(symname) + 1;
  2865. if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
  2866. return -ENAMETOOLONG;
  2867. mutex_lock(&root->fs_info->fs_mutex);
  2868. err = btrfs_check_free_space(root, 1, 0);
  2869. if (err)
  2870. goto out_fail;
  2871. trans = btrfs_start_transaction(root, 1);
  2872. btrfs_set_trans_block_group(trans, dir);
  2873. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  2874. if (err) {
  2875. err = -ENOSPC;
  2876. goto out_unlock;
  2877. }
  2878. inode = btrfs_new_inode(trans, root, dentry->d_name.name,
  2879. dentry->d_name.len,
  2880. dentry->d_parent->d_inode->i_ino, objectid,
  2881. BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
  2882. err = PTR_ERR(inode);
  2883. if (IS_ERR(inode))
  2884. goto out_unlock;
  2885. btrfs_set_trans_block_group(trans, inode);
  2886. err = btrfs_add_nondir(trans, dentry, inode, 0);
  2887. if (err)
  2888. drop_inode = 1;
  2889. else {
  2890. inode->i_mapping->a_ops = &btrfs_aops;
  2891. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  2892. inode->i_fop = &btrfs_file_operations;
  2893. inode->i_op = &btrfs_file_inode_operations;
  2894. extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
  2895. extent_io_tree_init(&BTRFS_I(inode)->io_tree,
  2896. inode->i_mapping, GFP_NOFS);
  2897. extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
  2898. inode->i_mapping, GFP_NOFS);
  2899. BTRFS_I(inode)->delalloc_bytes = 0;
  2900. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  2901. }
  2902. dir->i_sb->s_dirt = 1;
  2903. btrfs_update_inode_block_group(trans, inode);
  2904. btrfs_update_inode_block_group(trans, dir);
  2905. if (drop_inode)
  2906. goto out_unlock;
  2907. path = btrfs_alloc_path();
  2908. BUG_ON(!path);
  2909. key.objectid = inode->i_ino;
  2910. key.offset = 0;
  2911. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  2912. datasize = btrfs_file_extent_calc_inline_size(name_len);
  2913. err = btrfs_insert_empty_item(trans, root, path, &key,
  2914. datasize);
  2915. if (err) {
  2916. drop_inode = 1;
  2917. goto out_unlock;
  2918. }
  2919. leaf = path->nodes[0];
  2920. ei = btrfs_item_ptr(leaf, path->slots[0],
  2921. struct btrfs_file_extent_item);
  2922. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  2923. btrfs_set_file_extent_type(leaf, ei,
  2924. BTRFS_FILE_EXTENT_INLINE);
  2925. ptr = btrfs_file_extent_inline_start(ei);
  2926. write_extent_buffer(leaf, symname, ptr, name_len);
  2927. btrfs_mark_buffer_dirty(leaf);
  2928. btrfs_free_path(path);
  2929. inode->i_op = &btrfs_symlink_inode_operations;
  2930. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  2931. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  2932. inode->i_size = name_len - 1;
  2933. err = btrfs_update_inode(trans, root, inode);
  2934. if (err)
  2935. drop_inode = 1;
  2936. out_unlock:
  2937. nr = trans->blocks_used;
  2938. btrfs_end_transaction(trans, root);
  2939. out_fail:
  2940. mutex_unlock(&root->fs_info->fs_mutex);
  2941. if (drop_inode) {
  2942. inode_dec_link_count(inode);
  2943. iput(inode);
  2944. }
  2945. btrfs_btree_balance_dirty(root, nr);
  2946. btrfs_throttle(root);
  2947. return err;
  2948. }
  2949. static int btrfs_permission(struct inode *inode, int mask,
  2950. struct nameidata *nd)
  2951. {
  2952. if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
  2953. return -EACCES;
  2954. return generic_permission(inode, mask, NULL);
  2955. }
  2956. static struct inode_operations btrfs_dir_inode_operations = {
  2957. .lookup = btrfs_lookup,
  2958. .create = btrfs_create,
  2959. .unlink = btrfs_unlink,
  2960. .link = btrfs_link,
  2961. .mkdir = btrfs_mkdir,
  2962. .rmdir = btrfs_rmdir,
  2963. .rename = btrfs_rename,
  2964. .symlink = btrfs_symlink,
  2965. .setattr = btrfs_setattr,
  2966. .mknod = btrfs_mknod,
  2967. .setxattr = generic_setxattr,
  2968. .getxattr = generic_getxattr,
  2969. .listxattr = btrfs_listxattr,
  2970. .removexattr = generic_removexattr,
  2971. .permission = btrfs_permission,
  2972. };
  2973. static struct inode_operations btrfs_dir_ro_inode_operations = {
  2974. .lookup = btrfs_lookup,
  2975. .permission = btrfs_permission,
  2976. };
  2977. static struct file_operations btrfs_dir_file_operations = {
  2978. .llseek = generic_file_llseek,
  2979. .read = generic_read_dir,
  2980. .readdir = btrfs_readdir,
  2981. .unlocked_ioctl = btrfs_ioctl,
  2982. #ifdef CONFIG_COMPAT
  2983. .compat_ioctl = btrfs_ioctl,
  2984. #endif
  2985. };
  2986. static struct extent_io_ops btrfs_extent_io_ops = {
  2987. .fill_delalloc = run_delalloc_range,
  2988. .submit_bio_hook = btrfs_submit_bio_hook,
  2989. .merge_bio_hook = btrfs_merge_bio_hook,
  2990. .readpage_io_hook = btrfs_readpage_io_hook,
  2991. .readpage_end_io_hook = btrfs_readpage_end_io_hook,
  2992. .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
  2993. .set_bit_hook = btrfs_set_bit_hook,
  2994. .clear_bit_hook = btrfs_clear_bit_hook,
  2995. };
  2996. static struct address_space_operations btrfs_aops = {
  2997. .readpage = btrfs_readpage,
  2998. .writepage = btrfs_writepage,
  2999. .writepages = btrfs_writepages,
  3000. .readpages = btrfs_readpages,
  3001. .sync_page = block_sync_page,
  3002. .bmap = btrfs_bmap,
  3003. .direct_IO = btrfs_direct_IO,
  3004. .invalidatepage = btrfs_invalidatepage,
  3005. .releasepage = btrfs_releasepage,
  3006. .set_page_dirty = __set_page_dirty_nobuffers,
  3007. };
  3008. static struct address_space_operations btrfs_symlink_aops = {
  3009. .readpage = btrfs_readpage,
  3010. .writepage = btrfs_writepage,
  3011. .invalidatepage = btrfs_invalidatepage,
  3012. .releasepage = btrfs_releasepage,
  3013. };
  3014. static struct inode_operations btrfs_file_inode_operations = {
  3015. .truncate = btrfs_truncate,
  3016. .getattr = btrfs_getattr,
  3017. .setattr = btrfs_setattr,
  3018. .setxattr = generic_setxattr,
  3019. .getxattr = generic_getxattr,
  3020. .listxattr = btrfs_listxattr,
  3021. .removexattr = generic_removexattr,
  3022. .permission = btrfs_permission,
  3023. };
  3024. static struct inode_operations btrfs_special_inode_operations = {
  3025. .getattr = btrfs_getattr,
  3026. .setattr = btrfs_setattr,
  3027. .permission = btrfs_permission,
  3028. };
  3029. static struct inode_operations btrfs_symlink_inode_operations = {
  3030. .readlink = generic_readlink,
  3031. .follow_link = page_follow_link_light,
  3032. .put_link = page_put_link,
  3033. .permission = btrfs_permission,
  3034. };