super.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132
  1. #include <linux/module.h>
  2. #include <linux/buffer_head.h>
  3. #include <linux/fs.h>
  4. #include <linux/pagemap.h>
  5. #include <linux/highmem.h>
  6. #include <linux/time.h>
  7. #include <linux/init.h>
  8. #include <linux/string.h>
  9. #include <linux/smp_lock.h>
  10. #include <linux/backing-dev.h>
  11. #include <linux/mpage.h>
  12. #include <linux/swap.h>
  13. #include <linux/writeback.h>
  14. #include "ctree.h"
  15. #include "disk-io.h"
  16. #include "transaction.h"
  17. #include "btrfs_inode.h"
  18. #include "ioctl.h"
  19. void btrfs_fsinfo_release(struct kobject *obj)
  20. {
  21. struct btrfs_fs_info *fsinfo = container_of(obj,
  22. struct btrfs_fs_info, kobj);
  23. kfree(fsinfo);
  24. }
  25. struct kobj_type btrfs_fsinfo_ktype = {
  26. .release = btrfs_fsinfo_release,
  27. };
  28. struct btrfs_iget_args {
  29. u64 ino;
  30. struct btrfs_root *root;
  31. };
  32. decl_subsys(btrfs, &btrfs_fsinfo_ktype, NULL);
  33. #define BTRFS_SUPER_MAGIC 0x9123682E
  34. static struct inode_operations btrfs_dir_inode_operations;
  35. static struct inode_operations btrfs_dir_ro_inode_operations;
  36. static struct super_operations btrfs_super_ops;
  37. static struct file_operations btrfs_dir_file_operations;
  38. static struct inode_operations btrfs_file_inode_operations;
  39. static struct address_space_operations btrfs_aops;
  40. static struct file_operations btrfs_file_operations;
  41. static void btrfs_read_locked_inode(struct inode *inode)
  42. {
  43. struct btrfs_path *path;
  44. struct btrfs_inode_item *inode_item;
  45. struct btrfs_root *root = BTRFS_I(inode)->root;
  46. struct btrfs_key location;
  47. int ret;
  48. path = btrfs_alloc_path();
  49. BUG_ON(!path);
  50. btrfs_init_path(path);
  51. mutex_lock(&root->fs_info->fs_mutex);
  52. memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
  53. ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
  54. if (ret) {
  55. btrfs_free_path(path);
  56. goto make_bad;
  57. }
  58. inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  59. path->slots[0],
  60. struct btrfs_inode_item);
  61. inode->i_mode = btrfs_inode_mode(inode_item);
  62. inode->i_nlink = btrfs_inode_nlink(inode_item);
  63. inode->i_uid = btrfs_inode_uid(inode_item);
  64. inode->i_gid = btrfs_inode_gid(inode_item);
  65. inode->i_size = btrfs_inode_size(inode_item);
  66. inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
  67. inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
  68. inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
  69. inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
  70. inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
  71. inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
  72. inode->i_blocks = btrfs_inode_nblocks(inode_item);
  73. inode->i_generation = btrfs_inode_generation(inode_item);
  74. btrfs_free_path(path);
  75. inode_item = NULL;
  76. mutex_unlock(&root->fs_info->fs_mutex);
  77. switch (inode->i_mode & S_IFMT) {
  78. #if 0
  79. default:
  80. init_special_inode(inode, inode->i_mode,
  81. btrfs_inode_rdev(inode_item));
  82. break;
  83. #endif
  84. case S_IFREG:
  85. inode->i_mapping->a_ops = &btrfs_aops;
  86. inode->i_fop = &btrfs_file_operations;
  87. inode->i_op = &btrfs_file_inode_operations;
  88. break;
  89. case S_IFDIR:
  90. inode->i_fop = &btrfs_dir_file_operations;
  91. if (root == root->fs_info->tree_root)
  92. inode->i_op = &btrfs_dir_ro_inode_operations;
  93. else
  94. inode->i_op = &btrfs_dir_inode_operations;
  95. break;
  96. case S_IFLNK:
  97. // inode->i_op = &page_symlink_inode_operations;
  98. break;
  99. }
  100. return;
  101. make_bad:
  102. btrfs_release_path(root, path);
  103. btrfs_free_path(path);
  104. mutex_unlock(&root->fs_info->fs_mutex);
  105. make_bad_inode(inode);
  106. }
  107. static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
  108. struct btrfs_root *root,
  109. struct inode *dir,
  110. struct dentry *dentry)
  111. {
  112. struct btrfs_path *path;
  113. const char *name = dentry->d_name.name;
  114. int name_len = dentry->d_name.len;
  115. int ret;
  116. u64 objectid;
  117. struct btrfs_dir_item *di;
  118. path = btrfs_alloc_path();
  119. BUG_ON(!path);
  120. btrfs_init_path(path);
  121. ret = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  122. name, name_len, -1);
  123. if (ret < 0)
  124. goto err;
  125. if (ret > 0) {
  126. ret = -ENOENT;
  127. goto err;
  128. }
  129. di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  130. struct btrfs_dir_item);
  131. objectid = btrfs_disk_key_objectid(&di->location);
  132. ret = btrfs_del_item(trans, root, path);
  133. BUG_ON(ret);
  134. btrfs_release_path(root, path);
  135. ret = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
  136. objectid, -1);
  137. BUG_ON(ret);
  138. ret = btrfs_del_item(trans, root, path);
  139. BUG_ON(ret);
  140. dentry->d_inode->i_ctime = dir->i_ctime;
  141. err:
  142. btrfs_release_path(root, path);
  143. btrfs_free_path(path);
  144. if (ret == 0) {
  145. inode_dec_link_count(dentry->d_inode);
  146. dir->i_size -= name_len * 2;
  147. mark_inode_dirty(dir);
  148. }
  149. return ret;
  150. }
  151. static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
  152. {
  153. struct btrfs_root *root;
  154. struct btrfs_trans_handle *trans;
  155. int ret;
  156. root = BTRFS_I(dir)->root;
  157. mutex_lock(&root->fs_info->fs_mutex);
  158. trans = btrfs_start_transaction(root, 1);
  159. ret = btrfs_unlink_trans(trans, root, dir, dentry);
  160. btrfs_end_transaction(trans, root);
  161. mutex_unlock(&root->fs_info->fs_mutex);
  162. return ret;
  163. }
  164. static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
  165. {
  166. struct inode *inode = dentry->d_inode;
  167. int err;
  168. int ret;
  169. struct btrfs_root *root = BTRFS_I(dir)->root;
  170. struct btrfs_path *path;
  171. struct btrfs_key key;
  172. struct btrfs_trans_handle *trans;
  173. struct btrfs_key found_key;
  174. int found_type;
  175. struct btrfs_leaf *leaf;
  176. char *goodnames = "..";
  177. path = btrfs_alloc_path();
  178. BUG_ON(!path);
  179. btrfs_init_path(path);
  180. mutex_lock(&root->fs_info->fs_mutex);
  181. trans = btrfs_start_transaction(root, 1);
  182. key.objectid = inode->i_ino;
  183. key.offset = (u64)-1;
  184. key.flags = (u32)-1;
  185. while(1) {
  186. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  187. if (ret < 0) {
  188. err = ret;
  189. goto out;
  190. }
  191. BUG_ON(ret == 0);
  192. if (path->slots[0] == 0) {
  193. err = -ENOENT;
  194. goto out;
  195. }
  196. path->slots[0]--;
  197. leaf = btrfs_buffer_leaf(path->nodes[0]);
  198. btrfs_disk_key_to_cpu(&found_key,
  199. &leaf->items[path->slots[0]].key);
  200. found_type = btrfs_key_type(&found_key);
  201. if (found_key.objectid != inode->i_ino) {
  202. err = -ENOENT;
  203. goto out;
  204. }
  205. if ((found_type != BTRFS_DIR_ITEM_KEY &&
  206. found_type != BTRFS_DIR_INDEX_KEY) ||
  207. (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
  208. !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
  209. err = -ENOTEMPTY;
  210. goto out;
  211. }
  212. ret = btrfs_del_item(trans, root, path);
  213. BUG_ON(ret);
  214. if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
  215. break;
  216. btrfs_release_path(root, path);
  217. }
  218. ret = 0;
  219. btrfs_release_path(root, path);
  220. /* now the directory is empty */
  221. err = btrfs_unlink_trans(trans, root, dir, dentry);
  222. if (!err) {
  223. inode->i_size = 0;
  224. }
  225. out:
  226. btrfs_release_path(root, path);
  227. btrfs_free_path(path);
  228. mutex_unlock(&root->fs_info->fs_mutex);
  229. ret = btrfs_end_transaction(trans, root);
  230. if (ret && !err)
  231. err = ret;
  232. return err;
  233. }
  234. static int btrfs_free_inode(struct btrfs_trans_handle *trans,
  235. struct btrfs_root *root,
  236. struct inode *inode)
  237. {
  238. struct btrfs_path *path;
  239. int ret;
  240. clear_inode(inode);
  241. path = btrfs_alloc_path();
  242. BUG_ON(!path);
  243. btrfs_init_path(path);
  244. ret = btrfs_lookup_inode(trans, root, path,
  245. &BTRFS_I(inode)->location, -1);
  246. BUG_ON(ret);
  247. ret = btrfs_del_item(trans, root, path);
  248. BUG_ON(ret);
  249. btrfs_free_path(path);
  250. return ret;
  251. }
  252. static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
  253. struct btrfs_root *root,
  254. struct inode *inode)
  255. {
  256. int ret;
  257. struct btrfs_path *path;
  258. struct btrfs_key key;
  259. struct btrfs_disk_key *found_key;
  260. struct btrfs_leaf *leaf;
  261. struct btrfs_file_extent_item *fi = NULL;
  262. u64 extent_start = 0;
  263. u64 extent_num_blocks = 0;
  264. int found_extent;
  265. path = btrfs_alloc_path();
  266. BUG_ON(!path);
  267. /* FIXME, add redo link to tree so we don't leak on crash */
  268. key.objectid = inode->i_ino;
  269. key.offset = (u64)-1;
  270. key.flags = 0;
  271. /*
  272. * use BTRFS_CSUM_ITEM_KEY because it is larger than inline keys
  273. * or extent data
  274. */
  275. btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY);
  276. while(1) {
  277. btrfs_init_path(path);
  278. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  279. if (ret < 0) {
  280. goto error;
  281. }
  282. if (ret > 0) {
  283. BUG_ON(path->slots[0] == 0);
  284. path->slots[0]--;
  285. }
  286. leaf = btrfs_buffer_leaf(path->nodes[0]);
  287. found_key = &leaf->items[path->slots[0]].key;
  288. if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
  289. break;
  290. if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY &&
  291. btrfs_disk_key_type(found_key) != BTRFS_INLINE_DATA_KEY &&
  292. btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY)
  293. break;
  294. if (btrfs_disk_key_offset(found_key) < inode->i_size)
  295. break;
  296. if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) {
  297. fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  298. path->slots[0],
  299. struct btrfs_file_extent_item);
  300. extent_start = btrfs_file_extent_disk_blocknr(fi);
  301. extent_num_blocks =
  302. btrfs_file_extent_disk_num_blocks(fi);
  303. inode->i_blocks -=
  304. btrfs_file_extent_num_blocks(fi) >> 9;
  305. found_extent = 1;
  306. } else {
  307. found_extent = 0;
  308. }
  309. ret = btrfs_del_item(trans, root, path);
  310. BUG_ON(ret);
  311. btrfs_release_path(root, path);
  312. if (found_extent) {
  313. ret = btrfs_free_extent(trans, root, extent_start,
  314. extent_num_blocks, 0);
  315. BUG_ON(ret);
  316. }
  317. }
  318. ret = 0;
  319. error:
  320. btrfs_release_path(root, path);
  321. btrfs_free_path(path);
  322. return ret;
  323. }
  324. static void btrfs_delete_inode(struct inode *inode)
  325. {
  326. struct btrfs_trans_handle *trans;
  327. struct btrfs_root *root = BTRFS_I(inode)->root;
  328. int ret;
  329. truncate_inode_pages(&inode->i_data, 0);
  330. if (is_bad_inode(inode)) {
  331. goto no_delete;
  332. }
  333. inode->i_size = 0;
  334. mutex_lock(&root->fs_info->fs_mutex);
  335. trans = btrfs_start_transaction(root, 1);
  336. if (S_ISREG(inode->i_mode)) {
  337. ret = btrfs_truncate_in_trans(trans, root, inode);
  338. BUG_ON(ret);
  339. }
  340. btrfs_free_inode(trans, root, inode);
  341. btrfs_end_transaction(trans, root);
  342. mutex_unlock(&root->fs_info->fs_mutex);
  343. return;
  344. no_delete:
  345. clear_inode(inode);
  346. }
  347. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  348. struct btrfs_key *location)
  349. {
  350. const char *name = dentry->d_name.name;
  351. int namelen = dentry->d_name.len;
  352. struct btrfs_dir_item *di;
  353. struct btrfs_path *path;
  354. struct btrfs_root *root = BTRFS_I(dir)->root;
  355. int ret;
  356. path = btrfs_alloc_path();
  357. BUG_ON(!path);
  358. btrfs_init_path(path);
  359. ret = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
  360. namelen, 0);
  361. if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) {
  362. location->objectid = 0;
  363. ret = 0;
  364. goto out;
  365. }
  366. di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  367. struct btrfs_dir_item);
  368. btrfs_disk_key_to_cpu(location, &di->location);
  369. out:
  370. btrfs_release_path(root, path);
  371. btrfs_free_path(path);
  372. return ret;
  373. }
  374. int fixup_tree_root_location(struct btrfs_root *root,
  375. struct btrfs_key *location,
  376. struct btrfs_root **sub_root)
  377. {
  378. struct btrfs_path *path;
  379. struct btrfs_root_item *ri;
  380. if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
  381. return 0;
  382. if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
  383. return 0;
  384. path = btrfs_alloc_path();
  385. BUG_ON(!path);
  386. mutex_lock(&root->fs_info->fs_mutex);
  387. *sub_root = btrfs_read_fs_root(root->fs_info, location);
  388. if (IS_ERR(*sub_root))
  389. return PTR_ERR(*sub_root);
  390. ri = &(*sub_root)->root_item;
  391. location->objectid = btrfs_root_dirid(ri);
  392. location->flags = 0;
  393. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  394. location->offset = 0;
  395. btrfs_free_path(path);
  396. mutex_unlock(&root->fs_info->fs_mutex);
  397. return 0;
  398. }
  399. int btrfs_init_locked_inode(struct inode *inode, void *p)
  400. {
  401. struct btrfs_iget_args *args = p;
  402. inode->i_ino = args->ino;
  403. BTRFS_I(inode)->root = args->root;
  404. return 0;
  405. }
  406. int btrfs_find_actor(struct inode *inode, void *opaque)
  407. {
  408. struct btrfs_iget_args *args = opaque;
  409. return (args->ino == inode->i_ino &&
  410. args->root == BTRFS_I(inode)->root);
  411. }
  412. struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
  413. struct btrfs_root *root)
  414. {
  415. struct inode *inode;
  416. struct btrfs_iget_args args;
  417. args.ino = objectid;
  418. args.root = root;
  419. inode = iget5_locked(s, objectid, btrfs_find_actor,
  420. btrfs_init_locked_inode,
  421. (void *)&args);
  422. return inode;
  423. }
  424. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  425. struct nameidata *nd)
  426. {
  427. struct inode * inode;
  428. struct btrfs_inode *bi = BTRFS_I(dir);
  429. struct btrfs_root *root = bi->root;
  430. struct btrfs_root *sub_root = root;
  431. struct btrfs_key location;
  432. int ret;
  433. if (dentry->d_name.len > BTRFS_NAME_LEN)
  434. return ERR_PTR(-ENAMETOOLONG);
  435. mutex_lock(&root->fs_info->fs_mutex);
  436. ret = btrfs_inode_by_name(dir, dentry, &location);
  437. mutex_unlock(&root->fs_info->fs_mutex);
  438. if (ret < 0)
  439. return ERR_PTR(ret);
  440. inode = NULL;
  441. if (location.objectid) {
  442. ret = fixup_tree_root_location(root, &location, &sub_root);
  443. if (ret < 0)
  444. return ERR_PTR(ret);
  445. if (ret > 0)
  446. return ERR_PTR(-ENOENT);
  447. inode = btrfs_iget_locked(dir->i_sb, location.objectid,
  448. sub_root);
  449. if (!inode)
  450. return ERR_PTR(-EACCES);
  451. if (inode->i_state & I_NEW) {
  452. if (sub_root != root) {
  453. printk("adding new root for inode %lu root %p (found %p)\n", inode->i_ino, sub_root, BTRFS_I(inode)->root);
  454. igrab(inode);
  455. sub_root->inode = inode;
  456. }
  457. BTRFS_I(inode)->root = sub_root;
  458. memcpy(&BTRFS_I(inode)->location, &location,
  459. sizeof(location));
  460. btrfs_read_locked_inode(inode);
  461. unlock_new_inode(inode);
  462. }
  463. }
  464. return d_splice_alias(inode, dentry);
  465. }
  466. static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
  467. {
  468. struct inode *inode = filp->f_path.dentry->d_inode;
  469. struct btrfs_root *root = BTRFS_I(inode)->root;
  470. struct btrfs_item *item;
  471. struct btrfs_dir_item *di;
  472. struct btrfs_key key;
  473. struct btrfs_path *path;
  474. int ret;
  475. u32 nritems;
  476. struct btrfs_leaf *leaf;
  477. int slot;
  478. int advance;
  479. unsigned char d_type = DT_UNKNOWN;
  480. int over = 0;
  481. int key_type = BTRFS_DIR_INDEX_KEY;
  482. /* FIXME, use a real flag for deciding about the key type */
  483. if (root->fs_info->tree_root == root)
  484. key_type = BTRFS_DIR_ITEM_KEY;
  485. mutex_lock(&root->fs_info->fs_mutex);
  486. key.objectid = inode->i_ino;
  487. key.flags = 0;
  488. btrfs_set_key_type(&key, key_type);
  489. key.offset = filp->f_pos;
  490. path = btrfs_alloc_path();
  491. btrfs_init_path(path);
  492. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  493. if (ret < 0)
  494. goto err;
  495. advance = 0;
  496. while(1) {
  497. leaf = btrfs_buffer_leaf(path->nodes[0]);
  498. nritems = btrfs_header_nritems(&leaf->header);
  499. slot = path->slots[0];
  500. if (advance || slot >= nritems) {
  501. if (slot >= nritems -1) {
  502. ret = btrfs_next_leaf(root, path);
  503. if (ret)
  504. break;
  505. leaf = btrfs_buffer_leaf(path->nodes[0]);
  506. nritems = btrfs_header_nritems(&leaf->header);
  507. slot = path->slots[0];
  508. } else {
  509. slot++;
  510. path->slots[0]++;
  511. }
  512. }
  513. advance = 1;
  514. item = leaf->items + slot;
  515. if (btrfs_disk_key_objectid(&item->key) != key.objectid)
  516. break;
  517. if (key_type == BTRFS_DIR_INDEX_KEY &&
  518. btrfs_disk_key_offset(&item->key) > root->highest_inode)
  519. break;
  520. if (btrfs_disk_key_type(&item->key) != key_type)
  521. continue;
  522. if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
  523. continue;
  524. filp->f_pos = btrfs_disk_key_offset(&item->key);
  525. advance = 1;
  526. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  527. over = filldir(dirent, (const char *)(di + 1),
  528. btrfs_dir_name_len(di),
  529. btrfs_disk_key_offset(&item->key),
  530. btrfs_disk_key_objectid(&di->location), d_type);
  531. if (over)
  532. goto nopos;
  533. }
  534. filp->f_pos++;
  535. nopos:
  536. ret = 0;
  537. err:
  538. btrfs_release_path(root, path);
  539. btrfs_free_path(path);
  540. mutex_unlock(&root->fs_info->fs_mutex);
  541. return ret;
  542. }
  543. static void btrfs_put_super (struct super_block * sb)
  544. {
  545. struct btrfs_root *root = btrfs_sb(sb);
  546. int ret;
  547. ret = close_ctree(root);
  548. if (ret) {
  549. printk("close ctree returns %d\n", ret);
  550. }
  551. sb->s_fs_info = NULL;
  552. }
  553. static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
  554. {
  555. struct inode * inode;
  556. struct dentry * root_dentry;
  557. struct btrfs_super_block *disk_super;
  558. struct btrfs_root *tree_root;
  559. struct btrfs_inode *bi;
  560. sb->s_maxbytes = MAX_LFS_FILESIZE;
  561. sb->s_magic = BTRFS_SUPER_MAGIC;
  562. sb->s_op = &btrfs_super_ops;
  563. sb->s_time_gran = 1;
  564. tree_root = open_ctree(sb);
  565. if (!tree_root) {
  566. printk("btrfs: open_ctree failed\n");
  567. return -EIO;
  568. }
  569. sb->s_fs_info = tree_root;
  570. disk_super = tree_root->fs_info->disk_super;
  571. printk("read in super total blocks %Lu root %Lu\n",
  572. btrfs_super_total_blocks(disk_super),
  573. btrfs_super_root_dir(disk_super));
  574. inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
  575. tree_root);
  576. bi = BTRFS_I(inode);
  577. bi->location.objectid = inode->i_ino;
  578. bi->location.offset = 0;
  579. bi->location.flags = 0;
  580. bi->root = tree_root;
  581. btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
  582. if (!inode)
  583. return -ENOMEM;
  584. if (inode->i_state & I_NEW) {
  585. btrfs_read_locked_inode(inode);
  586. unlock_new_inode(inode);
  587. }
  588. root_dentry = d_alloc_root(inode);
  589. if (!root_dentry) {
  590. iput(inode);
  591. return -ENOMEM;
  592. }
  593. sb->s_root = root_dentry;
  594. return 0;
  595. }
  596. static void fill_inode_item(struct btrfs_inode_item *item,
  597. struct inode *inode)
  598. {
  599. btrfs_set_inode_uid(item, inode->i_uid);
  600. btrfs_set_inode_gid(item, inode->i_gid);
  601. btrfs_set_inode_size(item, inode->i_size);
  602. btrfs_set_inode_mode(item, inode->i_mode);
  603. btrfs_set_inode_nlink(item, inode->i_nlink);
  604. btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
  605. btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
  606. btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
  607. btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
  608. btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
  609. btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
  610. btrfs_set_inode_nblocks(item, inode->i_blocks);
  611. btrfs_set_inode_generation(item, inode->i_generation);
  612. }
  613. static int btrfs_update_inode(struct btrfs_trans_handle *trans,
  614. struct btrfs_root *root,
  615. struct inode *inode)
  616. {
  617. struct btrfs_inode_item *inode_item;
  618. struct btrfs_path *path;
  619. int ret;
  620. path = btrfs_alloc_path();
  621. BUG_ON(!path);
  622. btrfs_init_path(path);
  623. ret = btrfs_lookup_inode(trans, root, path,
  624. &BTRFS_I(inode)->location, 1);
  625. if (ret) {
  626. if (ret > 0)
  627. ret = -ENOENT;
  628. goto failed;
  629. }
  630. inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  631. path->slots[0],
  632. struct btrfs_inode_item);
  633. fill_inode_item(inode_item, inode);
  634. btrfs_mark_buffer_dirty(path->nodes[0]);
  635. ret = 0;
  636. failed:
  637. btrfs_release_path(root, path);
  638. btrfs_free_path(path);
  639. return ret;
  640. }
  641. static int btrfs_write_inode(struct inode *inode, int wait)
  642. {
  643. struct btrfs_root *root = BTRFS_I(inode)->root;
  644. struct btrfs_trans_handle *trans;
  645. int ret;
  646. mutex_lock(&root->fs_info->fs_mutex);
  647. trans = btrfs_start_transaction(root, 1);
  648. ret = btrfs_update_inode(trans, root, inode);
  649. if (wait)
  650. btrfs_commit_transaction(trans, root);
  651. else
  652. btrfs_end_transaction(trans, root);
  653. mutex_unlock(&root->fs_info->fs_mutex);
  654. return ret;
  655. }
  656. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  657. struct btrfs_root *root,
  658. u64 objectid, int mode)
  659. {
  660. struct inode *inode;
  661. struct btrfs_inode_item inode_item;
  662. struct btrfs_key *location;
  663. int ret;
  664. inode = new_inode(root->fs_info->sb);
  665. if (!inode)
  666. return ERR_PTR(-ENOMEM);
  667. BTRFS_I(inode)->root = root;
  668. inode->i_uid = current->fsuid;
  669. inode->i_gid = current->fsgid;
  670. inode->i_mode = mode;
  671. inode->i_ino = objectid;
  672. inode->i_blocks = 0;
  673. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  674. fill_inode_item(&inode_item, inode);
  675. location = &BTRFS_I(inode)->location;
  676. location->objectid = objectid;
  677. location->flags = 0;
  678. location->offset = 0;
  679. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  680. ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
  681. BUG_ON(ret);
  682. insert_inode_hash(inode);
  683. return inode;
  684. }
  685. static int btrfs_add_link(struct btrfs_trans_handle *trans,
  686. struct dentry *dentry, struct inode *inode)
  687. {
  688. int ret;
  689. struct btrfs_key key;
  690. struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
  691. key.objectid = inode->i_ino;
  692. key.flags = 0;
  693. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  694. key.offset = 0;
  695. ret = btrfs_insert_dir_item(trans, root,
  696. dentry->d_name.name, dentry->d_name.len,
  697. dentry->d_parent->d_inode->i_ino,
  698. &key, 0);
  699. if (ret == 0) {
  700. dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
  701. ret = btrfs_update_inode(trans, root,
  702. dentry->d_parent->d_inode);
  703. }
  704. return ret;
  705. }
  706. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  707. struct dentry *dentry, struct inode *inode)
  708. {
  709. int err = btrfs_add_link(trans, dentry, inode);
  710. if (!err) {
  711. d_instantiate(dentry, inode);
  712. return 0;
  713. }
  714. if (err > 0)
  715. err = -EEXIST;
  716. return err;
  717. }
  718. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  719. int mode, struct nameidata *nd)
  720. {
  721. struct btrfs_trans_handle *trans;
  722. struct btrfs_root *root = BTRFS_I(dir)->root;
  723. struct inode *inode;
  724. int err;
  725. int drop_inode = 0;
  726. u64 objectid;
  727. mutex_lock(&root->fs_info->fs_mutex);
  728. trans = btrfs_start_transaction(root, 1);
  729. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  730. if (err) {
  731. err = -ENOSPC;
  732. goto out_unlock;
  733. }
  734. inode = btrfs_new_inode(trans, root, objectid, mode);
  735. err = PTR_ERR(inode);
  736. if (IS_ERR(inode))
  737. goto out_unlock;
  738. // FIXME mark the inode dirty
  739. err = btrfs_add_nondir(trans, dentry, inode);
  740. if (err)
  741. drop_inode = 1;
  742. else {
  743. inode->i_mapping->a_ops = &btrfs_aops;
  744. inode->i_fop = &btrfs_file_operations;
  745. inode->i_op = &btrfs_file_inode_operations;
  746. }
  747. dir->i_sb->s_dirt = 1;
  748. out_unlock:
  749. btrfs_end_transaction(trans, root);
  750. mutex_unlock(&root->fs_info->fs_mutex);
  751. if (drop_inode) {
  752. inode_dec_link_count(inode);
  753. iput(inode);
  754. }
  755. return err;
  756. }
  757. static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
  758. struct btrfs_root *root,
  759. u64 objectid, u64 dirid)
  760. {
  761. int ret;
  762. char buf[2];
  763. struct btrfs_key key;
  764. buf[0] = '.';
  765. buf[1] = '.';
  766. key.objectid = objectid;
  767. key.offset = 0;
  768. key.flags = 0;
  769. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  770. ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
  771. &key, 1);
  772. if (ret)
  773. goto error;
  774. key.objectid = dirid;
  775. ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
  776. &key, 1);
  777. if (ret)
  778. goto error;
  779. error:
  780. return ret;
  781. }
  782. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  783. {
  784. struct inode *inode;
  785. struct btrfs_trans_handle *trans;
  786. struct btrfs_root *root = BTRFS_I(dir)->root;
  787. int err = 0;
  788. int drop_on_err = 0;
  789. u64 objectid;
  790. mutex_lock(&root->fs_info->fs_mutex);
  791. trans = btrfs_start_transaction(root, 1);
  792. if (IS_ERR(trans)) {
  793. err = PTR_ERR(trans);
  794. goto out_unlock;
  795. }
  796. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  797. if (err) {
  798. err = -ENOSPC;
  799. goto out_unlock;
  800. }
  801. inode = btrfs_new_inode(trans, root, objectid, S_IFDIR | mode);
  802. if (IS_ERR(inode)) {
  803. err = PTR_ERR(inode);
  804. goto out_fail;
  805. }
  806. drop_on_err = 1;
  807. inode->i_op = &btrfs_dir_inode_operations;
  808. inode->i_fop = &btrfs_dir_file_operations;
  809. err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
  810. if (err)
  811. goto out_fail;
  812. inode->i_size = 6;
  813. err = btrfs_update_inode(trans, root, inode);
  814. if (err)
  815. goto out_fail;
  816. err = btrfs_add_link(trans, dentry, inode);
  817. if (err)
  818. goto out_fail;
  819. d_instantiate(dentry, inode);
  820. drop_on_err = 0;
  821. out_fail:
  822. btrfs_end_transaction(trans, root);
  823. out_unlock:
  824. mutex_unlock(&root->fs_info->fs_mutex);
  825. if (drop_on_err)
  826. iput(inode);
  827. return err;
  828. }
  829. static int btrfs_sync_fs(struct super_block *sb, int wait)
  830. {
  831. struct btrfs_trans_handle *trans;
  832. struct btrfs_root *root;
  833. int ret;
  834. root = btrfs_sb(sb);
  835. sb->s_dirt = 0;
  836. if (!wait) {
  837. filemap_flush(root->fs_info->btree_inode->i_mapping);
  838. return 0;
  839. }
  840. filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
  841. mutex_lock(&root->fs_info->fs_mutex);
  842. trans = btrfs_start_transaction(root, 1);
  843. ret = btrfs_commit_transaction(trans, root);
  844. sb->s_dirt = 0;
  845. BUG_ON(ret);
  846. printk("btrfs sync_fs\n");
  847. mutex_unlock(&root->fs_info->fs_mutex);
  848. return 0;
  849. }
  850. #if 0
  851. static int btrfs_get_block_inline(struct inode *inode, sector_t iblock,
  852. struct buffer_head *result, int create)
  853. {
  854. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  855. struct btrfs_path *path;
  856. struct btrfs_key key;
  857. struct btrfs_leaf *leaf;
  858. int num_bytes = result->b_size;
  859. int item_size;
  860. int ret;
  861. u64 pos;
  862. char *ptr;
  863. int copy_size;
  864. int err = 0;
  865. char *safe_ptr;
  866. char *data_ptr;
  867. path = btrfs_alloc_path();
  868. BUG_ON(!path);
  869. WARN_ON(create);
  870. if (create) {
  871. return 0;
  872. }
  873. pos = iblock << inode->i_blkbits;
  874. key.objectid = inode->i_ino;
  875. key.flags = 0;
  876. btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
  877. ptr = kmap(result->b_page);
  878. safe_ptr = ptr;
  879. ptr += (pos & (PAGE_CACHE_SIZE -1));
  880. again:
  881. key.offset = pos;
  882. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  883. if (ret) {
  884. if (ret < 0)
  885. err = ret;
  886. else
  887. err = 0;
  888. goto out;
  889. }
  890. leaf = btrfs_buffer_leaf(path->nodes[0]);
  891. item_size = btrfs_item_size(leaf->items + path->slots[0]);
  892. copy_size = min(num_bytes, item_size);
  893. data_ptr = btrfs_item_ptr(leaf, path->slots[0], char);
  894. WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size);
  895. memcpy(ptr, data_ptr, copy_size);
  896. pos += copy_size;
  897. num_bytes -= copy_size;
  898. WARN_ON(num_bytes < 0);
  899. ptr += copy_size;
  900. btrfs_release_path(root, path);
  901. if (num_bytes != 0) {
  902. if (pos >= i_size_read(inode))
  903. memset(ptr, 0, num_bytes);
  904. else
  905. goto again;
  906. }
  907. set_buffer_uptodate(result);
  908. map_bh(result, inode->i_sb, 0);
  909. err = 0;
  910. out:
  911. btrfs_free_path(path);
  912. kunmap(result->b_page);
  913. return err;
  914. }
  915. #endif
  916. static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
  917. struct buffer_head *result, int create)
  918. {
  919. int ret;
  920. int err = 0;
  921. u64 blocknr;
  922. u64 extent_start = 0;
  923. u64 extent_end = 0;
  924. u64 objectid = inode->i_ino;
  925. struct btrfs_path *path;
  926. struct btrfs_root *root = BTRFS_I(inode)->root;
  927. struct btrfs_trans_handle *trans = NULL;
  928. struct btrfs_file_extent_item *item;
  929. struct btrfs_leaf *leaf;
  930. struct btrfs_disk_key *found_key;
  931. path = btrfs_alloc_path();
  932. BUG_ON(!path);
  933. btrfs_init_path(path);
  934. if (create)
  935. trans = btrfs_start_transaction(root, 1);
  936. ret = btrfs_lookup_file_extent(trans, root, path,
  937. inode->i_ino,
  938. iblock << inode->i_blkbits, create);
  939. if (ret < 0) {
  940. err = ret;
  941. goto out;
  942. }
  943. if (ret != 0) {
  944. if (path->slots[0] == 0) {
  945. btrfs_release_path(root, path);
  946. goto allocate;
  947. }
  948. path->slots[0]--;
  949. }
  950. item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  951. struct btrfs_file_extent_item);
  952. leaf = btrfs_buffer_leaf(path->nodes[0]);
  953. blocknr = btrfs_file_extent_disk_blocknr(item);
  954. blocknr += btrfs_file_extent_offset(item);
  955. /* exact match found, use it, FIXME, deal with extents
  956. * other than the page size
  957. */
  958. if (ret == 0) {
  959. err = 0;
  960. BUG_ON(btrfs_file_extent_disk_num_blocks(item) != 1);
  961. if (create &&
  962. btrfs_file_extent_generation(item) != trans->transid) {
  963. struct btrfs_key ins;
  964. ret = btrfs_alloc_extent(trans, root, 1,
  965. blocknr, (u64)-1, &ins);
  966. BUG_ON(ret);
  967. btrfs_set_file_extent_disk_blocknr(item, ins.objectid);
  968. mark_buffer_dirty(path->nodes[0]);
  969. ret = btrfs_free_extent(trans, root,
  970. blocknr, 1, 0);
  971. BUG_ON(ret);
  972. blocknr = ins.objectid;
  973. }
  974. map_bh(result, inode->i_sb, blocknr);
  975. btrfs_map_bh_to_logical(root, result, blocknr);
  976. goto out;
  977. }
  978. /* are we inside the extent that was found? */
  979. found_key = &leaf->items[path->slots[0]].key;
  980. if (btrfs_disk_key_objectid(found_key) != objectid ||
  981. btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
  982. extent_end = 0;
  983. extent_start = 0;
  984. btrfs_release_path(root, path);
  985. goto allocate;
  986. }
  987. extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
  988. extent_start = extent_start >> inode->i_blkbits;
  989. extent_start += btrfs_file_extent_offset(item);
  990. extent_end = extent_start + btrfs_file_extent_num_blocks(item);
  991. if (iblock >= extent_start && iblock < extent_end) {
  992. err = 0;
  993. btrfs_map_bh_to_logical(root, result, blocknr + iblock -
  994. extent_start);
  995. goto out;
  996. }
  997. allocate:
  998. /* ok, create a new extent */
  999. if (!create) {
  1000. err = 0;
  1001. goto out;
  1002. }
  1003. ret = btrfs_alloc_file_extent(trans, root, objectid,
  1004. iblock << inode->i_blkbits,
  1005. 1, extent_end, &blocknr);
  1006. if (ret) {
  1007. err = ret;
  1008. goto out;
  1009. }
  1010. inode->i_blocks += inode->i_sb->s_blocksize >> 9;
  1011. set_buffer_new(result);
  1012. map_bh(result, inode->i_sb, blocknr);
  1013. btrfs_map_bh_to_logical(root, result, blocknr);
  1014. out:
  1015. btrfs_release_path(root, path);
  1016. btrfs_free_path(path);
  1017. if (trans)
  1018. btrfs_end_transaction(trans, root);
  1019. return err;
  1020. }
  1021. static int btrfs_get_block(struct inode *inode, sector_t iblock,
  1022. struct buffer_head *result, int create)
  1023. {
  1024. int err;
  1025. struct btrfs_root *root = BTRFS_I(inode)->root;
  1026. mutex_lock(&root->fs_info->fs_mutex);
  1027. err = btrfs_get_block_lock(inode, iblock, result, create);
  1028. // err = btrfs_get_block_inline(inode, iblock, result, create);
  1029. mutex_unlock(&root->fs_info->fs_mutex);
  1030. return err;
  1031. }
  1032. static int btrfs_prepare_write(struct file *file, struct page *page,
  1033. unsigned from, unsigned to)
  1034. {
  1035. return nobh_prepare_write(page, from, to, btrfs_get_block);
  1036. }
  1037. static int btrfs_commit_write(struct file *file, struct page *page,
  1038. unsigned from, unsigned to)
  1039. {
  1040. return nobh_commit_write(file, page, from, to);
  1041. }
  1042. static void btrfs_write_super(struct super_block *sb)
  1043. {
  1044. btrfs_sync_fs(sb, 1);
  1045. }
  1046. static int btrfs_readpage(struct file *file, struct page *page)
  1047. {
  1048. return mpage_readpage(page, btrfs_get_block);
  1049. }
  1050. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  1051. {
  1052. return nobh_writepage(page, btrfs_get_block, wbc);
  1053. }
  1054. static void btrfs_truncate(struct inode *inode)
  1055. {
  1056. struct btrfs_root *root = BTRFS_I(inode)->root;
  1057. int ret;
  1058. struct btrfs_trans_handle *trans;
  1059. if (!S_ISREG(inode->i_mode))
  1060. return;
  1061. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1062. return;
  1063. nobh_truncate_page(inode->i_mapping, inode->i_size);
  1064. /* FIXME, add redo link to tree so we don't leak on crash */
  1065. mutex_lock(&root->fs_info->fs_mutex);
  1066. trans = btrfs_start_transaction(root, 1);
  1067. ret = btrfs_truncate_in_trans(trans, root, inode);
  1068. BUG_ON(ret);
  1069. ret = btrfs_end_transaction(trans, root);
  1070. BUG_ON(ret);
  1071. mutex_unlock(&root->fs_info->fs_mutex);
  1072. mark_inode_dirty(inode);
  1073. }
  1074. static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
  1075. struct page **prepared_pages,
  1076. const char __user * buf)
  1077. {
  1078. long page_fault = 0;
  1079. int i;
  1080. int offset = pos & (PAGE_CACHE_SIZE - 1);
  1081. for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
  1082. size_t count = min_t(size_t,
  1083. PAGE_CACHE_SIZE - offset, write_bytes);
  1084. struct page *page = prepared_pages[i];
  1085. fault_in_pages_readable(buf, count);
  1086. /* Copy data from userspace to the current page */
  1087. kmap(page);
  1088. page_fault = __copy_from_user(page_address(page) + offset,
  1089. buf, count);
  1090. /* Flush processor's dcache for this page */
  1091. flush_dcache_page(page);
  1092. kunmap(page);
  1093. buf += count;
  1094. write_bytes -= count;
  1095. if (page_fault)
  1096. break;
  1097. }
  1098. return page_fault ? -EFAULT : 0;
  1099. }
  1100. static void btrfs_drop_pages(struct page **pages, size_t num_pages)
  1101. {
  1102. size_t i;
  1103. for (i = 0; i < num_pages; i++) {
  1104. if (!pages[i])
  1105. break;
  1106. unlock_page(pages[i]);
  1107. mark_page_accessed(pages[i]);
  1108. page_cache_release(pages[i]);
  1109. }
  1110. }
  1111. static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
  1112. struct btrfs_root *root,
  1113. struct file *file,
  1114. struct page **pages,
  1115. size_t num_pages,
  1116. loff_t pos,
  1117. size_t write_bytes)
  1118. {
  1119. int i;
  1120. int offset;
  1121. int err = 0;
  1122. int ret;
  1123. int this_write;
  1124. struct inode *inode = file->f_path.dentry->d_inode;
  1125. for (i = 0; i < num_pages; i++) {
  1126. offset = pos & (PAGE_CACHE_SIZE -1);
  1127. this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
  1128. /* FIXME, one block at a time */
  1129. mutex_lock(&root->fs_info->fs_mutex);
  1130. trans = btrfs_start_transaction(root, 1);
  1131. btrfs_csum_file_block(trans, root, inode->i_ino,
  1132. pages[i]->index << PAGE_CACHE_SHIFT,
  1133. kmap(pages[i]), PAGE_CACHE_SIZE);
  1134. kunmap(pages[i]);
  1135. SetPageChecked(pages[i]);
  1136. ret = btrfs_end_transaction(trans, root);
  1137. BUG_ON(ret);
  1138. mutex_unlock(&root->fs_info->fs_mutex);
  1139. ret = nobh_commit_write(file, pages[i], offset,
  1140. offset + this_write);
  1141. pos += this_write;
  1142. if (ret) {
  1143. err = ret;
  1144. goto failed;
  1145. }
  1146. WARN_ON(this_write > write_bytes);
  1147. write_bytes -= this_write;
  1148. }
  1149. failed:
  1150. return err;
  1151. }
  1152. static int prepare_pages(struct btrfs_trans_handle *trans,
  1153. struct btrfs_root *root,
  1154. struct file *file,
  1155. struct page **pages,
  1156. size_t num_pages,
  1157. loff_t pos,
  1158. unsigned long first_index,
  1159. unsigned long last_index,
  1160. size_t write_bytes)
  1161. {
  1162. int i;
  1163. unsigned long index = pos >> PAGE_CACHE_SHIFT;
  1164. struct inode *inode = file->f_path.dentry->d_inode;
  1165. int offset;
  1166. int err = 0;
  1167. int ret;
  1168. int this_write;
  1169. loff_t isize = i_size_read(inode);
  1170. memset(pages, 0, num_pages * sizeof(struct page *));
  1171. for (i = 0; i < num_pages; i++) {
  1172. pages[i] = grab_cache_page(inode->i_mapping, index + i);
  1173. if (!pages[i]) {
  1174. err = -ENOMEM;
  1175. goto failed_release;
  1176. }
  1177. offset = pos & (PAGE_CACHE_SIZE -1);
  1178. this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
  1179. if (!PageUptodate(pages[i]) &&
  1180. (pages[i]->index == first_index ||
  1181. pages[i]->index == last_index) && pos < isize) {
  1182. ret = mpage_readpage(pages[i], btrfs_get_block);
  1183. BUG_ON(ret);
  1184. lock_page(pages[i]);
  1185. }
  1186. ret = nobh_prepare_write(pages[i], offset,
  1187. offset + this_write,
  1188. btrfs_get_block);
  1189. pos += this_write;
  1190. if (ret) {
  1191. err = ret;
  1192. goto failed_truncate;
  1193. }
  1194. WARN_ON(this_write > write_bytes);
  1195. write_bytes -= this_write;
  1196. }
  1197. return 0;
  1198. failed_release:
  1199. btrfs_drop_pages(pages, num_pages);
  1200. return err;
  1201. failed_truncate:
  1202. btrfs_drop_pages(pages, num_pages);
  1203. if (pos > isize)
  1204. vmtruncate(inode, isize);
  1205. return err;
  1206. }
  1207. static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
  1208. size_t count, loff_t *ppos)
  1209. {
  1210. loff_t pos;
  1211. size_t num_written = 0;
  1212. int err = 0;
  1213. int ret = 0;
  1214. struct inode *inode = file->f_path.dentry->d_inode;
  1215. struct btrfs_root *root = BTRFS_I(inode)->root;
  1216. struct page *pages[1];
  1217. unsigned long first_index;
  1218. unsigned long last_index;
  1219. if (file->f_flags & O_DIRECT)
  1220. return -EINVAL;
  1221. pos = *ppos;
  1222. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1223. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  1224. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  1225. if (err)
  1226. goto out;
  1227. if (count == 0)
  1228. goto out;
  1229. err = remove_suid(file->f_path.dentry);
  1230. if (err)
  1231. goto out;
  1232. file_update_time(file);
  1233. mutex_lock(&inode->i_mutex);
  1234. first_index = pos >> PAGE_CACHE_SHIFT;
  1235. last_index = (pos + count) >> PAGE_CACHE_SHIFT;
  1236. while(count > 0) {
  1237. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  1238. size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
  1239. size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
  1240. PAGE_CACHE_SHIFT;
  1241. ret = prepare_pages(NULL, root, file, pages, num_pages,
  1242. pos, first_index, last_index, write_bytes);
  1243. BUG_ON(ret);
  1244. ret = btrfs_copy_from_user(pos, num_pages,
  1245. write_bytes, pages, buf);
  1246. BUG_ON(ret);
  1247. ret = dirty_and_release_pages(NULL, root, file, pages,
  1248. num_pages, pos, write_bytes);
  1249. BUG_ON(ret);
  1250. btrfs_drop_pages(pages, num_pages);
  1251. buf += write_bytes;
  1252. count -= write_bytes;
  1253. pos += write_bytes;
  1254. num_written += write_bytes;
  1255. balance_dirty_pages_ratelimited(inode->i_mapping);
  1256. cond_resched();
  1257. }
  1258. mutex_unlock(&inode->i_mutex);
  1259. out:
  1260. *ppos = pos;
  1261. current->backing_dev_info = NULL;
  1262. return num_written ? num_written : err;
  1263. }
  1264. #if 0
  1265. static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode,
  1266. struct page *page, loff_t pos,
  1267. size_t offset, size_t write_bytes)
  1268. {
  1269. struct btrfs_path *path;
  1270. struct btrfs_trans_handle *trans;
  1271. struct btrfs_key key;
  1272. struct btrfs_leaf *leaf;
  1273. struct btrfs_key found_key;
  1274. int ret;
  1275. size_t copy_size = 0;
  1276. char *dst = NULL;
  1277. int err = 0;
  1278. size_t num_written = 0;
  1279. path = btrfs_alloc_path();
  1280. BUG_ON(!path);
  1281. mutex_lock(&root->fs_info->fs_mutex);
  1282. trans = btrfs_start_transaction(root, 1);
  1283. key.objectid = inode->i_ino;
  1284. key.flags = 0;
  1285. btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
  1286. again:
  1287. key.offset = pos;
  1288. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1289. if (ret < 0) {
  1290. err = ret;
  1291. goto out;
  1292. }
  1293. if (ret == 0) {
  1294. leaf = btrfs_buffer_leaf(path->nodes[0]);
  1295. btrfs_disk_key_to_cpu(&found_key,
  1296. &leaf->items[path->slots[0]].key);
  1297. copy_size = btrfs_item_size(leaf->items + path->slots[0]);
  1298. dst = btrfs_item_ptr(leaf, path->slots[0], char);
  1299. copy_size = min(write_bytes, copy_size);
  1300. goto copyit;
  1301. } else {
  1302. int slot = path->slots[0];
  1303. if (slot > 0) {
  1304. slot--;
  1305. }
  1306. // FIXME find max key
  1307. leaf = btrfs_buffer_leaf(path->nodes[0]);
  1308. btrfs_disk_key_to_cpu(&found_key,
  1309. &leaf->items[slot].key);
  1310. if (found_key.objectid != inode->i_ino)
  1311. goto insert;
  1312. if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY)
  1313. goto insert;
  1314. copy_size = btrfs_item_size(leaf->items + slot);
  1315. if (found_key.offset + copy_size <= pos)
  1316. goto insert;
  1317. dst = btrfs_item_ptr(leaf, path->slots[0], char);
  1318. dst += pos - found_key.offset;
  1319. copy_size = copy_size - (pos - found_key.offset);
  1320. BUG_ON(copy_size < 0);
  1321. copy_size = min(write_bytes, copy_size);
  1322. WARN_ON(copy_size == 0);
  1323. goto copyit;
  1324. }
  1325. insert:
  1326. btrfs_release_path(root, path);
  1327. copy_size = min(write_bytes,
  1328. (size_t)BTRFS_LEAF_DATA_SIZE(root) -
  1329. sizeof(struct btrfs_item) * 4);
  1330. ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size);
  1331. BUG_ON(ret);
  1332. dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  1333. path->slots[0], char);
  1334. copyit:
  1335. WARN_ON(copy_size == 0);
  1336. WARN_ON(dst + copy_size >
  1337. btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  1338. path->slots[0], char) +
  1339. btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items +
  1340. path->slots[0]));
  1341. btrfs_memcpy(root, path->nodes[0]->b_data, dst,
  1342. page_address(page) + offset, copy_size);
  1343. mark_buffer_dirty(path->nodes[0]);
  1344. btrfs_release_path(root, path);
  1345. pos += copy_size;
  1346. offset += copy_size;
  1347. num_written += copy_size;
  1348. write_bytes -= copy_size;
  1349. if (write_bytes)
  1350. goto again;
  1351. out:
  1352. btrfs_free_path(path);
  1353. ret = btrfs_end_transaction(trans, root);
  1354. BUG_ON(ret);
  1355. mutex_unlock(&root->fs_info->fs_mutex);
  1356. return num_written ? num_written : err;
  1357. }
  1358. static ssize_t btrfs_file_inline_write(struct file *file,
  1359. const char __user *buf,
  1360. size_t count, loff_t *ppos)
  1361. {
  1362. loff_t pos;
  1363. size_t num_written = 0;
  1364. int err = 0;
  1365. int ret = 0;
  1366. struct inode *inode = file->f_path.dentry->d_inode;
  1367. struct btrfs_root *root = BTRFS_I(inode)->root;
  1368. unsigned long page_index;
  1369. if (file->f_flags & O_DIRECT)
  1370. return -EINVAL;
  1371. pos = *ppos;
  1372. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1373. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  1374. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  1375. if (err)
  1376. goto out;
  1377. if (count == 0)
  1378. goto out;
  1379. err = remove_suid(file->f_path.dentry);
  1380. if (err)
  1381. goto out;
  1382. file_update_time(file);
  1383. mutex_lock(&inode->i_mutex);
  1384. while(count > 0) {
  1385. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  1386. size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
  1387. struct page *page;
  1388. page_index = pos >> PAGE_CACHE_SHIFT;
  1389. page = grab_cache_page(inode->i_mapping, page_index);
  1390. if (!PageUptodate(page)) {
  1391. ret = mpage_readpage(page, btrfs_get_block);
  1392. BUG_ON(ret);
  1393. lock_page(page);
  1394. }
  1395. ret = btrfs_copy_from_user(pos, 1,
  1396. write_bytes, &page, buf);
  1397. BUG_ON(ret);
  1398. write_bytes = inline_one_page(root, inode, page, pos,
  1399. offset, write_bytes);
  1400. SetPageUptodate(page);
  1401. if (write_bytes > 0 && pos + write_bytes > inode->i_size) {
  1402. i_size_write(inode, pos + write_bytes);
  1403. mark_inode_dirty(inode);
  1404. }
  1405. page_cache_release(page);
  1406. unlock_page(page);
  1407. if (write_bytes < 0)
  1408. goto out_unlock;
  1409. buf += write_bytes;
  1410. count -= write_bytes;
  1411. pos += write_bytes;
  1412. num_written += write_bytes;
  1413. balance_dirty_pages_ratelimited(inode->i_mapping);
  1414. cond_resched();
  1415. }
  1416. out_unlock:
  1417. mutex_unlock(&inode->i_mutex);
  1418. out:
  1419. *ppos = pos;
  1420. current->backing_dev_info = NULL;
  1421. return num_written ? num_written : err;
  1422. }
  1423. #endif
  1424. static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
  1425. unsigned long offset, unsigned long size)
  1426. {
  1427. char *kaddr;
  1428. unsigned long left, count = desc->count;
  1429. struct inode *inode = page->mapping->host;
  1430. if (size > count)
  1431. size = count;
  1432. if (!PageChecked(page)) {
  1433. /* FIXME, do it per block */
  1434. struct btrfs_root *root = BTRFS_I(inode)->root;
  1435. int ret = btrfs_csum_verify_file_block(root,
  1436. page->mapping->host->i_ino,
  1437. page->index << PAGE_CACHE_SHIFT,
  1438. kmap(page), PAGE_CACHE_SIZE);
  1439. if (ret) {
  1440. printk("failed to verify ino %lu page %lu\n",
  1441. page->mapping->host->i_ino,
  1442. page->index);
  1443. memset(page_address(page), 0, PAGE_CACHE_SIZE);
  1444. }
  1445. SetPageChecked(page);
  1446. kunmap(page);
  1447. }
  1448. /*
  1449. * Faults on the destination of a read are common, so do it before
  1450. * taking the kmap.
  1451. */
  1452. if (!fault_in_pages_writeable(desc->arg.buf, size)) {
  1453. kaddr = kmap_atomic(page, KM_USER0);
  1454. left = __copy_to_user_inatomic(desc->arg.buf,
  1455. kaddr + offset, size);
  1456. kunmap_atomic(kaddr, KM_USER0);
  1457. if (left == 0)
  1458. goto success;
  1459. }
  1460. /* Do it the slow way */
  1461. kaddr = kmap(page);
  1462. left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
  1463. kunmap(page);
  1464. if (left) {
  1465. size -= left;
  1466. desc->error = -EFAULT;
  1467. }
  1468. success:
  1469. desc->count = count - size;
  1470. desc->written += size;
  1471. desc->arg.buf += size;
  1472. return size;
  1473. }
  1474. /**
  1475. * btrfs_file_aio_read - filesystem read routine
  1476. * @iocb: kernel I/O control block
  1477. * @iov: io vector request
  1478. * @nr_segs: number of segments in the iovec
  1479. * @pos: current file position
  1480. */
  1481. static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
  1482. unsigned long nr_segs, loff_t pos)
  1483. {
  1484. struct file *filp = iocb->ki_filp;
  1485. ssize_t retval;
  1486. unsigned long seg;
  1487. size_t count;
  1488. loff_t *ppos = &iocb->ki_pos;
  1489. count = 0;
  1490. for (seg = 0; seg < nr_segs; seg++) {
  1491. const struct iovec *iv = &iov[seg];
  1492. /*
  1493. * If any segment has a negative length, or the cumulative
  1494. * length ever wraps negative then return -EINVAL.
  1495. */
  1496. count += iv->iov_len;
  1497. if (unlikely((ssize_t)(count|iv->iov_len) < 0))
  1498. return -EINVAL;
  1499. if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
  1500. continue;
  1501. if (seg == 0)
  1502. return -EFAULT;
  1503. nr_segs = seg;
  1504. count -= iv->iov_len; /* This segment is no good */
  1505. break;
  1506. }
  1507. retval = 0;
  1508. if (count) {
  1509. for (seg = 0; seg < nr_segs; seg++) {
  1510. read_descriptor_t desc;
  1511. desc.written = 0;
  1512. desc.arg.buf = iov[seg].iov_base;
  1513. desc.count = iov[seg].iov_len;
  1514. if (desc.count == 0)
  1515. continue;
  1516. desc.error = 0;
  1517. do_generic_file_read(filp, ppos, &desc,
  1518. btrfs_read_actor);
  1519. retval += desc.written;
  1520. if (desc.error) {
  1521. retval = retval ?: desc.error;
  1522. break;
  1523. }
  1524. }
  1525. }
  1526. return retval;
  1527. }
  1528. static int create_subvol(struct btrfs_root *root, char *name, int namelen)
  1529. {
  1530. struct btrfs_trans_handle *trans;
  1531. struct btrfs_key key;
  1532. struct btrfs_root_item root_item;
  1533. struct btrfs_inode_item *inode_item;
  1534. struct buffer_head *subvol;
  1535. struct btrfs_leaf *leaf;
  1536. struct btrfs_root *new_root;
  1537. struct inode *inode;
  1538. int ret;
  1539. u64 objectid;
  1540. u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
  1541. mutex_lock(&root->fs_info->fs_mutex);
  1542. trans = btrfs_start_transaction(root, 1);
  1543. BUG_ON(!trans);
  1544. subvol = btrfs_alloc_free_block(trans, root);
  1545. leaf = btrfs_buffer_leaf(subvol);
  1546. btrfs_set_header_nritems(&leaf->header, 0);
  1547. btrfs_set_header_level(&leaf->header, 0);
  1548. btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol));
  1549. btrfs_set_header_generation(&leaf->header, trans->transid);
  1550. memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
  1551. sizeof(leaf->header.fsid));
  1552. inode_item = &root_item.inode;
  1553. memset(inode_item, 0, sizeof(*inode_item));
  1554. btrfs_set_inode_generation(inode_item, 1);
  1555. btrfs_set_inode_size(inode_item, 3);
  1556. btrfs_set_inode_nlink(inode_item, 1);
  1557. btrfs_set_inode_nblocks(inode_item, 1);
  1558. btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
  1559. btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
  1560. btrfs_set_root_refs(&root_item, 1);
  1561. mark_buffer_dirty(subvol);
  1562. brelse(subvol);
  1563. subvol = NULL;
  1564. ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
  1565. 0, &objectid);
  1566. BUG_ON(ret);
  1567. btrfs_set_root_dirid(&root_item, new_dirid);
  1568. key.objectid = objectid;
  1569. key.offset = 1;
  1570. key.flags = 0;
  1571. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  1572. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  1573. &root_item);
  1574. BUG_ON(ret);
  1575. /*
  1576. * insert the directory item
  1577. */
  1578. key.offset = (u64)-1;
  1579. ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
  1580. name, namelen,
  1581. root->fs_info->sb->s_root->d_inode->i_ino,
  1582. &key, 0);
  1583. BUG_ON(ret);
  1584. ret = btrfs_commit_transaction(trans, root);
  1585. BUG_ON(ret);
  1586. new_root = btrfs_read_fs_root(root->fs_info, &key);
  1587. BUG_ON(!new_root);
  1588. trans = btrfs_start_transaction(new_root, 1);
  1589. BUG_ON(!trans);
  1590. inode = btrfs_new_inode(trans, new_root, new_dirid, S_IFDIR | 0700);
  1591. inode->i_op = &btrfs_dir_inode_operations;
  1592. inode->i_fop = &btrfs_dir_file_operations;
  1593. ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
  1594. BUG_ON(ret);
  1595. inode->i_nlink = 1;
  1596. inode->i_size = 6;
  1597. ret = btrfs_update_inode(trans, new_root, inode);
  1598. BUG_ON(ret);
  1599. ret = btrfs_commit_transaction(trans, new_root);
  1600. BUG_ON(ret);
  1601. iput(inode);
  1602. mutex_unlock(&root->fs_info->fs_mutex);
  1603. return 0;
  1604. }
  1605. static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
  1606. {
  1607. struct btrfs_trans_handle *trans;
  1608. struct btrfs_key key;
  1609. struct btrfs_root_item new_root_item;
  1610. int ret;
  1611. u64 objectid;
  1612. if (!root->ref_cows)
  1613. return -EINVAL;
  1614. mutex_lock(&root->fs_info->fs_mutex);
  1615. trans = btrfs_start_transaction(root, 1);
  1616. BUG_ON(!trans);
  1617. ret = btrfs_update_inode(trans, root, root->inode);
  1618. BUG_ON(ret);
  1619. ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
  1620. 0, &objectid);
  1621. BUG_ON(ret);
  1622. memcpy(&new_root_item, &root->root_item,
  1623. sizeof(new_root_item));
  1624. key.objectid = objectid;
  1625. key.offset = 1;
  1626. key.flags = 0;
  1627. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  1628. btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node));
  1629. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  1630. &new_root_item);
  1631. BUG_ON(ret);
  1632. printk("adding snapshot name %.*s root %Lu %Lu %u\n", namelen, name, key.objectid, key.offset, key.flags);
  1633. /*
  1634. * insert the directory item
  1635. */
  1636. key.offset = (u64)-1;
  1637. ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
  1638. name, namelen,
  1639. root->fs_info->sb->s_root->d_inode->i_ino,
  1640. &key, 0);
  1641. BUG_ON(ret);
  1642. ret = btrfs_inc_root_ref(trans, root);
  1643. BUG_ON(ret);
  1644. ret = btrfs_commit_transaction(trans, root);
  1645. BUG_ON(ret);
  1646. mutex_unlock(&root->fs_info->fs_mutex);
  1647. return 0;
  1648. }
  1649. static int add_disk(struct btrfs_root *root, char *name, int namelen)
  1650. {
  1651. struct block_device *bdev;
  1652. struct btrfs_path *path;
  1653. struct super_block *sb = root->fs_info->sb;
  1654. struct btrfs_root *dev_root = root->fs_info->dev_root;
  1655. struct btrfs_trans_handle *trans;
  1656. struct btrfs_device_item *dev_item;
  1657. struct btrfs_key key;
  1658. u16 item_size;
  1659. u64 num_blocks;
  1660. u64 new_blocks;
  1661. int ret;
  1662. printk("adding disk %s\n", name);
  1663. path = btrfs_alloc_path();
  1664. if (!path)
  1665. return -ENOMEM;
  1666. num_blocks = btrfs_super_total_blocks(root->fs_info->disk_super);
  1667. bdev = open_bdev_excl(name, O_RDWR, sb);
  1668. if (IS_ERR(bdev)) {
  1669. ret = PTR_ERR(bdev);
  1670. printk("open bdev excl failed ret %d\n", ret);
  1671. goto out_nolock;
  1672. }
  1673. set_blocksize(bdev, sb->s_blocksize);
  1674. new_blocks = bdev->bd_inode->i_size >> sb->s_blocksize_bits;
  1675. key.objectid = num_blocks;
  1676. key.offset = new_blocks;
  1677. key.flags = 0;
  1678. btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY);
  1679. mutex_lock(&dev_root->fs_info->fs_mutex);
  1680. trans = btrfs_start_transaction(dev_root, 1);
  1681. item_size = sizeof(*dev_item) + namelen;
  1682. printk("insert empty on %Lu %Lu %u size %d\n", num_blocks, new_blocks, key.flags, item_size);
  1683. ret = btrfs_insert_empty_item(trans, dev_root, path, &key, item_size);
  1684. if (ret) {
  1685. printk("insert failed %d\n", ret);
  1686. close_bdev_excl(bdev);
  1687. if (ret > 0)
  1688. ret = -EEXIST;
  1689. goto out;
  1690. }
  1691. dev_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  1692. path->slots[0], struct btrfs_device_item);
  1693. btrfs_set_device_pathlen(dev_item, namelen);
  1694. memcpy(dev_item + 1, name, namelen);
  1695. mark_buffer_dirty(path->nodes[0]);
  1696. ret = btrfs_insert_dev_radix(root, bdev, num_blocks, new_blocks);
  1697. if (!ret) {
  1698. btrfs_set_super_total_blocks(root->fs_info->disk_super,
  1699. num_blocks + new_blocks);
  1700. i_size_write(root->fs_info->btree_inode,
  1701. (num_blocks + new_blocks) <<
  1702. root->fs_info->btree_inode->i_blkbits);
  1703. }
  1704. out:
  1705. ret = btrfs_commit_transaction(trans, dev_root);
  1706. BUG_ON(ret);
  1707. mutex_unlock(&root->fs_info->fs_mutex);
  1708. out_nolock:
  1709. btrfs_free_path(path);
  1710. return ret;
  1711. }
  1712. static int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
  1713. cmd, unsigned long arg)
  1714. {
  1715. struct btrfs_root *root = BTRFS_I(inode)->root;
  1716. struct btrfs_ioctl_vol_args vol_args;
  1717. int ret = 0;
  1718. int namelen;
  1719. struct btrfs_path *path;
  1720. u64 root_dirid;
  1721. switch (cmd) {
  1722. case BTRFS_IOC_SNAP_CREATE:
  1723. if (copy_from_user(&vol_args,
  1724. (struct btrfs_ioctl_vol_args __user *)arg,
  1725. sizeof(vol_args)))
  1726. return -EFAULT;
  1727. namelen = strlen(vol_args.name);
  1728. if (namelen > BTRFS_VOL_NAME_MAX)
  1729. return -EINVAL;
  1730. path = btrfs_alloc_path();
  1731. if (!path)
  1732. return -ENOMEM;
  1733. root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
  1734. mutex_lock(&root->fs_info->fs_mutex);
  1735. ret = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
  1736. path, root_dirid,
  1737. vol_args.name, namelen, 0);
  1738. mutex_unlock(&root->fs_info->fs_mutex);
  1739. btrfs_free_path(path);
  1740. if (ret == 0)
  1741. return -EEXIST;
  1742. if (root == root->fs_info->tree_root)
  1743. ret = create_subvol(root, vol_args.name, namelen);
  1744. else
  1745. ret = create_snapshot(root, vol_args.name, namelen);
  1746. WARN_ON(ret);
  1747. break;
  1748. case BTRFS_IOC_ADD_DISK:
  1749. if (copy_from_user(&vol_args,
  1750. (struct btrfs_ioctl_vol_args __user *)arg,
  1751. sizeof(vol_args)))
  1752. return -EFAULT;
  1753. namelen = strlen(vol_args.name);
  1754. if (namelen > BTRFS_VOL_NAME_MAX)
  1755. return -EINVAL;
  1756. vol_args.name[namelen] = '\0';
  1757. ret = add_disk(root, vol_args.name, namelen);
  1758. break;
  1759. default:
  1760. return -ENOTTY;
  1761. }
  1762. return ret;
  1763. }
  1764. static struct kmem_cache *btrfs_inode_cachep;
  1765. struct kmem_cache *btrfs_trans_handle_cachep;
  1766. struct kmem_cache *btrfs_transaction_cachep;
  1767. struct kmem_cache *btrfs_bit_radix_cachep;
  1768. struct kmem_cache *btrfs_path_cachep;
  1769. /*
  1770. * Called inside transaction, so use GFP_NOFS
  1771. */
  1772. static struct inode *btrfs_alloc_inode(struct super_block *sb)
  1773. {
  1774. struct btrfs_inode *ei;
  1775. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  1776. if (!ei)
  1777. return NULL;
  1778. return &ei->vfs_inode;
  1779. }
  1780. static void btrfs_destroy_inode(struct inode *inode)
  1781. {
  1782. WARN_ON(!list_empty(&inode->i_dentry));
  1783. WARN_ON(inode->i_data.nrpages);
  1784. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  1785. }
  1786. static void init_once(void * foo, struct kmem_cache * cachep,
  1787. unsigned long flags)
  1788. {
  1789. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  1790. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  1791. SLAB_CTOR_CONSTRUCTOR) {
  1792. inode_init_once(&ei->vfs_inode);
  1793. }
  1794. }
  1795. static int init_inodecache(void)
  1796. {
  1797. btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
  1798. sizeof(struct btrfs_inode),
  1799. 0, (SLAB_RECLAIM_ACCOUNT|
  1800. SLAB_MEM_SPREAD),
  1801. init_once, NULL);
  1802. btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
  1803. sizeof(struct btrfs_trans_handle),
  1804. 0, (SLAB_RECLAIM_ACCOUNT|
  1805. SLAB_MEM_SPREAD),
  1806. NULL, NULL);
  1807. btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
  1808. sizeof(struct btrfs_transaction),
  1809. 0, (SLAB_RECLAIM_ACCOUNT|
  1810. SLAB_MEM_SPREAD),
  1811. NULL, NULL);
  1812. btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
  1813. sizeof(struct btrfs_transaction),
  1814. 0, (SLAB_RECLAIM_ACCOUNT|
  1815. SLAB_MEM_SPREAD),
  1816. NULL, NULL);
  1817. btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
  1818. 256,
  1819. 0, (SLAB_RECLAIM_ACCOUNT|
  1820. SLAB_MEM_SPREAD |
  1821. SLAB_DESTROY_BY_RCU),
  1822. NULL, NULL);
  1823. if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
  1824. btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
  1825. return -ENOMEM;
  1826. return 0;
  1827. }
  1828. static void destroy_inodecache(void)
  1829. {
  1830. kmem_cache_destroy(btrfs_inode_cachep);
  1831. kmem_cache_destroy(btrfs_trans_handle_cachep);
  1832. kmem_cache_destroy(btrfs_transaction_cachep);
  1833. kmem_cache_destroy(btrfs_bit_radix_cachep);
  1834. kmem_cache_destroy(btrfs_path_cachep);
  1835. }
  1836. static int btrfs_get_sb(struct file_system_type *fs_type,
  1837. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  1838. {
  1839. return get_sb_bdev(fs_type, flags, dev_name, data,
  1840. btrfs_fill_super, mnt);
  1841. }
  1842. static struct file_system_type btrfs_fs_type = {
  1843. .owner = THIS_MODULE,
  1844. .name = "btrfs",
  1845. .get_sb = btrfs_get_sb,
  1846. .kill_sb = kill_block_super,
  1847. .fs_flags = FS_REQUIRES_DEV,
  1848. };
  1849. static struct super_operations btrfs_super_ops = {
  1850. .statfs = simple_statfs,
  1851. .delete_inode = btrfs_delete_inode,
  1852. .put_super = btrfs_put_super,
  1853. .read_inode = btrfs_read_locked_inode,
  1854. .write_super = btrfs_write_super,
  1855. .sync_fs = btrfs_sync_fs,
  1856. .write_inode = btrfs_write_inode,
  1857. .alloc_inode = btrfs_alloc_inode,
  1858. .destroy_inode = btrfs_destroy_inode,
  1859. };
  1860. static struct inode_operations btrfs_dir_inode_operations = {
  1861. .lookup = btrfs_lookup,
  1862. .create = btrfs_create,
  1863. .unlink = btrfs_unlink,
  1864. .mkdir = btrfs_mkdir,
  1865. .rmdir = btrfs_rmdir,
  1866. };
  1867. static struct inode_operations btrfs_dir_ro_inode_operations = {
  1868. .lookup = btrfs_lookup,
  1869. };
  1870. static struct file_operations btrfs_dir_file_operations = {
  1871. .llseek = generic_file_llseek,
  1872. .read = generic_read_dir,
  1873. .readdir = btrfs_readdir,
  1874. .ioctl = btrfs_ioctl,
  1875. };
  1876. static struct address_space_operations btrfs_aops = {
  1877. .readpage = btrfs_readpage,
  1878. .writepage = btrfs_writepage,
  1879. .sync_page = block_sync_page,
  1880. .prepare_write = btrfs_prepare_write,
  1881. .commit_write = btrfs_commit_write,
  1882. };
  1883. static struct inode_operations btrfs_file_inode_operations = {
  1884. .truncate = btrfs_truncate,
  1885. };
  1886. static struct file_operations btrfs_file_operations = {
  1887. .llseek = generic_file_llseek,
  1888. .read = do_sync_read,
  1889. .aio_read = btrfs_file_aio_read,
  1890. .write = btrfs_file_write,
  1891. .mmap = generic_file_mmap,
  1892. .open = generic_file_open,
  1893. .ioctl = btrfs_ioctl,
  1894. };
  1895. static int __init init_btrfs_fs(void)
  1896. {
  1897. int err;
  1898. printk("btrfs loaded!\n");
  1899. err = init_inodecache();
  1900. if (err)
  1901. return err;
  1902. kset_set_kset_s(&btrfs_subsys, fs_subsys);
  1903. err = subsystem_register(&btrfs_subsys);
  1904. if (err)
  1905. goto out;
  1906. return register_filesystem(&btrfs_fs_type);
  1907. out:
  1908. destroy_inodecache();
  1909. return err;
  1910. }
  1911. static void __exit exit_btrfs_fs(void)
  1912. {
  1913. destroy_inodecache();
  1914. unregister_filesystem(&btrfs_fs_type);
  1915. subsystem_unregister(&btrfs_subsys);
  1916. printk("btrfs unloaded\n");
  1917. }
  1918. module_init(init_btrfs_fs)
  1919. module_exit(exit_btrfs_fs)
  1920. MODULE_LICENSE("GPL");