inode.c 102 KB

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