inode.c 82 KB

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