inode.c 95 KB

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