inode.c 86 KB

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