inode.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930
  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_delete_inode(struct inode *inode)
  885. {
  886. struct btrfs_trans_handle *trans;
  887. struct btrfs_root *root = BTRFS_I(inode)->root;
  888. unsigned long nr;
  889. int ret;
  890. truncate_inode_pages(&inode->i_data, 0);
  891. if (is_bad_inode(inode)) {
  892. goto no_delete;
  893. }
  894. inode->i_size = 0;
  895. mutex_lock(&root->fs_info->fs_mutex);
  896. trans = btrfs_start_transaction(root, 1);
  897. btrfs_set_trans_block_group(trans, inode);
  898. ret = btrfs_truncate_in_trans(trans, root, inode);
  899. if (ret)
  900. goto no_delete_lock;
  901. ret = btrfs_delete_xattrs(trans, root, inode);
  902. if (ret)
  903. goto no_delete_lock;
  904. ret = btrfs_free_inode(trans, root, inode);
  905. if (ret)
  906. goto no_delete_lock;
  907. nr = trans->blocks_used;
  908. btrfs_end_transaction(trans, root);
  909. mutex_unlock(&root->fs_info->fs_mutex);
  910. btrfs_btree_balance_dirty(root, nr);
  911. btrfs_throttle(root);
  912. return;
  913. no_delete_lock:
  914. nr = trans->blocks_used;
  915. btrfs_end_transaction(trans, root);
  916. mutex_unlock(&root->fs_info->fs_mutex);
  917. btrfs_btree_balance_dirty(root, nr);
  918. btrfs_throttle(root);
  919. no_delete:
  920. clear_inode(inode);
  921. }
  922. /*
  923. * this returns the key found in the dir entry in the location pointer.
  924. * If no dir entries were found, location->objectid is 0.
  925. */
  926. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  927. struct btrfs_key *location)
  928. {
  929. const char *name = dentry->d_name.name;
  930. int namelen = dentry->d_name.len;
  931. struct btrfs_dir_item *di;
  932. struct btrfs_path *path;
  933. struct btrfs_root *root = BTRFS_I(dir)->root;
  934. int ret = 0;
  935. if (namelen == 1 && strcmp(name, ".") == 0) {
  936. location->objectid = dir->i_ino;
  937. location->type = BTRFS_INODE_ITEM_KEY;
  938. location->offset = 0;
  939. return 0;
  940. }
  941. path = btrfs_alloc_path();
  942. BUG_ON(!path);
  943. if (namelen == 2 && strcmp(name, "..") == 0) {
  944. struct btrfs_key key;
  945. struct extent_buffer *leaf;
  946. u32 nritems;
  947. int slot;
  948. key.objectid = dir->i_ino;
  949. btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
  950. key.offset = 0;
  951. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  952. BUG_ON(ret == 0);
  953. ret = 0;
  954. leaf = path->nodes[0];
  955. slot = path->slots[0];
  956. nritems = btrfs_header_nritems(leaf);
  957. if (slot >= nritems)
  958. goto out_err;
  959. btrfs_item_key_to_cpu(leaf, &key, slot);
  960. if (key.objectid != dir->i_ino ||
  961. key.type != BTRFS_INODE_REF_KEY) {
  962. goto out_err;
  963. }
  964. location->objectid = key.offset;
  965. location->type = BTRFS_INODE_ITEM_KEY;
  966. location->offset = 0;
  967. goto out;
  968. }
  969. di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
  970. namelen, 0);
  971. if (IS_ERR(di))
  972. ret = PTR_ERR(di);
  973. if (!di || IS_ERR(di)) {
  974. goto out_err;
  975. }
  976. btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
  977. out:
  978. btrfs_free_path(path);
  979. return ret;
  980. out_err:
  981. location->objectid = 0;
  982. goto out;
  983. }
  984. /*
  985. * when we hit a tree root in a directory, the btrfs part of the inode
  986. * needs to be changed to reflect the root directory of the tree root. This
  987. * is kind of like crossing a mount point.
  988. */
  989. static int fixup_tree_root_location(struct btrfs_root *root,
  990. struct btrfs_key *location,
  991. struct btrfs_root **sub_root,
  992. struct dentry *dentry)
  993. {
  994. struct btrfs_path *path;
  995. struct btrfs_root_item *ri;
  996. if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
  997. return 0;
  998. if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
  999. return 0;
  1000. path = btrfs_alloc_path();
  1001. BUG_ON(!path);
  1002. mutex_lock(&root->fs_info->fs_mutex);
  1003. *sub_root = btrfs_read_fs_root(root->fs_info, location,
  1004. dentry->d_name.name,
  1005. dentry->d_name.len);
  1006. if (IS_ERR(*sub_root))
  1007. return PTR_ERR(*sub_root);
  1008. ri = &(*sub_root)->root_item;
  1009. location->objectid = btrfs_root_dirid(ri);
  1010. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  1011. location->offset = 0;
  1012. btrfs_free_path(path);
  1013. mutex_unlock(&root->fs_info->fs_mutex);
  1014. return 0;
  1015. }
  1016. static int btrfs_init_locked_inode(struct inode *inode, void *p)
  1017. {
  1018. struct btrfs_iget_args *args = p;
  1019. inode->i_ino = args->ino;
  1020. BTRFS_I(inode)->root = args->root;
  1021. extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
  1022. inode->i_mapping, GFP_NOFS);
  1023. return 0;
  1024. }
  1025. static int btrfs_find_actor(struct inode *inode, void *opaque)
  1026. {
  1027. struct btrfs_iget_args *args = opaque;
  1028. return (args->ino == inode->i_ino &&
  1029. args->root == BTRFS_I(inode)->root);
  1030. }
  1031. struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
  1032. u64 root_objectid)
  1033. {
  1034. struct btrfs_iget_args args;
  1035. args.ino = objectid;
  1036. args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
  1037. if (!args.root)
  1038. return NULL;
  1039. return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
  1040. }
  1041. struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
  1042. struct btrfs_root *root)
  1043. {
  1044. struct inode *inode;
  1045. struct btrfs_iget_args args;
  1046. args.ino = objectid;
  1047. args.root = root;
  1048. inode = iget5_locked(s, objectid, btrfs_find_actor,
  1049. btrfs_init_locked_inode,
  1050. (void *)&args);
  1051. return inode;
  1052. }
  1053. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  1054. struct nameidata *nd)
  1055. {
  1056. struct inode * inode;
  1057. struct btrfs_inode *bi = BTRFS_I(dir);
  1058. struct btrfs_root *root = bi->root;
  1059. struct btrfs_root *sub_root = root;
  1060. struct btrfs_key location;
  1061. int ret;
  1062. if (dentry->d_name.len > BTRFS_NAME_LEN)
  1063. return ERR_PTR(-ENAMETOOLONG);
  1064. mutex_lock(&root->fs_info->fs_mutex);
  1065. ret = btrfs_inode_by_name(dir, dentry, &location);
  1066. mutex_unlock(&root->fs_info->fs_mutex);
  1067. if (ret < 0)
  1068. return ERR_PTR(ret);
  1069. inode = NULL;
  1070. if (location.objectid) {
  1071. ret = fixup_tree_root_location(root, &location, &sub_root,
  1072. dentry);
  1073. if (ret < 0)
  1074. return ERR_PTR(ret);
  1075. if (ret > 0)
  1076. return ERR_PTR(-ENOENT);
  1077. inode = btrfs_iget_locked(dir->i_sb, location.objectid,
  1078. sub_root);
  1079. if (!inode)
  1080. return ERR_PTR(-EACCES);
  1081. if (inode->i_state & I_NEW) {
  1082. /* the inode and parent dir are two different roots */
  1083. if (sub_root != root) {
  1084. igrab(inode);
  1085. sub_root->inode = inode;
  1086. }
  1087. BTRFS_I(inode)->root = sub_root;
  1088. memcpy(&BTRFS_I(inode)->location, &location,
  1089. sizeof(location));
  1090. btrfs_read_locked_inode(inode);
  1091. unlock_new_inode(inode);
  1092. }
  1093. }
  1094. return d_splice_alias(inode, dentry);
  1095. }
  1096. static unsigned char btrfs_filetype_table[] = {
  1097. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  1098. };
  1099. static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
  1100. {
  1101. struct inode *inode = filp->f_dentry->d_inode;
  1102. struct btrfs_root *root = BTRFS_I(inode)->root;
  1103. struct btrfs_item *item;
  1104. struct btrfs_dir_item *di;
  1105. struct btrfs_key key;
  1106. struct btrfs_key found_key;
  1107. struct btrfs_path *path;
  1108. int ret;
  1109. u32 nritems;
  1110. struct extent_buffer *leaf;
  1111. int slot;
  1112. int advance;
  1113. unsigned char d_type;
  1114. int over = 0;
  1115. u32 di_cur;
  1116. u32 di_total;
  1117. u32 di_len;
  1118. int key_type = BTRFS_DIR_INDEX_KEY;
  1119. char tmp_name[32];
  1120. char *name_ptr;
  1121. int name_len;
  1122. /* FIXME, use a real flag for deciding about the key type */
  1123. if (root->fs_info->tree_root == root)
  1124. key_type = BTRFS_DIR_ITEM_KEY;
  1125. /* special case for "." */
  1126. if (filp->f_pos == 0) {
  1127. over = filldir(dirent, ".", 1,
  1128. 1, inode->i_ino,
  1129. DT_DIR);
  1130. if (over)
  1131. return 0;
  1132. filp->f_pos = 1;
  1133. }
  1134. mutex_lock(&root->fs_info->fs_mutex);
  1135. key.objectid = inode->i_ino;
  1136. path = btrfs_alloc_path();
  1137. path->reada = 2;
  1138. /* special case for .., just use the back ref */
  1139. if (filp->f_pos == 1) {
  1140. btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
  1141. key.offset = 0;
  1142. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1143. BUG_ON(ret == 0);
  1144. leaf = path->nodes[0];
  1145. slot = path->slots[0];
  1146. nritems = btrfs_header_nritems(leaf);
  1147. if (slot >= nritems) {
  1148. btrfs_release_path(root, path);
  1149. goto read_dir_items;
  1150. }
  1151. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  1152. btrfs_release_path(root, path);
  1153. if (found_key.objectid != key.objectid ||
  1154. found_key.type != BTRFS_INODE_REF_KEY)
  1155. goto read_dir_items;
  1156. over = filldir(dirent, "..", 2,
  1157. 2, found_key.offset, DT_DIR);
  1158. if (over)
  1159. goto nopos;
  1160. filp->f_pos = 2;
  1161. }
  1162. read_dir_items:
  1163. btrfs_set_key_type(&key, key_type);
  1164. key.offset = filp->f_pos;
  1165. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1166. if (ret < 0)
  1167. goto err;
  1168. advance = 0;
  1169. while(1) {
  1170. leaf = path->nodes[0];
  1171. nritems = btrfs_header_nritems(leaf);
  1172. slot = path->slots[0];
  1173. if (advance || slot >= nritems) {
  1174. if (slot >= nritems -1) {
  1175. ret = btrfs_next_leaf(root, path);
  1176. if (ret)
  1177. break;
  1178. leaf = path->nodes[0];
  1179. nritems = btrfs_header_nritems(leaf);
  1180. slot = path->slots[0];
  1181. } else {
  1182. slot++;
  1183. path->slots[0]++;
  1184. }
  1185. }
  1186. advance = 1;
  1187. item = btrfs_item_nr(leaf, slot);
  1188. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  1189. if (found_key.objectid != key.objectid)
  1190. break;
  1191. if (btrfs_key_type(&found_key) != key_type)
  1192. break;
  1193. if (found_key.offset < filp->f_pos)
  1194. continue;
  1195. filp->f_pos = found_key.offset;
  1196. advance = 1;
  1197. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  1198. di_cur = 0;
  1199. di_total = btrfs_item_size(leaf, item);
  1200. while(di_cur < di_total) {
  1201. struct btrfs_key location;
  1202. name_len = btrfs_dir_name_len(leaf, di);
  1203. if (name_len < 32) {
  1204. name_ptr = tmp_name;
  1205. } else {
  1206. name_ptr = kmalloc(name_len, GFP_NOFS);
  1207. BUG_ON(!name_ptr);
  1208. }
  1209. read_extent_buffer(leaf, name_ptr,
  1210. (unsigned long)(di + 1), name_len);
  1211. d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
  1212. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  1213. over = filldir(dirent, name_ptr, name_len,
  1214. found_key.offset,
  1215. location.objectid,
  1216. d_type);
  1217. if (name_ptr != tmp_name)
  1218. kfree(name_ptr);
  1219. if (over)
  1220. goto nopos;
  1221. di_len = btrfs_dir_name_len(leaf, di) +
  1222. btrfs_dir_data_len(leaf, di) +sizeof(*di);
  1223. di_cur += di_len;
  1224. di = (struct btrfs_dir_item *)((char *)di + di_len);
  1225. }
  1226. }
  1227. filp->f_pos++;
  1228. nopos:
  1229. ret = 0;
  1230. err:
  1231. btrfs_release_path(root, path);
  1232. btrfs_free_path(path);
  1233. mutex_unlock(&root->fs_info->fs_mutex);
  1234. return ret;
  1235. }
  1236. int btrfs_write_inode(struct inode *inode, int wait)
  1237. {
  1238. struct btrfs_root *root = BTRFS_I(inode)->root;
  1239. struct btrfs_trans_handle *trans;
  1240. int ret = 0;
  1241. if (wait) {
  1242. mutex_lock(&root->fs_info->fs_mutex);
  1243. trans = btrfs_start_transaction(root, 1);
  1244. btrfs_set_trans_block_group(trans, inode);
  1245. ret = btrfs_commit_transaction(trans, root);
  1246. mutex_unlock(&root->fs_info->fs_mutex);
  1247. }
  1248. return ret;
  1249. }
  1250. /*
  1251. * This is somewhat expensive, updating the tree every time the
  1252. * inode changes. But, it is most likely to find the inode in cache.
  1253. * FIXME, needs more benchmarking...there are no reasons other than performance
  1254. * to keep or drop this code.
  1255. */
  1256. void btrfs_dirty_inode(struct inode *inode)
  1257. {
  1258. struct btrfs_root *root = BTRFS_I(inode)->root;
  1259. struct btrfs_trans_handle *trans;
  1260. mutex_lock(&root->fs_info->fs_mutex);
  1261. trans = btrfs_start_transaction(root, 1);
  1262. btrfs_set_trans_block_group(trans, inode);
  1263. btrfs_update_inode(trans, root, inode);
  1264. btrfs_end_transaction(trans, root);
  1265. mutex_unlock(&root->fs_info->fs_mutex);
  1266. }
  1267. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  1268. struct btrfs_root *root,
  1269. u64 objectid,
  1270. struct btrfs_block_group_cache *group,
  1271. int mode)
  1272. {
  1273. struct inode *inode;
  1274. struct btrfs_inode_item *inode_item;
  1275. struct btrfs_key *location;
  1276. struct btrfs_path *path;
  1277. int ret;
  1278. int owner;
  1279. path = btrfs_alloc_path();
  1280. BUG_ON(!path);
  1281. inode = new_inode(root->fs_info->sb);
  1282. if (!inode)
  1283. return ERR_PTR(-ENOMEM);
  1284. extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
  1285. inode->i_mapping, GFP_NOFS);
  1286. BTRFS_I(inode)->root = root;
  1287. if (mode & S_IFDIR)
  1288. owner = 0;
  1289. else
  1290. owner = 1;
  1291. group = btrfs_find_block_group(root, group, 0, 0, owner);
  1292. BTRFS_I(inode)->block_group = group;
  1293. BTRFS_I(inode)->flags = 0;
  1294. ret = btrfs_insert_empty_inode(trans, root, path, objectid);
  1295. if (ret)
  1296. goto fail;
  1297. inode->i_uid = current->fsuid;
  1298. inode->i_gid = current->fsgid;
  1299. inode->i_mode = mode;
  1300. inode->i_ino = objectid;
  1301. inode->i_blocks = 0;
  1302. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  1303. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1304. struct btrfs_inode_item);
  1305. fill_inode_item(path->nodes[0], inode_item, inode);
  1306. btrfs_mark_buffer_dirty(path->nodes[0]);
  1307. btrfs_free_path(path);
  1308. location = &BTRFS_I(inode)->location;
  1309. location->objectid = objectid;
  1310. location->offset = 0;
  1311. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  1312. insert_inode_hash(inode);
  1313. return inode;
  1314. fail:
  1315. btrfs_free_path(path);
  1316. return ERR_PTR(ret);
  1317. }
  1318. static inline u8 btrfs_inode_type(struct inode *inode)
  1319. {
  1320. return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
  1321. }
  1322. static int btrfs_add_link(struct btrfs_trans_handle *trans,
  1323. struct dentry *dentry, struct inode *inode)
  1324. {
  1325. int ret;
  1326. struct btrfs_key key;
  1327. struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
  1328. struct inode *parent_inode;
  1329. key.objectid = inode->i_ino;
  1330. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  1331. key.offset = 0;
  1332. ret = btrfs_insert_dir_item(trans, root,
  1333. dentry->d_name.name, dentry->d_name.len,
  1334. dentry->d_parent->d_inode->i_ino,
  1335. &key, btrfs_inode_type(inode));
  1336. if (ret == 0) {
  1337. ret = btrfs_insert_inode_ref(trans, root,
  1338. dentry->d_name.name,
  1339. dentry->d_name.len,
  1340. inode->i_ino,
  1341. dentry->d_parent->d_inode->i_ino);
  1342. parent_inode = dentry->d_parent->d_inode;
  1343. parent_inode->i_size += dentry->d_name.len * 2;
  1344. parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
  1345. ret = btrfs_update_inode(trans, root,
  1346. dentry->d_parent->d_inode);
  1347. }
  1348. return ret;
  1349. }
  1350. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  1351. struct dentry *dentry, struct inode *inode)
  1352. {
  1353. int err = btrfs_add_link(trans, dentry, inode);
  1354. if (!err) {
  1355. d_instantiate(dentry, inode);
  1356. return 0;
  1357. }
  1358. if (err > 0)
  1359. err = -EEXIST;
  1360. return err;
  1361. }
  1362. static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
  1363. int mode, dev_t rdev)
  1364. {
  1365. struct btrfs_trans_handle *trans;
  1366. struct btrfs_root *root = BTRFS_I(dir)->root;
  1367. struct inode *inode = NULL;
  1368. int err;
  1369. int drop_inode = 0;
  1370. u64 objectid;
  1371. unsigned long nr = 0;
  1372. if (!new_valid_dev(rdev))
  1373. return -EINVAL;
  1374. mutex_lock(&root->fs_info->fs_mutex);
  1375. err = btrfs_check_free_space(root, 1, 0);
  1376. if (err)
  1377. goto fail;
  1378. trans = btrfs_start_transaction(root, 1);
  1379. btrfs_set_trans_block_group(trans, dir);
  1380. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  1381. if (err) {
  1382. err = -ENOSPC;
  1383. goto out_unlock;
  1384. }
  1385. inode = btrfs_new_inode(trans, root, objectid,
  1386. BTRFS_I(dir)->block_group, mode);
  1387. err = PTR_ERR(inode);
  1388. if (IS_ERR(inode))
  1389. goto out_unlock;
  1390. btrfs_set_trans_block_group(trans, inode);
  1391. err = btrfs_add_nondir(trans, dentry, inode);
  1392. if (err)
  1393. drop_inode = 1;
  1394. else {
  1395. inode->i_op = &btrfs_special_inode_operations;
  1396. init_special_inode(inode, inode->i_mode, rdev);
  1397. btrfs_update_inode(trans, root, inode);
  1398. }
  1399. dir->i_sb->s_dirt = 1;
  1400. btrfs_update_inode_block_group(trans, inode);
  1401. btrfs_update_inode_block_group(trans, dir);
  1402. out_unlock:
  1403. nr = trans->blocks_used;
  1404. btrfs_end_transaction(trans, root);
  1405. fail:
  1406. mutex_unlock(&root->fs_info->fs_mutex);
  1407. if (drop_inode) {
  1408. inode_dec_link_count(inode);
  1409. iput(inode);
  1410. }
  1411. btrfs_btree_balance_dirty(root, nr);
  1412. btrfs_throttle(root);
  1413. return err;
  1414. }
  1415. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  1416. int mode, struct nameidata *nd)
  1417. {
  1418. struct btrfs_trans_handle *trans;
  1419. struct btrfs_root *root = BTRFS_I(dir)->root;
  1420. struct inode *inode = NULL;
  1421. int err;
  1422. int drop_inode = 0;
  1423. unsigned long nr = 0;
  1424. u64 objectid;
  1425. mutex_lock(&root->fs_info->fs_mutex);
  1426. err = btrfs_check_free_space(root, 1, 0);
  1427. if (err)
  1428. goto fail;
  1429. trans = btrfs_start_transaction(root, 1);
  1430. btrfs_set_trans_block_group(trans, dir);
  1431. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  1432. if (err) {
  1433. err = -ENOSPC;
  1434. goto out_unlock;
  1435. }
  1436. inode = btrfs_new_inode(trans, root, objectid,
  1437. BTRFS_I(dir)->block_group, mode);
  1438. err = PTR_ERR(inode);
  1439. if (IS_ERR(inode))
  1440. goto out_unlock;
  1441. btrfs_set_trans_block_group(trans, inode);
  1442. err = btrfs_add_nondir(trans, dentry, inode);
  1443. if (err)
  1444. drop_inode = 1;
  1445. else {
  1446. inode->i_mapping->a_ops = &btrfs_aops;
  1447. inode->i_fop = &btrfs_file_operations;
  1448. inode->i_op = &btrfs_file_inode_operations;
  1449. extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
  1450. inode->i_mapping, GFP_NOFS);
  1451. BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
  1452. }
  1453. dir->i_sb->s_dirt = 1;
  1454. btrfs_update_inode_block_group(trans, inode);
  1455. btrfs_update_inode_block_group(trans, dir);
  1456. out_unlock:
  1457. nr = trans->blocks_used;
  1458. btrfs_end_transaction(trans, root);
  1459. fail:
  1460. mutex_unlock(&root->fs_info->fs_mutex);
  1461. if (drop_inode) {
  1462. inode_dec_link_count(inode);
  1463. iput(inode);
  1464. }
  1465. btrfs_btree_balance_dirty(root, nr);
  1466. btrfs_throttle(root);
  1467. return err;
  1468. }
  1469. static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
  1470. struct dentry *dentry)
  1471. {
  1472. struct btrfs_trans_handle *trans;
  1473. struct btrfs_root *root = BTRFS_I(dir)->root;
  1474. struct inode *inode = old_dentry->d_inode;
  1475. unsigned long nr = 0;
  1476. int err;
  1477. int drop_inode = 0;
  1478. if (inode->i_nlink == 0)
  1479. return -ENOENT;
  1480. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  1481. inode->i_nlink++;
  1482. #else
  1483. inc_nlink(inode);
  1484. #endif
  1485. mutex_lock(&root->fs_info->fs_mutex);
  1486. err = btrfs_check_free_space(root, 1, 0);
  1487. if (err)
  1488. goto fail;
  1489. trans = btrfs_start_transaction(root, 1);
  1490. btrfs_set_trans_block_group(trans, dir);
  1491. atomic_inc(&inode->i_count);
  1492. err = btrfs_add_nondir(trans, dentry, inode);
  1493. if (err)
  1494. drop_inode = 1;
  1495. dir->i_sb->s_dirt = 1;
  1496. btrfs_update_inode_block_group(trans, dir);
  1497. err = btrfs_update_inode(trans, root, inode);
  1498. if (err)
  1499. drop_inode = 1;
  1500. nr = trans->blocks_used;
  1501. btrfs_end_transaction(trans, root);
  1502. fail:
  1503. mutex_unlock(&root->fs_info->fs_mutex);
  1504. if (drop_inode) {
  1505. inode_dec_link_count(inode);
  1506. iput(inode);
  1507. }
  1508. btrfs_btree_balance_dirty(root, nr);
  1509. btrfs_throttle(root);
  1510. return err;
  1511. }
  1512. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1513. {
  1514. struct inode *inode;
  1515. struct btrfs_trans_handle *trans;
  1516. struct btrfs_root *root = BTRFS_I(dir)->root;
  1517. int err = 0;
  1518. int drop_on_err = 0;
  1519. u64 objectid;
  1520. unsigned long nr = 1;
  1521. mutex_lock(&root->fs_info->fs_mutex);
  1522. err = btrfs_check_free_space(root, 1, 0);
  1523. if (err)
  1524. goto out_unlock;
  1525. trans = btrfs_start_transaction(root, 1);
  1526. btrfs_set_trans_block_group(trans, dir);
  1527. if (IS_ERR(trans)) {
  1528. err = PTR_ERR(trans);
  1529. goto out_unlock;
  1530. }
  1531. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  1532. if (err) {
  1533. err = -ENOSPC;
  1534. goto out_unlock;
  1535. }
  1536. inode = btrfs_new_inode(trans, root, objectid,
  1537. BTRFS_I(dir)->block_group, S_IFDIR | mode);
  1538. if (IS_ERR(inode)) {
  1539. err = PTR_ERR(inode);
  1540. goto out_fail;
  1541. }
  1542. drop_on_err = 1;
  1543. inode->i_op = &btrfs_dir_inode_operations;
  1544. inode->i_fop = &btrfs_dir_file_operations;
  1545. btrfs_set_trans_block_group(trans, inode);
  1546. inode->i_size = 0;
  1547. err = btrfs_update_inode(trans, root, inode);
  1548. if (err)
  1549. goto out_fail;
  1550. err = btrfs_add_link(trans, dentry, inode);
  1551. if (err)
  1552. goto out_fail;
  1553. d_instantiate(dentry, inode);
  1554. drop_on_err = 0;
  1555. dir->i_sb->s_dirt = 1;
  1556. btrfs_update_inode_block_group(trans, inode);
  1557. btrfs_update_inode_block_group(trans, dir);
  1558. out_fail:
  1559. nr = trans->blocks_used;
  1560. btrfs_end_transaction(trans, root);
  1561. out_unlock:
  1562. mutex_unlock(&root->fs_info->fs_mutex);
  1563. if (drop_on_err)
  1564. iput(inode);
  1565. btrfs_btree_balance_dirty(root, nr);
  1566. btrfs_throttle(root);
  1567. return err;
  1568. }
  1569. struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
  1570. size_t page_offset, u64 start, u64 end,
  1571. int create)
  1572. {
  1573. int ret;
  1574. int err = 0;
  1575. u64 bytenr;
  1576. u64 extent_start = 0;
  1577. u64 extent_end = 0;
  1578. u64 objectid = inode->i_ino;
  1579. u32 found_type;
  1580. int failed_insert = 0;
  1581. struct btrfs_path *path;
  1582. struct btrfs_root *root = BTRFS_I(inode)->root;
  1583. struct btrfs_file_extent_item *item;
  1584. struct extent_buffer *leaf;
  1585. struct btrfs_key found_key;
  1586. struct extent_map *em = NULL;
  1587. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  1588. struct btrfs_trans_handle *trans = NULL;
  1589. path = btrfs_alloc_path();
  1590. BUG_ON(!path);
  1591. mutex_lock(&root->fs_info->fs_mutex);
  1592. again:
  1593. em = lookup_extent_mapping(em_tree, start, end);
  1594. if (em) {
  1595. if (em->start > start) {
  1596. printk("get_extent start %Lu em start %Lu\n",
  1597. start, em->start);
  1598. WARN_ON(1);
  1599. }
  1600. goto out;
  1601. }
  1602. if (!em) {
  1603. em = alloc_extent_map(GFP_NOFS);
  1604. if (!em) {
  1605. err = -ENOMEM;
  1606. goto out;
  1607. }
  1608. em->start = EXTENT_MAP_HOLE;
  1609. em->end = EXTENT_MAP_HOLE;
  1610. }
  1611. em->bdev = inode->i_sb->s_bdev;
  1612. ret = btrfs_lookup_file_extent(trans, root, path,
  1613. objectid, start, trans != NULL);
  1614. if (ret < 0) {
  1615. err = ret;
  1616. goto out;
  1617. }
  1618. if (ret != 0) {
  1619. if (path->slots[0] == 0)
  1620. goto not_found;
  1621. path->slots[0]--;
  1622. }
  1623. leaf = path->nodes[0];
  1624. item = btrfs_item_ptr(leaf, path->slots[0],
  1625. struct btrfs_file_extent_item);
  1626. /* are we inside the extent that was found? */
  1627. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1628. found_type = btrfs_key_type(&found_key);
  1629. if (found_key.objectid != objectid ||
  1630. found_type != BTRFS_EXTENT_DATA_KEY) {
  1631. goto not_found;
  1632. }
  1633. found_type = btrfs_file_extent_type(leaf, item);
  1634. extent_start = found_key.offset;
  1635. if (found_type == BTRFS_FILE_EXTENT_REG) {
  1636. extent_end = extent_start +
  1637. btrfs_file_extent_num_bytes(leaf, item);
  1638. err = 0;
  1639. if (start < extent_start || start >= extent_end) {
  1640. em->start = start;
  1641. if (start < extent_start) {
  1642. if (end < extent_start)
  1643. goto not_found;
  1644. em->end = extent_end - 1;
  1645. } else {
  1646. em->end = end;
  1647. }
  1648. goto not_found_em;
  1649. }
  1650. bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
  1651. if (bytenr == 0) {
  1652. em->start = extent_start;
  1653. em->end = extent_end - 1;
  1654. em->block_start = EXTENT_MAP_HOLE;
  1655. em->block_end = EXTENT_MAP_HOLE;
  1656. goto insert;
  1657. }
  1658. bytenr += btrfs_file_extent_offset(leaf, item);
  1659. em->block_start = bytenr;
  1660. em->block_end = em->block_start +
  1661. btrfs_file_extent_num_bytes(leaf, item) - 1;
  1662. em->start = extent_start;
  1663. em->end = extent_end - 1;
  1664. goto insert;
  1665. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  1666. unsigned long ptr;
  1667. char *map;
  1668. size_t size;
  1669. size_t extent_offset;
  1670. size_t copy_size;
  1671. size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
  1672. path->slots[0]));
  1673. extent_end = (extent_start + size - 1) |
  1674. ((u64)root->sectorsize - 1);
  1675. if (start < extent_start || start >= extent_end) {
  1676. em->start = start;
  1677. if (start < extent_start) {
  1678. if (end < extent_start)
  1679. goto not_found;
  1680. em->end = extent_end;
  1681. } else {
  1682. em->end = end;
  1683. }
  1684. goto not_found_em;
  1685. }
  1686. em->block_start = EXTENT_MAP_INLINE;
  1687. em->block_end = EXTENT_MAP_INLINE;
  1688. if (!page) {
  1689. em->start = extent_start;
  1690. em->end = extent_start + size - 1;
  1691. goto out;
  1692. }
  1693. extent_offset = ((u64)page->index << PAGE_CACHE_SHIFT) -
  1694. extent_start + page_offset;
  1695. copy_size = min_t(u64, PAGE_CACHE_SIZE - page_offset,
  1696. size - extent_offset);
  1697. em->start = extent_start + extent_offset;
  1698. em->end = (em->start + copy_size -1) |
  1699. ((u64)root->sectorsize -1);
  1700. map = kmap(page);
  1701. ptr = btrfs_file_extent_inline_start(item) + extent_offset;
  1702. if (create == 0 && !PageUptodate(page)) {
  1703. read_extent_buffer(leaf, map + page_offset, ptr,
  1704. copy_size);
  1705. flush_dcache_page(page);
  1706. } else if (create && PageUptodate(page)) {
  1707. if (!trans) {
  1708. kunmap(page);
  1709. free_extent_map(em);
  1710. em = NULL;
  1711. btrfs_release_path(root, path);
  1712. trans = btrfs_start_transaction(root, 1);
  1713. goto again;
  1714. }
  1715. write_extent_buffer(leaf, map + page_offset, ptr,
  1716. copy_size);
  1717. btrfs_mark_buffer_dirty(leaf);
  1718. }
  1719. kunmap(page);
  1720. set_extent_uptodate(em_tree, em->start, em->end, GFP_NOFS);
  1721. goto insert;
  1722. } else {
  1723. printk("unkknown found_type %d\n", found_type);
  1724. WARN_ON(1);
  1725. }
  1726. not_found:
  1727. em->start = start;
  1728. em->end = end;
  1729. not_found_em:
  1730. em->block_start = EXTENT_MAP_HOLE;
  1731. em->block_end = EXTENT_MAP_HOLE;
  1732. insert:
  1733. btrfs_release_path(root, path);
  1734. if (em->start > start || em->end < start) {
  1735. printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->end, start, end);
  1736. err = -EIO;
  1737. goto out;
  1738. }
  1739. ret = add_extent_mapping(em_tree, em);
  1740. if (ret == -EEXIST) {
  1741. free_extent_map(em);
  1742. em = NULL;
  1743. if (0 && failed_insert == 1) {
  1744. btrfs_drop_extent_cache(inode, start, end);
  1745. }
  1746. failed_insert++;
  1747. if (failed_insert > 5) {
  1748. printk("failing to insert %Lu %Lu\n", start, end);
  1749. err = -EIO;
  1750. goto out;
  1751. }
  1752. goto again;
  1753. }
  1754. err = 0;
  1755. out:
  1756. btrfs_free_path(path);
  1757. if (trans) {
  1758. ret = btrfs_end_transaction(trans, root);
  1759. if (!err)
  1760. err = ret;
  1761. }
  1762. mutex_unlock(&root->fs_info->fs_mutex);
  1763. if (err) {
  1764. free_extent_map(em);
  1765. WARN_ON(1);
  1766. return ERR_PTR(err);
  1767. }
  1768. return em;
  1769. }
  1770. static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
  1771. {
  1772. return extent_bmap(mapping, iblock, btrfs_get_extent);
  1773. }
  1774. int btrfs_readpage(struct file *file, struct page *page)
  1775. {
  1776. struct extent_map_tree *tree;
  1777. tree = &BTRFS_I(page->mapping->host)->extent_tree;
  1778. return extent_read_full_page(tree, page, btrfs_get_extent);
  1779. }
  1780. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  1781. {
  1782. struct extent_map_tree *tree;
  1783. if (current->flags & PF_MEMALLOC) {
  1784. redirty_page_for_writepage(wbc, page);
  1785. unlock_page(page);
  1786. return 0;
  1787. }
  1788. tree = &BTRFS_I(page->mapping->host)->extent_tree;
  1789. return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
  1790. }
  1791. static int btrfs_writepages(struct address_space *mapping,
  1792. struct writeback_control *wbc)
  1793. {
  1794. struct extent_map_tree *tree;
  1795. tree = &BTRFS_I(mapping->host)->extent_tree;
  1796. return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
  1797. }
  1798. static int
  1799. btrfs_readpages(struct file *file, struct address_space *mapping,
  1800. struct list_head *pages, unsigned nr_pages)
  1801. {
  1802. struct extent_map_tree *tree;
  1803. tree = &BTRFS_I(mapping->host)->extent_tree;
  1804. return extent_readpages(tree, mapping, pages, nr_pages,
  1805. btrfs_get_extent);
  1806. }
  1807. static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
  1808. {
  1809. struct extent_map_tree *tree;
  1810. int ret;
  1811. tree = &BTRFS_I(page->mapping->host)->extent_tree;
  1812. ret = try_release_extent_mapping(tree, page);
  1813. if (ret == 1) {
  1814. ClearPagePrivate(page);
  1815. set_page_private(page, 0);
  1816. page_cache_release(page);
  1817. }
  1818. return ret;
  1819. }
  1820. static void btrfs_invalidatepage(struct page *page, unsigned long offset)
  1821. {
  1822. struct extent_map_tree *tree;
  1823. tree = &BTRFS_I(page->mapping->host)->extent_tree;
  1824. extent_invalidatepage(tree, page, offset);
  1825. btrfs_releasepage(page, GFP_NOFS);
  1826. }
  1827. /*
  1828. * btrfs_page_mkwrite() is not allowed to change the file size as it gets
  1829. * called from a page fault handler when a page is first dirtied. Hence we must
  1830. * be careful to check for EOF conditions here. We set the page up correctly
  1831. * for a written page which means we get ENOSPC checking when writing into
  1832. * holes and correct delalloc and unwritten extent mapping on filesystems that
  1833. * support these features.
  1834. *
  1835. * We are not allowed to take the i_mutex here so we have to play games to
  1836. * protect against truncate races as the page could now be beyond EOF. Because
  1837. * vmtruncate() writes the inode size before removing pages, once we have the
  1838. * page lock we can determine safely if the page is beyond EOF. If it is not
  1839. * beyond EOF, then the page is guaranteed safe against truncation until we
  1840. * unlock the page.
  1841. */
  1842. int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
  1843. {
  1844. struct inode *inode = fdentry(vma->vm_file)->d_inode;
  1845. struct btrfs_root *root = BTRFS_I(inode)->root;
  1846. unsigned long end;
  1847. loff_t size;
  1848. int ret;
  1849. u64 page_start;
  1850. mutex_lock(&root->fs_info->fs_mutex);
  1851. ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
  1852. mutex_unlock(&root->fs_info->fs_mutex);
  1853. if (ret)
  1854. goto out;
  1855. ret = -EINVAL;
  1856. lock_page(page);
  1857. wait_on_page_writeback(page);
  1858. size = i_size_read(inode);
  1859. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  1860. if ((page->mapping != inode->i_mapping) ||
  1861. (page_start > size)) {
  1862. /* page got truncated out from underneath us */
  1863. goto out_unlock;
  1864. }
  1865. /* page is wholly or partially inside EOF */
  1866. if (page_start + PAGE_CACHE_SIZE > size)
  1867. end = size & ~PAGE_CACHE_MASK;
  1868. else
  1869. end = PAGE_CACHE_SIZE;
  1870. ret = btrfs_cow_one_page(inode, page, end);
  1871. out_unlock:
  1872. unlock_page(page);
  1873. out:
  1874. return ret;
  1875. }
  1876. static void btrfs_truncate(struct inode *inode)
  1877. {
  1878. struct btrfs_root *root = BTRFS_I(inode)->root;
  1879. int ret;
  1880. struct btrfs_trans_handle *trans;
  1881. unsigned long nr;
  1882. if (!S_ISREG(inode->i_mode))
  1883. return;
  1884. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1885. return;
  1886. btrfs_truncate_page(inode->i_mapping, inode->i_size);
  1887. mutex_lock(&root->fs_info->fs_mutex);
  1888. trans = btrfs_start_transaction(root, 1);
  1889. btrfs_set_trans_block_group(trans, inode);
  1890. /* FIXME, add redo link to tree so we don't leak on crash */
  1891. ret = btrfs_truncate_in_trans(trans, root, inode);
  1892. btrfs_update_inode(trans, root, inode);
  1893. nr = trans->blocks_used;
  1894. ret = btrfs_end_transaction(trans, root);
  1895. BUG_ON(ret);
  1896. mutex_unlock(&root->fs_info->fs_mutex);
  1897. btrfs_btree_balance_dirty(root, nr);
  1898. btrfs_throttle(root);
  1899. }
  1900. static int noinline create_subvol(struct btrfs_root *root, char *name,
  1901. int namelen)
  1902. {
  1903. struct btrfs_trans_handle *trans;
  1904. struct btrfs_key key;
  1905. struct btrfs_root_item root_item;
  1906. struct btrfs_inode_item *inode_item;
  1907. struct extent_buffer *leaf;
  1908. struct btrfs_root *new_root = root;
  1909. struct inode *inode;
  1910. struct inode *dir;
  1911. int ret;
  1912. int err;
  1913. u64 objectid;
  1914. u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
  1915. unsigned long nr = 1;
  1916. mutex_lock(&root->fs_info->fs_mutex);
  1917. ret = btrfs_check_free_space(root, 1, 0);
  1918. if (ret)
  1919. goto fail_commit;
  1920. trans = btrfs_start_transaction(root, 1);
  1921. BUG_ON(!trans);
  1922. ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
  1923. 0, &objectid);
  1924. if (ret)
  1925. goto fail;
  1926. leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
  1927. objectid, trans->transid, 0, 0,
  1928. 0, 0);
  1929. if (IS_ERR(leaf))
  1930. return PTR_ERR(leaf);
  1931. btrfs_set_header_nritems(leaf, 0);
  1932. btrfs_set_header_level(leaf, 0);
  1933. btrfs_set_header_bytenr(leaf, leaf->start);
  1934. btrfs_set_header_generation(leaf, trans->transid);
  1935. btrfs_set_header_owner(leaf, objectid);
  1936. write_extent_buffer(leaf, root->fs_info->fsid,
  1937. (unsigned long)btrfs_header_fsid(leaf),
  1938. BTRFS_FSID_SIZE);
  1939. btrfs_mark_buffer_dirty(leaf);
  1940. inode_item = &root_item.inode;
  1941. memset(inode_item, 0, sizeof(*inode_item));
  1942. inode_item->generation = cpu_to_le64(1);
  1943. inode_item->size = cpu_to_le64(3);
  1944. inode_item->nlink = cpu_to_le32(1);
  1945. inode_item->nblocks = cpu_to_le64(1);
  1946. inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
  1947. btrfs_set_root_bytenr(&root_item, leaf->start);
  1948. btrfs_set_root_level(&root_item, 0);
  1949. btrfs_set_root_refs(&root_item, 1);
  1950. btrfs_set_root_used(&root_item, 0);
  1951. memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
  1952. root_item.drop_level = 0;
  1953. free_extent_buffer(leaf);
  1954. leaf = NULL;
  1955. btrfs_set_root_dirid(&root_item, new_dirid);
  1956. key.objectid = objectid;
  1957. key.offset = 1;
  1958. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  1959. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  1960. &root_item);
  1961. if (ret)
  1962. goto fail;
  1963. /*
  1964. * insert the directory item
  1965. */
  1966. key.offset = (u64)-1;
  1967. dir = root->fs_info->sb->s_root->d_inode;
  1968. ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
  1969. name, namelen, dir->i_ino, &key,
  1970. BTRFS_FT_DIR);
  1971. if (ret)
  1972. goto fail;
  1973. ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
  1974. name, namelen, objectid,
  1975. root->fs_info->sb->s_root->d_inode->i_ino);
  1976. if (ret)
  1977. goto fail;
  1978. ret = btrfs_commit_transaction(trans, root);
  1979. if (ret)
  1980. goto fail_commit;
  1981. new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
  1982. BUG_ON(!new_root);
  1983. trans = btrfs_start_transaction(new_root, 1);
  1984. BUG_ON(!trans);
  1985. inode = btrfs_new_inode(trans, new_root, new_dirid,
  1986. BTRFS_I(dir)->block_group, S_IFDIR | 0700);
  1987. if (IS_ERR(inode))
  1988. goto fail;
  1989. inode->i_op = &btrfs_dir_inode_operations;
  1990. inode->i_fop = &btrfs_dir_file_operations;
  1991. new_root->inode = inode;
  1992. ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
  1993. new_dirid);
  1994. inode->i_nlink = 1;
  1995. inode->i_size = 0;
  1996. ret = btrfs_update_inode(trans, new_root, inode);
  1997. if (ret)
  1998. goto fail;
  1999. fail:
  2000. nr = trans->blocks_used;
  2001. err = btrfs_commit_transaction(trans, new_root);
  2002. if (err && !ret)
  2003. ret = err;
  2004. fail_commit:
  2005. mutex_unlock(&root->fs_info->fs_mutex);
  2006. btrfs_btree_balance_dirty(root, nr);
  2007. btrfs_throttle(root);
  2008. return ret;
  2009. }
  2010. static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
  2011. {
  2012. struct btrfs_pending_snapshot *pending_snapshot;
  2013. struct btrfs_trans_handle *trans;
  2014. int ret;
  2015. int err;
  2016. unsigned long nr = 0;
  2017. if (!root->ref_cows)
  2018. return -EINVAL;
  2019. mutex_lock(&root->fs_info->fs_mutex);
  2020. ret = btrfs_check_free_space(root, 1, 0);
  2021. if (ret)
  2022. goto fail_unlock;
  2023. pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
  2024. if (!pending_snapshot) {
  2025. ret = -ENOMEM;
  2026. goto fail_unlock;
  2027. }
  2028. pending_snapshot->name = kstrndup(name, namelen, GFP_NOFS);
  2029. if (!pending_snapshot->name) {
  2030. ret = -ENOMEM;
  2031. kfree(pending_snapshot);
  2032. goto fail_unlock;
  2033. }
  2034. trans = btrfs_start_transaction(root, 1);
  2035. BUG_ON(!trans);
  2036. pending_snapshot->root = root;
  2037. list_add(&pending_snapshot->list,
  2038. &trans->transaction->pending_snapshots);
  2039. ret = btrfs_update_inode(trans, root, root->inode);
  2040. err = btrfs_commit_transaction(trans, root);
  2041. fail_unlock:
  2042. mutex_unlock(&root->fs_info->fs_mutex);
  2043. btrfs_btree_balance_dirty(root, nr);
  2044. btrfs_throttle(root);
  2045. return ret;
  2046. }
  2047. unsigned long btrfs_force_ra(struct address_space *mapping,
  2048. struct file_ra_state *ra, struct file *file,
  2049. pgoff_t offset, pgoff_t last_index)
  2050. {
  2051. pgoff_t req_size;
  2052. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
  2053. req_size = last_index - offset + 1;
  2054. offset = page_cache_readahead(mapping, ra, file, offset, req_size);
  2055. return offset;
  2056. #else
  2057. req_size = min(last_index - offset + 1, (pgoff_t)128);
  2058. page_cache_sync_readahead(mapping, ra, file, offset, req_size);
  2059. return offset + req_size;
  2060. #endif
  2061. }
  2062. int btrfs_defrag_file(struct file *file) {
  2063. struct inode *inode = fdentry(file)->d_inode;
  2064. struct btrfs_root *root = BTRFS_I(inode)->root;
  2065. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  2066. struct page *page;
  2067. unsigned long last_index;
  2068. unsigned long ra_index = 0;
  2069. u64 page_start;
  2070. u64 page_end;
  2071. u64 delalloc_start;
  2072. u64 existing_delalloc;
  2073. unsigned long i;
  2074. int ret;
  2075. mutex_lock(&root->fs_info->fs_mutex);
  2076. ret = btrfs_check_free_space(root, inode->i_size, 0);
  2077. mutex_unlock(&root->fs_info->fs_mutex);
  2078. if (ret)
  2079. return -ENOSPC;
  2080. mutex_lock(&inode->i_mutex);
  2081. last_index = inode->i_size >> PAGE_CACHE_SHIFT;
  2082. for (i = 0; i <= last_index; i++) {
  2083. if (i == ra_index) {
  2084. ra_index = btrfs_force_ra(inode->i_mapping,
  2085. &file->f_ra,
  2086. file, ra_index, last_index);
  2087. }
  2088. page = grab_cache_page(inode->i_mapping, i);
  2089. if (!page)
  2090. goto out_unlock;
  2091. if (!PageUptodate(page)) {
  2092. btrfs_readpage(NULL, page);
  2093. lock_page(page);
  2094. if (!PageUptodate(page)) {
  2095. unlock_page(page);
  2096. page_cache_release(page);
  2097. goto out_unlock;
  2098. }
  2099. }
  2100. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  2101. page_end = page_start + PAGE_CACHE_SIZE - 1;
  2102. lock_extent(em_tree, page_start, page_end, GFP_NOFS);
  2103. delalloc_start = page_start;
  2104. existing_delalloc =
  2105. count_range_bits(&BTRFS_I(inode)->extent_tree,
  2106. &delalloc_start, page_end,
  2107. PAGE_CACHE_SIZE, EXTENT_DELALLOC);
  2108. set_extent_delalloc(em_tree, page_start,
  2109. page_end, GFP_NOFS);
  2110. spin_lock(&root->fs_info->delalloc_lock);
  2111. root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE -
  2112. existing_delalloc;
  2113. spin_unlock(&root->fs_info->delalloc_lock);
  2114. unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
  2115. set_page_dirty(page);
  2116. unlock_page(page);
  2117. page_cache_release(page);
  2118. balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
  2119. }
  2120. out_unlock:
  2121. mutex_unlock(&inode->i_mutex);
  2122. return 0;
  2123. }
  2124. static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
  2125. {
  2126. u64 new_size;
  2127. u64 old_size;
  2128. struct btrfs_ioctl_vol_args *vol_args;
  2129. struct btrfs_trans_handle *trans;
  2130. char *sizestr;
  2131. int ret = 0;
  2132. int namelen;
  2133. int mod = 0;
  2134. vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
  2135. if (!vol_args)
  2136. return -ENOMEM;
  2137. if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
  2138. ret = -EFAULT;
  2139. goto out;
  2140. }
  2141. namelen = strlen(vol_args->name);
  2142. if (namelen > BTRFS_VOL_NAME_MAX) {
  2143. ret = -EINVAL;
  2144. goto out;
  2145. }
  2146. sizestr = vol_args->name;
  2147. if (!strcmp(sizestr, "max"))
  2148. new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
  2149. else {
  2150. if (sizestr[0] == '-') {
  2151. mod = -1;
  2152. sizestr++;
  2153. } else if (sizestr[0] == '+') {
  2154. mod = 1;
  2155. sizestr++;
  2156. }
  2157. new_size = btrfs_parse_size(sizestr);
  2158. if (new_size == 0) {
  2159. ret = -EINVAL;
  2160. goto out;
  2161. }
  2162. }
  2163. mutex_lock(&root->fs_info->fs_mutex);
  2164. old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
  2165. if (mod < 0) {
  2166. if (new_size > old_size) {
  2167. ret = -EINVAL;
  2168. goto out_unlock;
  2169. }
  2170. new_size = old_size - new_size;
  2171. } else if (mod > 0) {
  2172. new_size = old_size + new_size;
  2173. }
  2174. if (new_size < 256 * 1024 * 1024) {
  2175. ret = -EINVAL;
  2176. goto out_unlock;
  2177. }
  2178. if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
  2179. ret = -EFBIG;
  2180. goto out_unlock;
  2181. }
  2182. do_div(new_size, root->sectorsize);
  2183. new_size *= root->sectorsize;
  2184. printk("new size is %Lu\n", new_size);
  2185. if (new_size > old_size) {
  2186. trans = btrfs_start_transaction(root, 1);
  2187. ret = btrfs_grow_extent_tree(trans, root, new_size);
  2188. btrfs_commit_transaction(trans, root);
  2189. } else {
  2190. ret = btrfs_shrink_extent_tree(root, new_size);
  2191. }
  2192. out_unlock:
  2193. mutex_unlock(&root->fs_info->fs_mutex);
  2194. out:
  2195. kfree(vol_args);
  2196. return ret;
  2197. }
  2198. static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
  2199. void __user *arg)
  2200. {
  2201. struct btrfs_ioctl_vol_args *vol_args;
  2202. struct btrfs_dir_item *di;
  2203. struct btrfs_path *path;
  2204. u64 root_dirid;
  2205. int namelen;
  2206. int ret;
  2207. vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
  2208. if (!vol_args)
  2209. return -ENOMEM;
  2210. if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
  2211. ret = -EFAULT;
  2212. goto out;
  2213. }
  2214. namelen = strlen(vol_args->name);
  2215. if (namelen > BTRFS_VOL_NAME_MAX) {
  2216. ret = -EINVAL;
  2217. goto out;
  2218. }
  2219. if (strchr(vol_args->name, '/')) {
  2220. ret = -EINVAL;
  2221. goto out;
  2222. }
  2223. path = btrfs_alloc_path();
  2224. if (!path) {
  2225. ret = -ENOMEM;
  2226. goto out;
  2227. }
  2228. root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
  2229. mutex_lock(&root->fs_info->fs_mutex);
  2230. di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
  2231. path, root_dirid,
  2232. vol_args->name, namelen, 0);
  2233. mutex_unlock(&root->fs_info->fs_mutex);
  2234. btrfs_free_path(path);
  2235. if (di && !IS_ERR(di)) {
  2236. ret = -EEXIST;
  2237. goto out;
  2238. }
  2239. if (IS_ERR(di)) {
  2240. ret = PTR_ERR(di);
  2241. goto out;
  2242. }
  2243. if (root == root->fs_info->tree_root)
  2244. ret = create_subvol(root, vol_args->name, namelen);
  2245. else
  2246. ret = create_snapshot(root, vol_args->name, namelen);
  2247. out:
  2248. kfree(vol_args);
  2249. return ret;
  2250. }
  2251. static int btrfs_ioctl_defrag(struct file *file)
  2252. {
  2253. struct inode *inode = fdentry(file)->d_inode;
  2254. struct btrfs_root *root = BTRFS_I(inode)->root;
  2255. switch (inode->i_mode & S_IFMT) {
  2256. case S_IFDIR:
  2257. mutex_lock(&root->fs_info->fs_mutex);
  2258. btrfs_defrag_root(root, 0);
  2259. btrfs_defrag_root(root->fs_info->extent_root, 0);
  2260. mutex_unlock(&root->fs_info->fs_mutex);
  2261. break;
  2262. case S_IFREG:
  2263. btrfs_defrag_file(file);
  2264. break;
  2265. }
  2266. return 0;
  2267. }
  2268. long btrfs_ioctl(struct file *file, unsigned int
  2269. cmd, unsigned long arg)
  2270. {
  2271. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  2272. switch (cmd) {
  2273. case BTRFS_IOC_SNAP_CREATE:
  2274. return btrfs_ioctl_snap_create(root, (void __user *)arg);
  2275. case BTRFS_IOC_DEFRAG:
  2276. return btrfs_ioctl_defrag(file);
  2277. case BTRFS_IOC_RESIZE:
  2278. return btrfs_ioctl_resize(root, (void __user *)arg);
  2279. }
  2280. return -ENOTTY;
  2281. }
  2282. /*
  2283. * Called inside transaction, so use GFP_NOFS
  2284. */
  2285. struct inode *btrfs_alloc_inode(struct super_block *sb)
  2286. {
  2287. struct btrfs_inode *ei;
  2288. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  2289. if (!ei)
  2290. return NULL;
  2291. ei->last_trans = 0;
  2292. ei->ordered_trans = 0;
  2293. return &ei->vfs_inode;
  2294. }
  2295. void btrfs_destroy_inode(struct inode *inode)
  2296. {
  2297. WARN_ON(!list_empty(&inode->i_dentry));
  2298. WARN_ON(inode->i_data.nrpages);
  2299. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  2300. }
  2301. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
  2302. static void init_once(struct kmem_cache * cachep, void *foo)
  2303. #else
  2304. static void init_once(void * foo, struct kmem_cache * cachep,
  2305. unsigned long flags)
  2306. #endif
  2307. {
  2308. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  2309. inode_init_once(&ei->vfs_inode);
  2310. }
  2311. void btrfs_destroy_cachep(void)
  2312. {
  2313. if (btrfs_inode_cachep)
  2314. kmem_cache_destroy(btrfs_inode_cachep);
  2315. if (btrfs_trans_handle_cachep)
  2316. kmem_cache_destroy(btrfs_trans_handle_cachep);
  2317. if (btrfs_transaction_cachep)
  2318. kmem_cache_destroy(btrfs_transaction_cachep);
  2319. if (btrfs_bit_radix_cachep)
  2320. kmem_cache_destroy(btrfs_bit_radix_cachep);
  2321. if (btrfs_path_cachep)
  2322. kmem_cache_destroy(btrfs_path_cachep);
  2323. }
  2324. struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
  2325. unsigned long extra_flags,
  2326. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
  2327. void (*ctor)(struct kmem_cache *, void *)
  2328. #else
  2329. void (*ctor)(void *, struct kmem_cache *,
  2330. unsigned long)
  2331. #endif
  2332. )
  2333. {
  2334. return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
  2335. SLAB_MEM_SPREAD | extra_flags), ctor
  2336. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
  2337. ,NULL
  2338. #endif
  2339. );
  2340. }
  2341. int btrfs_init_cachep(void)
  2342. {
  2343. btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
  2344. sizeof(struct btrfs_inode),
  2345. 0, init_once);
  2346. if (!btrfs_inode_cachep)
  2347. goto fail;
  2348. btrfs_trans_handle_cachep =
  2349. btrfs_cache_create("btrfs_trans_handle_cache",
  2350. sizeof(struct btrfs_trans_handle),
  2351. 0, NULL);
  2352. if (!btrfs_trans_handle_cachep)
  2353. goto fail;
  2354. btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
  2355. sizeof(struct btrfs_transaction),
  2356. 0, NULL);
  2357. if (!btrfs_transaction_cachep)
  2358. goto fail;
  2359. btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
  2360. sizeof(struct btrfs_path),
  2361. 0, NULL);
  2362. if (!btrfs_path_cachep)
  2363. goto fail;
  2364. btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
  2365. SLAB_DESTROY_BY_RCU, NULL);
  2366. if (!btrfs_bit_radix_cachep)
  2367. goto fail;
  2368. return 0;
  2369. fail:
  2370. btrfs_destroy_cachep();
  2371. return -ENOMEM;
  2372. }
  2373. static int btrfs_getattr(struct vfsmount *mnt,
  2374. struct dentry *dentry, struct kstat *stat)
  2375. {
  2376. struct inode *inode = dentry->d_inode;
  2377. generic_fillattr(inode, stat);
  2378. stat->blksize = PAGE_CACHE_SIZE;
  2379. return 0;
  2380. }
  2381. static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
  2382. struct inode * new_dir,struct dentry *new_dentry)
  2383. {
  2384. struct btrfs_trans_handle *trans;
  2385. struct btrfs_root *root = BTRFS_I(old_dir)->root;
  2386. struct inode *new_inode = new_dentry->d_inode;
  2387. struct inode *old_inode = old_dentry->d_inode;
  2388. struct timespec ctime = CURRENT_TIME;
  2389. struct btrfs_path *path;
  2390. int ret;
  2391. if (S_ISDIR(old_inode->i_mode) && new_inode &&
  2392. new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
  2393. return -ENOTEMPTY;
  2394. }
  2395. mutex_lock(&root->fs_info->fs_mutex);
  2396. ret = btrfs_check_free_space(root, 1, 0);
  2397. if (ret)
  2398. goto out_unlock;
  2399. trans = btrfs_start_transaction(root, 1);
  2400. btrfs_set_trans_block_group(trans, new_dir);
  2401. path = btrfs_alloc_path();
  2402. if (!path) {
  2403. ret = -ENOMEM;
  2404. goto out_fail;
  2405. }
  2406. old_dentry->d_inode->i_nlink++;
  2407. old_dir->i_ctime = old_dir->i_mtime = ctime;
  2408. new_dir->i_ctime = new_dir->i_mtime = ctime;
  2409. old_inode->i_ctime = ctime;
  2410. ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
  2411. if (ret)
  2412. goto out_fail;
  2413. if (new_inode) {
  2414. new_inode->i_ctime = CURRENT_TIME;
  2415. ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
  2416. if (ret)
  2417. goto out_fail;
  2418. }
  2419. ret = btrfs_add_link(trans, new_dentry, old_inode);
  2420. if (ret)
  2421. goto out_fail;
  2422. out_fail:
  2423. btrfs_free_path(path);
  2424. btrfs_end_transaction(trans, root);
  2425. out_unlock:
  2426. mutex_unlock(&root->fs_info->fs_mutex);
  2427. return ret;
  2428. }
  2429. static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
  2430. const char *symname)
  2431. {
  2432. struct btrfs_trans_handle *trans;
  2433. struct btrfs_root *root = BTRFS_I(dir)->root;
  2434. struct btrfs_path *path;
  2435. struct btrfs_key key;
  2436. struct inode *inode = NULL;
  2437. int err;
  2438. int drop_inode = 0;
  2439. u64 objectid;
  2440. int name_len;
  2441. int datasize;
  2442. unsigned long ptr;
  2443. struct btrfs_file_extent_item *ei;
  2444. struct extent_buffer *leaf;
  2445. unsigned long nr = 0;
  2446. name_len = strlen(symname) + 1;
  2447. if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
  2448. return -ENAMETOOLONG;
  2449. mutex_lock(&root->fs_info->fs_mutex);
  2450. err = btrfs_check_free_space(root, 1, 0);
  2451. if (err)
  2452. goto out_fail;
  2453. trans = btrfs_start_transaction(root, 1);
  2454. btrfs_set_trans_block_group(trans, dir);
  2455. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  2456. if (err) {
  2457. err = -ENOSPC;
  2458. goto out_unlock;
  2459. }
  2460. inode = btrfs_new_inode(trans, root, objectid,
  2461. BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
  2462. err = PTR_ERR(inode);
  2463. if (IS_ERR(inode))
  2464. goto out_unlock;
  2465. btrfs_set_trans_block_group(trans, inode);
  2466. err = btrfs_add_nondir(trans, dentry, inode);
  2467. if (err)
  2468. drop_inode = 1;
  2469. else {
  2470. inode->i_mapping->a_ops = &btrfs_aops;
  2471. inode->i_fop = &btrfs_file_operations;
  2472. inode->i_op = &btrfs_file_inode_operations;
  2473. extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
  2474. inode->i_mapping, GFP_NOFS);
  2475. BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
  2476. }
  2477. dir->i_sb->s_dirt = 1;
  2478. btrfs_update_inode_block_group(trans, inode);
  2479. btrfs_update_inode_block_group(trans, dir);
  2480. if (drop_inode)
  2481. goto out_unlock;
  2482. path = btrfs_alloc_path();
  2483. BUG_ON(!path);
  2484. key.objectid = inode->i_ino;
  2485. key.offset = 0;
  2486. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  2487. datasize = btrfs_file_extent_calc_inline_size(name_len);
  2488. err = btrfs_insert_empty_item(trans, root, path, &key,
  2489. datasize);
  2490. if (err) {
  2491. drop_inode = 1;
  2492. goto out_unlock;
  2493. }
  2494. leaf = path->nodes[0];
  2495. ei = btrfs_item_ptr(leaf, path->slots[0],
  2496. struct btrfs_file_extent_item);
  2497. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  2498. btrfs_set_file_extent_type(leaf, ei,
  2499. BTRFS_FILE_EXTENT_INLINE);
  2500. ptr = btrfs_file_extent_inline_start(ei);
  2501. write_extent_buffer(leaf, symname, ptr, name_len);
  2502. btrfs_mark_buffer_dirty(leaf);
  2503. btrfs_free_path(path);
  2504. inode->i_op = &btrfs_symlink_inode_operations;
  2505. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  2506. inode->i_size = name_len - 1;
  2507. err = btrfs_update_inode(trans, root, inode);
  2508. if (err)
  2509. drop_inode = 1;
  2510. out_unlock:
  2511. nr = trans->blocks_used;
  2512. btrfs_end_transaction(trans, root);
  2513. out_fail:
  2514. mutex_unlock(&root->fs_info->fs_mutex);
  2515. if (drop_inode) {
  2516. inode_dec_link_count(inode);
  2517. iput(inode);
  2518. }
  2519. btrfs_btree_balance_dirty(root, nr);
  2520. btrfs_throttle(root);
  2521. return err;
  2522. }
  2523. static struct inode_operations btrfs_dir_inode_operations = {
  2524. .lookup = btrfs_lookup,
  2525. .create = btrfs_create,
  2526. .unlink = btrfs_unlink,
  2527. .link = btrfs_link,
  2528. .mkdir = btrfs_mkdir,
  2529. .rmdir = btrfs_rmdir,
  2530. .rename = btrfs_rename,
  2531. .symlink = btrfs_symlink,
  2532. .setattr = btrfs_setattr,
  2533. .mknod = btrfs_mknod,
  2534. .setxattr = generic_setxattr,
  2535. .getxattr = generic_getxattr,
  2536. .listxattr = btrfs_listxattr,
  2537. .removexattr = generic_removexattr,
  2538. };
  2539. static struct inode_operations btrfs_dir_ro_inode_operations = {
  2540. .lookup = btrfs_lookup,
  2541. };
  2542. static struct file_operations btrfs_dir_file_operations = {
  2543. .llseek = generic_file_llseek,
  2544. .read = generic_read_dir,
  2545. .readdir = btrfs_readdir,
  2546. .unlocked_ioctl = btrfs_ioctl,
  2547. #ifdef CONFIG_COMPAT
  2548. .compat_ioctl = btrfs_ioctl,
  2549. #endif
  2550. };
  2551. static struct extent_map_ops btrfs_extent_map_ops = {
  2552. .fill_delalloc = run_delalloc_range,
  2553. .writepage_io_hook = btrfs_writepage_io_hook,
  2554. .readpage_io_hook = btrfs_readpage_io_hook,
  2555. .readpage_end_io_hook = btrfs_readpage_end_io_hook,
  2556. };
  2557. static struct address_space_operations btrfs_aops = {
  2558. .readpage = btrfs_readpage,
  2559. .writepage = btrfs_writepage,
  2560. .writepages = btrfs_writepages,
  2561. .readpages = btrfs_readpages,
  2562. .sync_page = block_sync_page,
  2563. .bmap = btrfs_bmap,
  2564. .invalidatepage = btrfs_invalidatepage,
  2565. .releasepage = btrfs_releasepage,
  2566. .set_page_dirty = __set_page_dirty_nobuffers,
  2567. };
  2568. static struct address_space_operations btrfs_symlink_aops = {
  2569. .readpage = btrfs_readpage,
  2570. .writepage = btrfs_writepage,
  2571. .invalidatepage = btrfs_invalidatepage,
  2572. .releasepage = btrfs_releasepage,
  2573. };
  2574. static struct inode_operations btrfs_file_inode_operations = {
  2575. .truncate = btrfs_truncate,
  2576. .getattr = btrfs_getattr,
  2577. .setattr = btrfs_setattr,
  2578. .setxattr = generic_setxattr,
  2579. .getxattr = generic_getxattr,
  2580. .listxattr = btrfs_listxattr,
  2581. .removexattr = generic_removexattr,
  2582. };
  2583. static struct inode_operations btrfs_special_inode_operations = {
  2584. .getattr = btrfs_getattr,
  2585. .setattr = btrfs_setattr,
  2586. };
  2587. static struct inode_operations btrfs_symlink_inode_operations = {
  2588. .readlink = generic_readlink,
  2589. .follow_link = page_follow_link_light,
  2590. .put_link = page_put_link,
  2591. };