inode.c 95 KB

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