inode.c 78 KB

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