inode.c 80 KB

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