inode.c 85 KB

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