inode.c 64 KB

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