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