inode.c 67 KB

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