inode.c 79 KB

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