inode.c 75 KB

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