inode.c 98 KB

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