inode.c 94 KB

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