inode.c 84 KB

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