inode.c 96 KB

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