inode.c 97 KB

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