inode.c 78 KB

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