inode.c 94 KB

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