super.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333
  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_ITEM_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 (btrfs_disk_key_type(&item->key) != key_type)
  518. continue;
  519. if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
  520. continue;
  521. filp->f_pos = btrfs_disk_key_offset(&item->key);
  522. advance = 1;
  523. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  524. over = filldir(dirent, (const char *)(di + 1),
  525. btrfs_dir_name_len(di),
  526. btrfs_disk_key_offset(&item->key),
  527. btrfs_disk_key_objectid(&di->location), d_type);
  528. if (over)
  529. goto nopos;
  530. }
  531. filp->f_pos++;
  532. nopos:
  533. ret = 0;
  534. err:
  535. btrfs_release_path(root, path);
  536. btrfs_free_path(path);
  537. mutex_unlock(&root->fs_info->fs_mutex);
  538. return ret;
  539. }
  540. static void btrfs_put_super (struct super_block * sb)
  541. {
  542. struct btrfs_root *root = btrfs_sb(sb);
  543. int ret;
  544. ret = close_ctree(root);
  545. if (ret) {
  546. printk("close ctree returns %d\n", ret);
  547. }
  548. sb->s_fs_info = NULL;
  549. }
  550. static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
  551. {
  552. struct inode * inode;
  553. struct dentry * root_dentry;
  554. struct btrfs_super_block *disk_super;
  555. struct btrfs_root *tree_root;
  556. struct btrfs_inode *bi;
  557. sb->s_maxbytes = MAX_LFS_FILESIZE;
  558. sb->s_magic = BTRFS_SUPER_MAGIC;
  559. sb->s_op = &btrfs_super_ops;
  560. sb->s_time_gran = 1;
  561. tree_root = open_ctree(sb);
  562. if (!tree_root) {
  563. printk("btrfs: open_ctree failed\n");
  564. return -EIO;
  565. }
  566. sb->s_fs_info = tree_root;
  567. disk_super = tree_root->fs_info->disk_super;
  568. printk("read in super total blocks %Lu root %Lu\n",
  569. btrfs_super_total_blocks(disk_super),
  570. btrfs_super_root_dir(disk_super));
  571. inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
  572. tree_root);
  573. bi = BTRFS_I(inode);
  574. bi->location.objectid = inode->i_ino;
  575. bi->location.offset = 0;
  576. bi->location.flags = 0;
  577. bi->root = tree_root;
  578. btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
  579. if (!inode)
  580. return -ENOMEM;
  581. if (inode->i_state & I_NEW) {
  582. btrfs_read_locked_inode(inode);
  583. unlock_new_inode(inode);
  584. }
  585. root_dentry = d_alloc_root(inode);
  586. if (!root_dentry) {
  587. iput(inode);
  588. return -ENOMEM;
  589. }
  590. sb->s_root = root_dentry;
  591. return 0;
  592. }
  593. static void fill_inode_item(struct btrfs_inode_item *item,
  594. struct inode *inode)
  595. {
  596. btrfs_set_inode_uid(item, inode->i_uid);
  597. btrfs_set_inode_gid(item, inode->i_gid);
  598. btrfs_set_inode_size(item, inode->i_size);
  599. btrfs_set_inode_mode(item, inode->i_mode);
  600. btrfs_set_inode_nlink(item, inode->i_nlink);
  601. btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
  602. btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
  603. btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
  604. btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
  605. btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
  606. btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
  607. btrfs_set_inode_nblocks(item, inode->i_blocks);
  608. btrfs_set_inode_generation(item, inode->i_generation);
  609. }
  610. static int btrfs_update_inode(struct btrfs_trans_handle *trans,
  611. struct btrfs_root *root,
  612. struct inode *inode)
  613. {
  614. struct btrfs_inode_item *inode_item;
  615. struct btrfs_path *path;
  616. int ret;
  617. path = btrfs_alloc_path();
  618. BUG_ON(!path);
  619. btrfs_init_path(path);
  620. ret = btrfs_lookup_inode(trans, root, path,
  621. &BTRFS_I(inode)->location, 1);
  622. if (ret) {
  623. if (ret > 0)
  624. ret = -ENOENT;
  625. goto failed;
  626. }
  627. inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  628. path->slots[0],
  629. struct btrfs_inode_item);
  630. fill_inode_item(inode_item, inode);
  631. btrfs_mark_buffer_dirty(path->nodes[0]);
  632. ret = 0;
  633. failed:
  634. btrfs_release_path(root, path);
  635. btrfs_free_path(path);
  636. return ret;
  637. }
  638. static int btrfs_write_inode(struct inode *inode, int wait)
  639. {
  640. struct btrfs_root *root = BTRFS_I(inode)->root;
  641. struct btrfs_trans_handle *trans;
  642. int ret;
  643. mutex_lock(&root->fs_info->fs_mutex);
  644. trans = btrfs_start_transaction(root, 1);
  645. ret = btrfs_update_inode(trans, root, inode);
  646. if (wait)
  647. btrfs_commit_transaction(trans, root);
  648. else
  649. btrfs_end_transaction(trans, root);
  650. mutex_unlock(&root->fs_info->fs_mutex);
  651. return ret;
  652. }
  653. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  654. struct btrfs_root *root,
  655. u64 objectid, int mode)
  656. {
  657. struct inode *inode;
  658. struct btrfs_inode_item inode_item;
  659. struct btrfs_key *location;
  660. int ret;
  661. inode = new_inode(root->fs_info->sb);
  662. if (!inode)
  663. return ERR_PTR(-ENOMEM);
  664. BTRFS_I(inode)->root = root;
  665. inode->i_uid = current->fsuid;
  666. inode->i_gid = current->fsgid;
  667. inode->i_mode = mode;
  668. inode->i_ino = objectid;
  669. inode->i_blocks = 0;
  670. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  671. fill_inode_item(&inode_item, inode);
  672. location = &BTRFS_I(inode)->location;
  673. location->objectid = objectid;
  674. location->flags = 0;
  675. location->offset = 0;
  676. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  677. ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
  678. BUG_ON(ret);
  679. insert_inode_hash(inode);
  680. return inode;
  681. }
  682. static int btrfs_add_link(struct btrfs_trans_handle *trans,
  683. struct dentry *dentry, struct inode *inode)
  684. {
  685. int ret;
  686. struct btrfs_key key;
  687. struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
  688. key.objectid = inode->i_ino;
  689. key.flags = 0;
  690. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  691. key.offset = 0;
  692. ret = btrfs_insert_dir_item(trans, root,
  693. dentry->d_name.name, dentry->d_name.len,
  694. dentry->d_parent->d_inode->i_ino,
  695. &key, 0);
  696. if (ret == 0) {
  697. dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
  698. ret = btrfs_update_inode(trans, root,
  699. dentry->d_parent->d_inode);
  700. }
  701. return ret;
  702. }
  703. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  704. struct dentry *dentry, struct inode *inode)
  705. {
  706. int err = btrfs_add_link(trans, dentry, inode);
  707. if (!err) {
  708. d_instantiate(dentry, inode);
  709. return 0;
  710. }
  711. if (err > 0)
  712. err = -EEXIST;
  713. return err;
  714. }
  715. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  716. int mode, struct nameidata *nd)
  717. {
  718. struct btrfs_trans_handle *trans;
  719. struct btrfs_root *root = BTRFS_I(dir)->root;
  720. struct inode *inode;
  721. int err;
  722. int drop_inode = 0;
  723. u64 objectid;
  724. mutex_lock(&root->fs_info->fs_mutex);
  725. trans = btrfs_start_transaction(root, 1);
  726. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  727. if (err) {
  728. err = -ENOSPC;
  729. goto out_unlock;
  730. }
  731. inode = btrfs_new_inode(trans, root, objectid, mode);
  732. err = PTR_ERR(inode);
  733. if (IS_ERR(inode))
  734. goto out_unlock;
  735. // FIXME mark the inode dirty
  736. err = btrfs_add_nondir(trans, dentry, inode);
  737. if (err)
  738. drop_inode = 1;
  739. else {
  740. inode->i_mapping->a_ops = &btrfs_aops;
  741. inode->i_fop = &btrfs_file_operations;
  742. inode->i_op = &btrfs_file_inode_operations;
  743. }
  744. dir->i_sb->s_dirt = 1;
  745. out_unlock:
  746. btrfs_end_transaction(trans, root);
  747. mutex_unlock(&root->fs_info->fs_mutex);
  748. if (drop_inode) {
  749. inode_dec_link_count(inode);
  750. iput(inode);
  751. }
  752. return err;
  753. }
  754. static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
  755. struct btrfs_root *root,
  756. u64 objectid, u64 dirid)
  757. {
  758. int ret;
  759. char buf[2];
  760. struct btrfs_key key;
  761. buf[0] = '.';
  762. buf[1] = '.';
  763. key.objectid = objectid;
  764. key.offset = 0;
  765. key.flags = 0;
  766. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  767. ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
  768. &key, 1);
  769. if (ret)
  770. goto error;
  771. key.objectid = dirid;
  772. ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
  773. &key, 1);
  774. if (ret)
  775. goto error;
  776. error:
  777. return ret;
  778. }
  779. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  780. {
  781. struct inode *inode;
  782. struct btrfs_trans_handle *trans;
  783. struct btrfs_root *root = BTRFS_I(dir)->root;
  784. int err = 0;
  785. int drop_on_err = 0;
  786. u64 objectid;
  787. mutex_lock(&root->fs_info->fs_mutex);
  788. trans = btrfs_start_transaction(root, 1);
  789. if (IS_ERR(trans)) {
  790. err = PTR_ERR(trans);
  791. goto out_unlock;
  792. }
  793. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  794. if (err) {
  795. err = -ENOSPC;
  796. goto out_unlock;
  797. }
  798. inode = btrfs_new_inode(trans, root, objectid, S_IFDIR | mode);
  799. if (IS_ERR(inode)) {
  800. err = PTR_ERR(inode);
  801. goto out_fail;
  802. }
  803. drop_on_err = 1;
  804. inode->i_op = &btrfs_dir_inode_operations;
  805. inode->i_fop = &btrfs_dir_file_operations;
  806. err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
  807. if (err)
  808. goto out_fail;
  809. inode->i_size = 6;
  810. err = btrfs_update_inode(trans, root, inode);
  811. if (err)
  812. goto out_fail;
  813. err = btrfs_add_link(trans, dentry, inode);
  814. if (err)
  815. goto out_fail;
  816. d_instantiate(dentry, inode);
  817. drop_on_err = 0;
  818. out_fail:
  819. btrfs_end_transaction(trans, root);
  820. out_unlock:
  821. mutex_unlock(&root->fs_info->fs_mutex);
  822. if (drop_on_err)
  823. iput(inode);
  824. return err;
  825. }
  826. static int btrfs_sync_fs(struct super_block *sb, int wait)
  827. {
  828. struct btrfs_trans_handle *trans;
  829. struct btrfs_root *root;
  830. int ret;
  831. root = btrfs_sb(sb);
  832. sb->s_dirt = 0;
  833. if (!wait) {
  834. filemap_flush(root->fs_info->btree_inode->i_mapping);
  835. return 0;
  836. }
  837. filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
  838. mutex_lock(&root->fs_info->fs_mutex);
  839. trans = btrfs_start_transaction(root, 1);
  840. ret = btrfs_commit_transaction(trans, root);
  841. sb->s_dirt = 0;
  842. BUG_ON(ret);
  843. printk("btrfs sync_fs\n");
  844. mutex_unlock(&root->fs_info->fs_mutex);
  845. return 0;
  846. }
  847. #if 0
  848. static int btrfs_get_block_inline(struct inode *inode, sector_t iblock,
  849. struct buffer_head *result, int create)
  850. {
  851. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  852. struct btrfs_path *path;
  853. struct btrfs_key key;
  854. struct btrfs_leaf *leaf;
  855. int num_bytes = result->b_size;
  856. int item_size;
  857. int ret;
  858. u64 pos;
  859. char *ptr;
  860. int copy_size;
  861. int err = 0;
  862. char *safe_ptr;
  863. char *data_ptr;
  864. path = btrfs_alloc_path();
  865. BUG_ON(!path);
  866. WARN_ON(create);
  867. if (create) {
  868. return 0;
  869. }
  870. pos = iblock << inode->i_blkbits;
  871. key.objectid = inode->i_ino;
  872. key.flags = 0;
  873. btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
  874. ptr = kmap(result->b_page);
  875. safe_ptr = ptr;
  876. ptr += (pos & (PAGE_CACHE_SIZE -1));
  877. again:
  878. key.offset = pos;
  879. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  880. if (ret) {
  881. if (ret < 0)
  882. err = ret;
  883. else
  884. err = 0;
  885. goto out;
  886. }
  887. leaf = btrfs_buffer_leaf(path->nodes[0]);
  888. item_size = btrfs_item_size(leaf->items + path->slots[0]);
  889. copy_size = min(num_bytes, item_size);
  890. data_ptr = btrfs_item_ptr(leaf, path->slots[0], char);
  891. WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size);
  892. memcpy(ptr, data_ptr, copy_size);
  893. pos += copy_size;
  894. num_bytes -= copy_size;
  895. WARN_ON(num_bytes < 0);
  896. ptr += copy_size;
  897. btrfs_release_path(root, path);
  898. if (num_bytes != 0) {
  899. if (pos >= i_size_read(inode))
  900. memset(ptr, 0, num_bytes);
  901. else
  902. goto again;
  903. }
  904. set_buffer_uptodate(result);
  905. map_bh(result, inode->i_sb, 0);
  906. err = 0;
  907. out:
  908. btrfs_free_path(path);
  909. kunmap(result->b_page);
  910. return err;
  911. }
  912. #endif
  913. static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
  914. struct buffer_head *result, int create)
  915. {
  916. int ret;
  917. int err = 0;
  918. u64 blocknr;
  919. u64 extent_start = 0;
  920. u64 extent_end = 0;
  921. u64 objectid = inode->i_ino;
  922. struct btrfs_path *path;
  923. struct btrfs_root *root = BTRFS_I(inode)->root;
  924. struct btrfs_trans_handle *trans = NULL;
  925. struct btrfs_file_extent_item *item;
  926. struct btrfs_leaf *leaf;
  927. struct btrfs_disk_key *found_key;
  928. path = btrfs_alloc_path();
  929. BUG_ON(!path);
  930. btrfs_init_path(path);
  931. if (create) {
  932. trans = btrfs_start_transaction(root, 1);
  933. WARN_ON(1);
  934. }
  935. ret = btrfs_lookup_file_extent(trans, root, path,
  936. inode->i_ino,
  937. iblock << inode->i_blkbits, create);
  938. if (ret < 0) {
  939. err = ret;
  940. goto out;
  941. }
  942. if (ret != 0) {
  943. if (path->slots[0] == 0) {
  944. btrfs_release_path(root, path);
  945. goto allocate;
  946. }
  947. path->slots[0]--;
  948. }
  949. item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  950. struct btrfs_file_extent_item);
  951. leaf = btrfs_buffer_leaf(path->nodes[0]);
  952. blocknr = btrfs_file_extent_disk_blocknr(item);
  953. blocknr += btrfs_file_extent_offset(item);
  954. /* exact match found, use it, FIXME, deal with extents
  955. * other than the page size
  956. */
  957. if (0 && ret == 0) {
  958. err = 0;
  959. if (create &&
  960. btrfs_file_extent_generation(item) != trans->transid) {
  961. struct btrfs_key ins;
  962. ret = btrfs_alloc_extent(trans, root, 1,
  963. blocknr, (u64)-1, &ins);
  964. BUG_ON(ret);
  965. btrfs_set_file_extent_disk_blocknr(item, ins.objectid);
  966. mark_buffer_dirty(path->nodes[0]);
  967. ret = btrfs_free_extent(trans, root,
  968. blocknr, 1, 0);
  969. BUG_ON(ret);
  970. blocknr = ins.objectid;
  971. }
  972. btrfs_map_bh_to_logical(root, result, blocknr);
  973. goto out;
  974. }
  975. /* are we inside the extent that was found? */
  976. found_key = &leaf->items[path->slots[0]].key;
  977. if (btrfs_disk_key_objectid(found_key) != objectid ||
  978. btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
  979. extent_end = 0;
  980. extent_start = 0;
  981. btrfs_release_path(root, path);
  982. goto allocate;
  983. }
  984. extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
  985. extent_start = extent_start >> inode->i_blkbits;
  986. extent_end = extent_start + btrfs_file_extent_num_blocks(item);
  987. if (iblock >= extent_start && iblock < extent_end) {
  988. err = 0;
  989. btrfs_map_bh_to_logical(root, result, blocknr + iblock -
  990. extent_start);
  991. goto out;
  992. }
  993. allocate:
  994. /* ok, create a new extent */
  995. if (!create) {
  996. err = 0;
  997. goto out;
  998. }
  999. #if 0
  1000. ret = btrfs_alloc_file_extent(trans, root, objectid,
  1001. iblock << inode->i_blkbits,
  1002. 1, extent_end, &blocknr);
  1003. if (ret) {
  1004. err = ret;
  1005. goto out;
  1006. }
  1007. inode->i_blocks += inode->i_sb->s_blocksize >> 9;
  1008. set_buffer_new(result);
  1009. map_bh(result, inode->i_sb, blocknr);
  1010. btrfs_map_bh_to_logical(root, result, blocknr);
  1011. #endif
  1012. out:
  1013. btrfs_release_path(root, path);
  1014. btrfs_free_path(path);
  1015. if (trans)
  1016. btrfs_end_transaction(trans, root);
  1017. return err;
  1018. }
  1019. static int btrfs_get_block(struct inode *inode, sector_t iblock,
  1020. struct buffer_head *result, int create)
  1021. {
  1022. int err;
  1023. struct btrfs_root *root = BTRFS_I(inode)->root;
  1024. mutex_lock(&root->fs_info->fs_mutex);
  1025. err = btrfs_get_block_lock(inode, iblock, result, create);
  1026. // err = btrfs_get_block_inline(inode, iblock, result, create);
  1027. mutex_unlock(&root->fs_info->fs_mutex);
  1028. return err;
  1029. }
  1030. static int btrfs_prepare_write(struct file *file, struct page *page,
  1031. unsigned from, unsigned to)
  1032. {
  1033. return nobh_prepare_write(page, from, to, btrfs_get_block);
  1034. }
  1035. static int btrfs_commit_write(struct file *file, struct page *page,
  1036. unsigned from, unsigned to)
  1037. {
  1038. return nobh_commit_write(file, page, from, to);
  1039. }
  1040. static void btrfs_write_super(struct super_block *sb)
  1041. {
  1042. btrfs_sync_fs(sb, 1);
  1043. }
  1044. static int btrfs_readpage(struct file *file, struct page *page)
  1045. {
  1046. return mpage_readpage(page, btrfs_get_block);
  1047. }
  1048. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  1049. {
  1050. return nobh_writepage(page, btrfs_get_block, wbc);
  1051. }
  1052. static void btrfs_truncate(struct inode *inode)
  1053. {
  1054. struct btrfs_root *root = BTRFS_I(inode)->root;
  1055. int ret;
  1056. struct btrfs_trans_handle *trans;
  1057. if (!S_ISREG(inode->i_mode))
  1058. return;
  1059. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1060. return;
  1061. nobh_truncate_page(inode->i_mapping, inode->i_size);
  1062. /* FIXME, add redo link to tree so we don't leak on crash */
  1063. mutex_lock(&root->fs_info->fs_mutex);
  1064. trans = btrfs_start_transaction(root, 1);
  1065. ret = btrfs_truncate_in_trans(trans, root, inode);
  1066. BUG_ON(ret);
  1067. ret = btrfs_end_transaction(trans, root);
  1068. BUG_ON(ret);
  1069. mutex_unlock(&root->fs_info->fs_mutex);
  1070. mark_inode_dirty(inode);
  1071. }
  1072. static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
  1073. struct page **prepared_pages,
  1074. const char __user * buf)
  1075. {
  1076. long page_fault = 0;
  1077. int i;
  1078. int offset = pos & (PAGE_CACHE_SIZE - 1);
  1079. for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
  1080. size_t count = min_t(size_t,
  1081. PAGE_CACHE_SIZE - offset, write_bytes);
  1082. struct page *page = prepared_pages[i];
  1083. fault_in_pages_readable(buf, count);
  1084. /* Copy data from userspace to the current page */
  1085. kmap(page);
  1086. page_fault = __copy_from_user(page_address(page) + offset,
  1087. buf, count);
  1088. /* Flush processor's dcache for this page */
  1089. flush_dcache_page(page);
  1090. kunmap(page);
  1091. buf += count;
  1092. write_bytes -= count;
  1093. if (page_fault)
  1094. break;
  1095. }
  1096. return page_fault ? -EFAULT : 0;
  1097. }
  1098. static void btrfs_drop_pages(struct page **pages, size_t num_pages)
  1099. {
  1100. size_t i;
  1101. for (i = 0; i < num_pages; i++) {
  1102. if (!pages[i])
  1103. break;
  1104. unlock_page(pages[i]);
  1105. mark_page_accessed(pages[i]);
  1106. page_cache_release(pages[i]);
  1107. }
  1108. }
  1109. static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
  1110. struct btrfs_root *root,
  1111. struct file *file,
  1112. struct page **pages,
  1113. size_t num_pages,
  1114. loff_t pos,
  1115. size_t write_bytes)
  1116. {
  1117. int i;
  1118. int offset;
  1119. int err = 0;
  1120. int ret;
  1121. int this_write;
  1122. struct inode *inode = file->f_path.dentry->d_inode;
  1123. for (i = 0; i < num_pages; i++) {
  1124. offset = pos & (PAGE_CACHE_SIZE -1);
  1125. this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
  1126. /* FIXME, one block at a time */
  1127. mutex_lock(&root->fs_info->fs_mutex);
  1128. trans = btrfs_start_transaction(root, 1);
  1129. btrfs_csum_file_block(trans, root, inode->i_ino,
  1130. pages[i]->index << PAGE_CACHE_SHIFT,
  1131. kmap(pages[i]), PAGE_CACHE_SIZE);
  1132. kunmap(pages[i]);
  1133. SetPageChecked(pages[i]);
  1134. ret = btrfs_end_transaction(trans, root);
  1135. BUG_ON(ret);
  1136. mutex_unlock(&root->fs_info->fs_mutex);
  1137. ret = nobh_commit_write(file, pages[i], offset,
  1138. offset + this_write);
  1139. pos += this_write;
  1140. if (ret) {
  1141. err = ret;
  1142. goto failed;
  1143. }
  1144. WARN_ON(this_write > write_bytes);
  1145. write_bytes -= this_write;
  1146. }
  1147. failed:
  1148. return err;
  1149. }
  1150. static int drop_extents(struct btrfs_trans_handle *trans,
  1151. struct btrfs_root *root,
  1152. struct inode *inode,
  1153. u64 start, u64 end)
  1154. {
  1155. int ret;
  1156. struct btrfs_key key;
  1157. struct btrfs_leaf *leaf;
  1158. int slot;
  1159. struct btrfs_file_extent_item *extent;
  1160. u64 extent_end;
  1161. int keep;
  1162. struct btrfs_file_extent_item old;
  1163. struct btrfs_path *path;
  1164. u64 search_start = start;
  1165. int bookend;
  1166. path = btrfs_alloc_path();
  1167. if (!path)
  1168. return -ENOMEM;
  1169. search_again:
  1170. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  1171. search_start, -1);
  1172. if (ret < 0)
  1173. goto out;
  1174. if (ret > 0) {
  1175. if (path->slots[0] == 0) {
  1176. ret = -ENOENT;
  1177. goto out;
  1178. }
  1179. path->slots[0]--;
  1180. }
  1181. while(1) {
  1182. keep = 0;
  1183. bookend = 0;
  1184. leaf = btrfs_buffer_leaf(path->nodes[0]);
  1185. slot = path->slots[0];
  1186. btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
  1187. extent = btrfs_item_ptr(leaf, slot,
  1188. struct btrfs_file_extent_item);
  1189. extent_end = key.offset +
  1190. (btrfs_file_extent_num_blocks(extent) <<
  1191. inode->i_blkbits);
  1192. if (key.offset >= end || key.objectid != inode->i_ino) {
  1193. ret = 0;
  1194. goto out;
  1195. }
  1196. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  1197. goto next_leaf;
  1198. if (end < extent_end && end >= key.offset) {
  1199. memcpy(&old, extent, sizeof(old));
  1200. ret = btrfs_inc_extent_ref(trans, root,
  1201. btrfs_file_extent_disk_blocknr(&old),
  1202. btrfs_file_extent_disk_num_blocks(&old));
  1203. BUG_ON(ret);
  1204. bookend = 1;
  1205. }
  1206. if (start > key.offset) {
  1207. u64 new_num;
  1208. /* truncate existing extent */
  1209. keep = 1;
  1210. WARN_ON(start & (root->blocksize - 1));
  1211. new_num = (start - key.offset) >> inode->i_blkbits;
  1212. btrfs_set_file_extent_num_blocks(extent, new_num);
  1213. mark_buffer_dirty(path->nodes[0]);
  1214. }
  1215. if (!keep) {
  1216. u64 disk_blocknr;
  1217. u64 disk_num_blocks;
  1218. disk_blocknr = btrfs_file_extent_disk_blocknr(extent);
  1219. disk_num_blocks =
  1220. btrfs_file_extent_disk_num_blocks(extent);
  1221. search_start = key.offset +
  1222. (btrfs_file_extent_num_blocks(extent) <<
  1223. inode->i_blkbits);
  1224. ret = btrfs_del_item(trans, root, path);
  1225. BUG_ON(ret);
  1226. btrfs_release_path(root, path);
  1227. ret = btrfs_free_extent(trans, root, disk_blocknr,
  1228. disk_num_blocks, 0);
  1229. BUG_ON(ret);
  1230. if (!bookend && search_start >= end) {
  1231. ret = 0;
  1232. goto out;
  1233. }
  1234. if (!bookend)
  1235. goto search_again;
  1236. }
  1237. if (bookend) {
  1238. /* create bookend */
  1239. struct btrfs_key ins;
  1240. ins.objectid = inode->i_ino;
  1241. ins.offset = end;
  1242. ins.flags = 0;
  1243. btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
  1244. btrfs_release_path(root, path);
  1245. ret = btrfs_insert_empty_item(trans, root, path, &ins,
  1246. sizeof(*extent));
  1247. BUG_ON(ret);
  1248. extent = btrfs_item_ptr(
  1249. btrfs_buffer_leaf(path->nodes[0]),
  1250. path->slots[0],
  1251. struct btrfs_file_extent_item);
  1252. btrfs_set_file_extent_disk_blocknr(extent,
  1253. btrfs_file_extent_disk_blocknr(&old));
  1254. btrfs_set_file_extent_disk_num_blocks(extent,
  1255. btrfs_file_extent_disk_num_blocks(&old));
  1256. btrfs_set_file_extent_offset(extent,
  1257. btrfs_file_extent_offset(&old) +
  1258. ((end - key.offset) >> inode->i_blkbits));
  1259. WARN_ON(btrfs_file_extent_num_blocks(&old) <
  1260. (end - key.offset) >> inode->i_blkbits);
  1261. btrfs_set_file_extent_num_blocks(extent,
  1262. btrfs_file_extent_num_blocks(&old) -
  1263. ((end - key.offset) >> inode->i_blkbits));
  1264. btrfs_set_file_extent_generation(extent,
  1265. btrfs_file_extent_generation(&old));
  1266. btrfs_mark_buffer_dirty(path->nodes[0]);
  1267. ret = 0;
  1268. goto out;
  1269. }
  1270. next_leaf:
  1271. if (slot >= btrfs_header_nritems(&leaf->header) - 1) {
  1272. ret = btrfs_next_leaf(root, path);
  1273. if (ret) {
  1274. ret = 0;
  1275. goto out;
  1276. }
  1277. } else {
  1278. path->slots[0]++;
  1279. }
  1280. }
  1281. out:
  1282. btrfs_free_path(path);
  1283. return ret;
  1284. }
  1285. static int prepare_pages(struct btrfs_root *root,
  1286. struct file *file,
  1287. struct page **pages,
  1288. size_t num_pages,
  1289. loff_t pos,
  1290. unsigned long first_index,
  1291. unsigned long last_index,
  1292. size_t write_bytes,
  1293. u64 alloc_extent_start)
  1294. {
  1295. int i;
  1296. unsigned long index = pos >> PAGE_CACHE_SHIFT;
  1297. struct inode *inode = file->f_path.dentry->d_inode;
  1298. int offset;
  1299. int err = 0;
  1300. int this_write;
  1301. struct buffer_head *bh;
  1302. struct buffer_head *head;
  1303. loff_t isize = i_size_read(inode);
  1304. memset(pages, 0, num_pages * sizeof(struct page *));
  1305. for (i = 0; i < num_pages; i++) {
  1306. pages[i] = grab_cache_page(inode->i_mapping, index + i);
  1307. if (!pages[i]) {
  1308. err = -ENOMEM;
  1309. goto failed_release;
  1310. }
  1311. offset = pos & (PAGE_CACHE_SIZE -1);
  1312. this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
  1313. #if 0
  1314. if ((pages[i]->index == first_index ||
  1315. pages[i]->index == last_index) && pos < isize &&
  1316. !PageUptodate(pages[i])) {
  1317. ret = mpage_readpage(pages[i], btrfs_get_block);
  1318. BUG_ON(ret);
  1319. lock_page(pages[i]);
  1320. }
  1321. #endif
  1322. create_empty_buffers(pages[i], root->fs_info->sb->s_blocksize,
  1323. (1 << BH_Uptodate));
  1324. head = page_buffers(pages[i]);
  1325. bh = head;
  1326. do {
  1327. err = btrfs_map_bh_to_logical(root, bh,
  1328. alloc_extent_start);
  1329. BUG_ON(err);
  1330. if (err)
  1331. goto failed_truncate;
  1332. bh = bh->b_this_page;
  1333. alloc_extent_start++;
  1334. } while (bh != head);
  1335. pos += this_write;
  1336. WARN_ON(this_write > write_bytes);
  1337. write_bytes -= this_write;
  1338. }
  1339. return 0;
  1340. failed_release:
  1341. btrfs_drop_pages(pages, num_pages);
  1342. return err;
  1343. failed_truncate:
  1344. btrfs_drop_pages(pages, num_pages);
  1345. if (pos > isize)
  1346. vmtruncate(inode, isize);
  1347. return err;
  1348. }
  1349. static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
  1350. size_t count, loff_t *ppos)
  1351. {
  1352. loff_t pos;
  1353. size_t num_written = 0;
  1354. int err = 0;
  1355. int ret = 0;
  1356. struct inode *inode = file->f_path.dentry->d_inode;
  1357. struct btrfs_root *root = BTRFS_I(inode)->root;
  1358. struct page *pages[8];
  1359. unsigned long first_index;
  1360. unsigned long last_index;
  1361. u64 start_pos;
  1362. u64 num_blocks;
  1363. u64 alloc_extent_start;
  1364. struct btrfs_trans_handle *trans;
  1365. struct btrfs_key ins;
  1366. if (file->f_flags & O_DIRECT)
  1367. return -EINVAL;
  1368. pos = *ppos;
  1369. start_pos = pos & ~(root->blocksize - 1);
  1370. /* FIXME */
  1371. if (start_pos != pos)
  1372. return -EINVAL;
  1373. num_blocks = (count + pos - start_pos + root->blocksize - 1) >>
  1374. inode->i_blkbits;
  1375. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1376. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  1377. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  1378. if (err)
  1379. goto out;
  1380. if (count == 0)
  1381. goto out;
  1382. err = remove_suid(file->f_path.dentry);
  1383. if (err)
  1384. goto out;
  1385. file_update_time(file);
  1386. mutex_lock(&inode->i_mutex);
  1387. first_index = pos >> PAGE_CACHE_SHIFT;
  1388. last_index = (pos + count) >> PAGE_CACHE_SHIFT;
  1389. mutex_lock(&root->fs_info->fs_mutex);
  1390. trans = btrfs_start_transaction(root, 1);
  1391. if (!trans) {
  1392. err = -ENOMEM;
  1393. mutex_unlock(&root->fs_info->fs_mutex);
  1394. goto out_unlock;
  1395. }
  1396. if (start_pos < inode->i_size) {
  1397. ret = drop_extents(trans, root, inode,
  1398. start_pos,
  1399. (pos + count + root->blocksize -1) &
  1400. ~(root->blocksize - 1));
  1401. }
  1402. ret = btrfs_alloc_extent(trans, root, num_blocks, 1,
  1403. (u64)-1, &ins);
  1404. BUG_ON(ret);
  1405. ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
  1406. start_pos, ins.objectid, ins.offset);
  1407. BUG_ON(ret);
  1408. alloc_extent_start = ins.objectid;
  1409. ret = btrfs_end_transaction(trans, root);
  1410. mutex_unlock(&root->fs_info->fs_mutex);
  1411. while(count > 0) {
  1412. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  1413. size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
  1414. size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
  1415. PAGE_CACHE_SHIFT;
  1416. memset(pages, 0, sizeof(pages));
  1417. ret = prepare_pages(root, file, pages, num_pages,
  1418. pos, first_index, last_index,
  1419. write_bytes, alloc_extent_start);
  1420. BUG_ON(ret);
  1421. /* FIXME blocks != pagesize */
  1422. alloc_extent_start += num_pages;
  1423. ret = btrfs_copy_from_user(pos, num_pages,
  1424. write_bytes, pages, buf);
  1425. BUG_ON(ret);
  1426. ret = dirty_and_release_pages(NULL, root, file, pages,
  1427. num_pages, pos, write_bytes);
  1428. BUG_ON(ret);
  1429. btrfs_drop_pages(pages, num_pages);
  1430. buf += write_bytes;
  1431. count -= write_bytes;
  1432. pos += write_bytes;
  1433. num_written += write_bytes;
  1434. balance_dirty_pages_ratelimited(inode->i_mapping);
  1435. cond_resched();
  1436. }
  1437. out_unlock:
  1438. mutex_unlock(&inode->i_mutex);
  1439. out:
  1440. *ppos = pos;
  1441. current->backing_dev_info = NULL;
  1442. return num_written ? num_written : err;
  1443. }
  1444. #if 0
  1445. static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode,
  1446. struct page *page, loff_t pos,
  1447. size_t offset, size_t write_bytes)
  1448. {
  1449. struct btrfs_path *path;
  1450. struct btrfs_trans_handle *trans;
  1451. struct btrfs_key key;
  1452. struct btrfs_leaf *leaf;
  1453. struct btrfs_key found_key;
  1454. int ret;
  1455. size_t copy_size = 0;
  1456. char *dst = NULL;
  1457. int err = 0;
  1458. size_t num_written = 0;
  1459. path = btrfs_alloc_path();
  1460. BUG_ON(!path);
  1461. mutex_lock(&root->fs_info->fs_mutex);
  1462. trans = btrfs_start_transaction(root, 1);
  1463. key.objectid = inode->i_ino;
  1464. key.flags = 0;
  1465. btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
  1466. again:
  1467. key.offset = pos;
  1468. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1469. if (ret < 0) {
  1470. err = ret;
  1471. goto out;
  1472. }
  1473. if (ret == 0) {
  1474. leaf = btrfs_buffer_leaf(path->nodes[0]);
  1475. btrfs_disk_key_to_cpu(&found_key,
  1476. &leaf->items[path->slots[0]].key);
  1477. copy_size = btrfs_item_size(leaf->items + path->slots[0]);
  1478. dst = btrfs_item_ptr(leaf, path->slots[0], char);
  1479. copy_size = min(write_bytes, copy_size);
  1480. goto copyit;
  1481. } else {
  1482. int slot = path->slots[0];
  1483. if (slot > 0) {
  1484. slot--;
  1485. }
  1486. // FIXME find max key
  1487. leaf = btrfs_buffer_leaf(path->nodes[0]);
  1488. btrfs_disk_key_to_cpu(&found_key,
  1489. &leaf->items[slot].key);
  1490. if (found_key.objectid != inode->i_ino)
  1491. goto insert;
  1492. if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY)
  1493. goto insert;
  1494. copy_size = btrfs_item_size(leaf->items + slot);
  1495. if (found_key.offset + copy_size <= pos)
  1496. goto insert;
  1497. dst = btrfs_item_ptr(leaf, path->slots[0], char);
  1498. dst += pos - found_key.offset;
  1499. copy_size = copy_size - (pos - found_key.offset);
  1500. BUG_ON(copy_size < 0);
  1501. copy_size = min(write_bytes, copy_size);
  1502. WARN_ON(copy_size == 0);
  1503. goto copyit;
  1504. }
  1505. insert:
  1506. btrfs_release_path(root, path);
  1507. copy_size = min(write_bytes,
  1508. (size_t)BTRFS_LEAF_DATA_SIZE(root) -
  1509. sizeof(struct btrfs_item) * 4);
  1510. ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size);
  1511. BUG_ON(ret);
  1512. dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  1513. path->slots[0], char);
  1514. copyit:
  1515. WARN_ON(copy_size == 0);
  1516. WARN_ON(dst + copy_size >
  1517. btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  1518. path->slots[0], char) +
  1519. btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items +
  1520. path->slots[0]));
  1521. btrfs_memcpy(root, path->nodes[0]->b_data, dst,
  1522. page_address(page) + offset, copy_size);
  1523. mark_buffer_dirty(path->nodes[0]);
  1524. btrfs_release_path(root, path);
  1525. pos += copy_size;
  1526. offset += copy_size;
  1527. num_written += copy_size;
  1528. write_bytes -= copy_size;
  1529. if (write_bytes)
  1530. goto again;
  1531. out:
  1532. btrfs_free_path(path);
  1533. ret = btrfs_end_transaction(trans, root);
  1534. BUG_ON(ret);
  1535. mutex_unlock(&root->fs_info->fs_mutex);
  1536. return num_written ? num_written : err;
  1537. }
  1538. static ssize_t btrfs_file_inline_write(struct file *file,
  1539. const char __user *buf,
  1540. size_t count, loff_t *ppos)
  1541. {
  1542. loff_t pos;
  1543. size_t num_written = 0;
  1544. int err = 0;
  1545. int ret = 0;
  1546. struct inode *inode = file->f_path.dentry->d_inode;
  1547. struct btrfs_root *root = BTRFS_I(inode)->root;
  1548. unsigned long page_index;
  1549. if (file->f_flags & O_DIRECT)
  1550. return -EINVAL;
  1551. pos = *ppos;
  1552. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1553. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  1554. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  1555. if (err)
  1556. goto out;
  1557. if (count == 0)
  1558. goto out;
  1559. err = remove_suid(file->f_path.dentry);
  1560. if (err)
  1561. goto out;
  1562. file_update_time(file);
  1563. mutex_lock(&inode->i_mutex);
  1564. while(count > 0) {
  1565. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  1566. size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
  1567. struct page *page;
  1568. page_index = pos >> PAGE_CACHE_SHIFT;
  1569. page = grab_cache_page(inode->i_mapping, page_index);
  1570. if (!PageUptodate(page)) {
  1571. ret = mpage_readpage(page, btrfs_get_block);
  1572. BUG_ON(ret);
  1573. lock_page(page);
  1574. }
  1575. ret = btrfs_copy_from_user(pos, 1,
  1576. write_bytes, &page, buf);
  1577. BUG_ON(ret);
  1578. write_bytes = inline_one_page(root, inode, page, pos,
  1579. offset, write_bytes);
  1580. SetPageUptodate(page);
  1581. if (write_bytes > 0 && pos + write_bytes > inode->i_size) {
  1582. i_size_write(inode, pos + write_bytes);
  1583. mark_inode_dirty(inode);
  1584. }
  1585. page_cache_release(page);
  1586. unlock_page(page);
  1587. if (write_bytes < 0)
  1588. goto out_unlock;
  1589. buf += write_bytes;
  1590. count -= write_bytes;
  1591. pos += write_bytes;
  1592. num_written += write_bytes;
  1593. balance_dirty_pages_ratelimited(inode->i_mapping);
  1594. cond_resched();
  1595. }
  1596. out_unlock:
  1597. mutex_unlock(&inode->i_mutex);
  1598. out:
  1599. *ppos = pos;
  1600. current->backing_dev_info = NULL;
  1601. return num_written ? num_written : err;
  1602. }
  1603. #endif
  1604. static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
  1605. unsigned long offset, unsigned long size)
  1606. {
  1607. char *kaddr;
  1608. unsigned long left, count = desc->count;
  1609. struct inode *inode = page->mapping->host;
  1610. if (size > count)
  1611. size = count;
  1612. if (!PageChecked(page)) {
  1613. /* FIXME, do it per block */
  1614. struct btrfs_root *root = BTRFS_I(inode)->root;
  1615. int ret = btrfs_csum_verify_file_block(root,
  1616. page->mapping->host->i_ino,
  1617. page->index << PAGE_CACHE_SHIFT,
  1618. kmap(page), PAGE_CACHE_SIZE);
  1619. if (ret) {
  1620. printk("failed to verify ino %lu page %lu\n",
  1621. page->mapping->host->i_ino,
  1622. page->index);
  1623. memset(page_address(page), 0, PAGE_CACHE_SIZE);
  1624. }
  1625. SetPageChecked(page);
  1626. kunmap(page);
  1627. }
  1628. /*
  1629. * Faults on the destination of a read are common, so do it before
  1630. * taking the kmap.
  1631. */
  1632. if (!fault_in_pages_writeable(desc->arg.buf, size)) {
  1633. kaddr = kmap_atomic(page, KM_USER0);
  1634. left = __copy_to_user_inatomic(desc->arg.buf,
  1635. kaddr + offset, size);
  1636. kunmap_atomic(kaddr, KM_USER0);
  1637. if (left == 0)
  1638. goto success;
  1639. }
  1640. /* Do it the slow way */
  1641. kaddr = kmap(page);
  1642. left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
  1643. kunmap(page);
  1644. if (left) {
  1645. size -= left;
  1646. desc->error = -EFAULT;
  1647. }
  1648. success:
  1649. desc->count = count - size;
  1650. desc->written += size;
  1651. desc->arg.buf += size;
  1652. return size;
  1653. }
  1654. /**
  1655. * btrfs_file_aio_read - filesystem read routine
  1656. * @iocb: kernel I/O control block
  1657. * @iov: io vector request
  1658. * @nr_segs: number of segments in the iovec
  1659. * @pos: current file position
  1660. */
  1661. static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
  1662. unsigned long nr_segs, loff_t pos)
  1663. {
  1664. struct file *filp = iocb->ki_filp;
  1665. ssize_t retval;
  1666. unsigned long seg;
  1667. size_t count;
  1668. loff_t *ppos = &iocb->ki_pos;
  1669. count = 0;
  1670. for (seg = 0; seg < nr_segs; seg++) {
  1671. const struct iovec *iv = &iov[seg];
  1672. /*
  1673. * If any segment has a negative length, or the cumulative
  1674. * length ever wraps negative then return -EINVAL.
  1675. */
  1676. count += iv->iov_len;
  1677. if (unlikely((ssize_t)(count|iv->iov_len) < 0))
  1678. return -EINVAL;
  1679. if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
  1680. continue;
  1681. if (seg == 0)
  1682. return -EFAULT;
  1683. nr_segs = seg;
  1684. count -= iv->iov_len; /* This segment is no good */
  1685. break;
  1686. }
  1687. retval = 0;
  1688. if (count) {
  1689. for (seg = 0; seg < nr_segs; seg++) {
  1690. read_descriptor_t desc;
  1691. desc.written = 0;
  1692. desc.arg.buf = iov[seg].iov_base;
  1693. desc.count = iov[seg].iov_len;
  1694. if (desc.count == 0)
  1695. continue;
  1696. desc.error = 0;
  1697. do_generic_file_read(filp, ppos, &desc,
  1698. btrfs_read_actor);
  1699. retval += desc.written;
  1700. if (desc.error) {
  1701. retval = retval ?: desc.error;
  1702. break;
  1703. }
  1704. }
  1705. }
  1706. return retval;
  1707. }
  1708. static int create_subvol(struct btrfs_root *root, char *name, int namelen)
  1709. {
  1710. struct btrfs_trans_handle *trans;
  1711. struct btrfs_key key;
  1712. struct btrfs_root_item root_item;
  1713. struct btrfs_inode_item *inode_item;
  1714. struct buffer_head *subvol;
  1715. struct btrfs_leaf *leaf;
  1716. struct btrfs_root *new_root;
  1717. struct inode *inode;
  1718. int ret;
  1719. u64 objectid;
  1720. u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
  1721. mutex_lock(&root->fs_info->fs_mutex);
  1722. trans = btrfs_start_transaction(root, 1);
  1723. BUG_ON(!trans);
  1724. subvol = btrfs_alloc_free_block(trans, root);
  1725. leaf = btrfs_buffer_leaf(subvol);
  1726. btrfs_set_header_nritems(&leaf->header, 0);
  1727. btrfs_set_header_level(&leaf->header, 0);
  1728. btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol));
  1729. btrfs_set_header_generation(&leaf->header, trans->transid);
  1730. memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
  1731. sizeof(leaf->header.fsid));
  1732. inode_item = &root_item.inode;
  1733. memset(inode_item, 0, sizeof(*inode_item));
  1734. btrfs_set_inode_generation(inode_item, 1);
  1735. btrfs_set_inode_size(inode_item, 3);
  1736. btrfs_set_inode_nlink(inode_item, 1);
  1737. btrfs_set_inode_nblocks(inode_item, 1);
  1738. btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
  1739. btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
  1740. btrfs_set_root_refs(&root_item, 1);
  1741. mark_buffer_dirty(subvol);
  1742. brelse(subvol);
  1743. subvol = NULL;
  1744. ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
  1745. 0, &objectid);
  1746. BUG_ON(ret);
  1747. btrfs_set_root_dirid(&root_item, new_dirid);
  1748. key.objectid = objectid;
  1749. key.offset = 1;
  1750. key.flags = 0;
  1751. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  1752. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  1753. &root_item);
  1754. BUG_ON(ret);
  1755. /*
  1756. * insert the directory item
  1757. */
  1758. key.offset = (u64)-1;
  1759. ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
  1760. name, namelen,
  1761. root->fs_info->sb->s_root->d_inode->i_ino,
  1762. &key, 0);
  1763. BUG_ON(ret);
  1764. ret = btrfs_commit_transaction(trans, root);
  1765. BUG_ON(ret);
  1766. new_root = btrfs_read_fs_root(root->fs_info, &key);
  1767. BUG_ON(!new_root);
  1768. trans = btrfs_start_transaction(new_root, 1);
  1769. BUG_ON(!trans);
  1770. inode = btrfs_new_inode(trans, new_root, new_dirid, S_IFDIR | 0700);
  1771. inode->i_op = &btrfs_dir_inode_operations;
  1772. inode->i_fop = &btrfs_dir_file_operations;
  1773. ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
  1774. BUG_ON(ret);
  1775. inode->i_nlink = 1;
  1776. inode->i_size = 6;
  1777. ret = btrfs_update_inode(trans, new_root, inode);
  1778. BUG_ON(ret);
  1779. ret = btrfs_commit_transaction(trans, new_root);
  1780. BUG_ON(ret);
  1781. iput(inode);
  1782. mutex_unlock(&root->fs_info->fs_mutex);
  1783. return 0;
  1784. }
  1785. static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
  1786. {
  1787. struct btrfs_trans_handle *trans;
  1788. struct btrfs_key key;
  1789. struct btrfs_root_item new_root_item;
  1790. int ret;
  1791. u64 objectid;
  1792. if (!root->ref_cows)
  1793. return -EINVAL;
  1794. mutex_lock(&root->fs_info->fs_mutex);
  1795. trans = btrfs_start_transaction(root, 1);
  1796. BUG_ON(!trans);
  1797. ret = btrfs_update_inode(trans, root, root->inode);
  1798. BUG_ON(ret);
  1799. ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
  1800. 0, &objectid);
  1801. BUG_ON(ret);
  1802. memcpy(&new_root_item, &root->root_item,
  1803. sizeof(new_root_item));
  1804. key.objectid = objectid;
  1805. key.offset = 1;
  1806. key.flags = 0;
  1807. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  1808. btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node));
  1809. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  1810. &new_root_item);
  1811. BUG_ON(ret);
  1812. /*
  1813. * insert the directory item
  1814. */
  1815. key.offset = (u64)-1;
  1816. ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
  1817. name, namelen,
  1818. root->fs_info->sb->s_root->d_inode->i_ino,
  1819. &key, 0);
  1820. BUG_ON(ret);
  1821. ret = btrfs_inc_root_ref(trans, root);
  1822. BUG_ON(ret);
  1823. ret = btrfs_commit_transaction(trans, root);
  1824. BUG_ON(ret);
  1825. mutex_unlock(&root->fs_info->fs_mutex);
  1826. return 0;
  1827. }
  1828. static int add_disk(struct btrfs_root *root, char *name, int namelen)
  1829. {
  1830. struct block_device *bdev;
  1831. struct btrfs_path *path;
  1832. struct super_block *sb = root->fs_info->sb;
  1833. struct btrfs_root *dev_root = root->fs_info->dev_root;
  1834. struct btrfs_trans_handle *trans;
  1835. struct btrfs_device_item *dev_item;
  1836. struct btrfs_key key;
  1837. u16 item_size;
  1838. u64 num_blocks;
  1839. u64 new_blocks;
  1840. u64 device_id;
  1841. int ret;
  1842. printk("adding disk %s\n", name);
  1843. path = btrfs_alloc_path();
  1844. if (!path)
  1845. return -ENOMEM;
  1846. num_blocks = btrfs_super_total_blocks(root->fs_info->disk_super);
  1847. bdev = open_bdev_excl(name, O_RDWR, sb);
  1848. if (IS_ERR(bdev)) {
  1849. ret = PTR_ERR(bdev);
  1850. printk("open bdev excl failed ret %d\n", ret);
  1851. goto out_nolock;
  1852. }
  1853. set_blocksize(bdev, sb->s_blocksize);
  1854. new_blocks = bdev->bd_inode->i_size >> sb->s_blocksize_bits;
  1855. key.objectid = num_blocks;
  1856. key.offset = new_blocks;
  1857. key.flags = 0;
  1858. btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY);
  1859. mutex_lock(&dev_root->fs_info->fs_mutex);
  1860. trans = btrfs_start_transaction(dev_root, 1);
  1861. item_size = sizeof(*dev_item) + namelen;
  1862. printk("insert empty on %Lu %Lu %u size %d\n", num_blocks, new_blocks, key.flags, item_size);
  1863. ret = btrfs_insert_empty_item(trans, dev_root, path, &key, item_size);
  1864. if (ret) {
  1865. printk("insert failed %d\n", ret);
  1866. close_bdev_excl(bdev);
  1867. if (ret > 0)
  1868. ret = -EEXIST;
  1869. goto out;
  1870. }
  1871. dev_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  1872. path->slots[0], struct btrfs_device_item);
  1873. btrfs_set_device_pathlen(dev_item, namelen);
  1874. memcpy(dev_item + 1, name, namelen);
  1875. device_id = btrfs_super_last_device_id(root->fs_info->disk_super) + 1;
  1876. btrfs_set_super_last_device_id(root->fs_info->disk_super, device_id);
  1877. btrfs_set_device_id(dev_item, device_id);
  1878. mark_buffer_dirty(path->nodes[0]);
  1879. ret = btrfs_insert_dev_radix(root, bdev, device_id, num_blocks,
  1880. new_blocks);
  1881. if (!ret) {
  1882. btrfs_set_super_total_blocks(root->fs_info->disk_super,
  1883. num_blocks + new_blocks);
  1884. i_size_write(root->fs_info->btree_inode,
  1885. (num_blocks + new_blocks) <<
  1886. root->fs_info->btree_inode->i_blkbits);
  1887. }
  1888. out:
  1889. ret = btrfs_commit_transaction(trans, dev_root);
  1890. BUG_ON(ret);
  1891. mutex_unlock(&root->fs_info->fs_mutex);
  1892. out_nolock:
  1893. btrfs_free_path(path);
  1894. return ret;
  1895. }
  1896. static int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
  1897. cmd, unsigned long arg)
  1898. {
  1899. struct btrfs_root *root = BTRFS_I(inode)->root;
  1900. struct btrfs_ioctl_vol_args vol_args;
  1901. int ret = 0;
  1902. int namelen;
  1903. struct btrfs_path *path;
  1904. u64 root_dirid;
  1905. switch (cmd) {
  1906. case BTRFS_IOC_SNAP_CREATE:
  1907. if (copy_from_user(&vol_args,
  1908. (struct btrfs_ioctl_vol_args __user *)arg,
  1909. sizeof(vol_args)))
  1910. return -EFAULT;
  1911. namelen = strlen(vol_args.name);
  1912. if (namelen > BTRFS_VOL_NAME_MAX)
  1913. return -EINVAL;
  1914. path = btrfs_alloc_path();
  1915. if (!path)
  1916. return -ENOMEM;
  1917. root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
  1918. mutex_lock(&root->fs_info->fs_mutex);
  1919. ret = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
  1920. path, root_dirid,
  1921. vol_args.name, namelen, 0);
  1922. mutex_unlock(&root->fs_info->fs_mutex);
  1923. btrfs_free_path(path);
  1924. if (ret == 0)
  1925. return -EEXIST;
  1926. if (root == root->fs_info->tree_root)
  1927. ret = create_subvol(root, vol_args.name, namelen);
  1928. else
  1929. ret = create_snapshot(root, vol_args.name, namelen);
  1930. WARN_ON(ret);
  1931. break;
  1932. case BTRFS_IOC_ADD_DISK:
  1933. if (copy_from_user(&vol_args,
  1934. (struct btrfs_ioctl_vol_args __user *)arg,
  1935. sizeof(vol_args)))
  1936. return -EFAULT;
  1937. namelen = strlen(vol_args.name);
  1938. if (namelen > BTRFS_VOL_NAME_MAX)
  1939. return -EINVAL;
  1940. vol_args.name[namelen] = '\0';
  1941. ret = add_disk(root, vol_args.name, namelen);
  1942. break;
  1943. default:
  1944. return -ENOTTY;
  1945. }
  1946. return ret;
  1947. }
  1948. static struct kmem_cache *btrfs_inode_cachep;
  1949. struct kmem_cache *btrfs_trans_handle_cachep;
  1950. struct kmem_cache *btrfs_transaction_cachep;
  1951. struct kmem_cache *btrfs_bit_radix_cachep;
  1952. struct kmem_cache *btrfs_path_cachep;
  1953. /*
  1954. * Called inside transaction, so use GFP_NOFS
  1955. */
  1956. static struct inode *btrfs_alloc_inode(struct super_block *sb)
  1957. {
  1958. struct btrfs_inode *ei;
  1959. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  1960. if (!ei)
  1961. return NULL;
  1962. return &ei->vfs_inode;
  1963. }
  1964. static void btrfs_destroy_inode(struct inode *inode)
  1965. {
  1966. WARN_ON(!list_empty(&inode->i_dentry));
  1967. WARN_ON(inode->i_data.nrpages);
  1968. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  1969. }
  1970. static void init_once(void * foo, struct kmem_cache * cachep,
  1971. unsigned long flags)
  1972. {
  1973. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  1974. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  1975. SLAB_CTOR_CONSTRUCTOR) {
  1976. inode_init_once(&ei->vfs_inode);
  1977. }
  1978. }
  1979. static int init_inodecache(void)
  1980. {
  1981. btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
  1982. sizeof(struct btrfs_inode),
  1983. 0, (SLAB_RECLAIM_ACCOUNT|
  1984. SLAB_MEM_SPREAD),
  1985. init_once, NULL);
  1986. btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
  1987. sizeof(struct btrfs_trans_handle),
  1988. 0, (SLAB_RECLAIM_ACCOUNT|
  1989. SLAB_MEM_SPREAD),
  1990. NULL, NULL);
  1991. btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
  1992. sizeof(struct btrfs_transaction),
  1993. 0, (SLAB_RECLAIM_ACCOUNT|
  1994. SLAB_MEM_SPREAD),
  1995. NULL, NULL);
  1996. btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
  1997. sizeof(struct btrfs_transaction),
  1998. 0, (SLAB_RECLAIM_ACCOUNT|
  1999. SLAB_MEM_SPREAD),
  2000. NULL, NULL);
  2001. btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
  2002. 256,
  2003. 0, (SLAB_RECLAIM_ACCOUNT|
  2004. SLAB_MEM_SPREAD |
  2005. SLAB_DESTROY_BY_RCU),
  2006. NULL, NULL);
  2007. if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
  2008. btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
  2009. return -ENOMEM;
  2010. return 0;
  2011. }
  2012. static void destroy_inodecache(void)
  2013. {
  2014. kmem_cache_destroy(btrfs_inode_cachep);
  2015. kmem_cache_destroy(btrfs_trans_handle_cachep);
  2016. kmem_cache_destroy(btrfs_transaction_cachep);
  2017. kmem_cache_destroy(btrfs_bit_radix_cachep);
  2018. kmem_cache_destroy(btrfs_path_cachep);
  2019. }
  2020. static int btrfs_get_sb(struct file_system_type *fs_type,
  2021. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  2022. {
  2023. return get_sb_bdev(fs_type, flags, dev_name, data,
  2024. btrfs_fill_super, mnt);
  2025. }
  2026. static struct file_system_type btrfs_fs_type = {
  2027. .owner = THIS_MODULE,
  2028. .name = "btrfs",
  2029. .get_sb = btrfs_get_sb,
  2030. .kill_sb = kill_block_super,
  2031. .fs_flags = FS_REQUIRES_DEV,
  2032. };
  2033. static struct super_operations btrfs_super_ops = {
  2034. .statfs = simple_statfs,
  2035. .delete_inode = btrfs_delete_inode,
  2036. .put_super = btrfs_put_super,
  2037. .read_inode = btrfs_read_locked_inode,
  2038. .write_super = btrfs_write_super,
  2039. .sync_fs = btrfs_sync_fs,
  2040. .write_inode = btrfs_write_inode,
  2041. .alloc_inode = btrfs_alloc_inode,
  2042. .destroy_inode = btrfs_destroy_inode,
  2043. };
  2044. static struct inode_operations btrfs_dir_inode_operations = {
  2045. .lookup = btrfs_lookup,
  2046. .create = btrfs_create,
  2047. .unlink = btrfs_unlink,
  2048. .mkdir = btrfs_mkdir,
  2049. .rmdir = btrfs_rmdir,
  2050. };
  2051. static struct inode_operations btrfs_dir_ro_inode_operations = {
  2052. .lookup = btrfs_lookup,
  2053. };
  2054. static struct file_operations btrfs_dir_file_operations = {
  2055. .llseek = generic_file_llseek,
  2056. .read = generic_read_dir,
  2057. .readdir = btrfs_readdir,
  2058. .ioctl = btrfs_ioctl,
  2059. };
  2060. static struct address_space_operations btrfs_aops = {
  2061. .readpage = btrfs_readpage,
  2062. .writepage = btrfs_writepage,
  2063. .sync_page = block_sync_page,
  2064. .prepare_write = btrfs_prepare_write,
  2065. .commit_write = btrfs_commit_write,
  2066. };
  2067. static struct inode_operations btrfs_file_inode_operations = {
  2068. .truncate = btrfs_truncate,
  2069. };
  2070. static struct file_operations btrfs_file_operations = {
  2071. .llseek = generic_file_llseek,
  2072. .read = do_sync_read,
  2073. .aio_read = btrfs_file_aio_read,
  2074. .write = btrfs_file_write,
  2075. .mmap = generic_file_mmap,
  2076. .open = generic_file_open,
  2077. .ioctl = btrfs_ioctl,
  2078. };
  2079. static int __init init_btrfs_fs(void)
  2080. {
  2081. int err;
  2082. printk("btrfs loaded!\n");
  2083. err = init_inodecache();
  2084. if (err)
  2085. return err;
  2086. kset_set_kset_s(&btrfs_subsys, fs_subsys);
  2087. err = subsystem_register(&btrfs_subsys);
  2088. if (err)
  2089. goto out;
  2090. return register_filesystem(&btrfs_fs_type);
  2091. out:
  2092. destroy_inodecache();
  2093. return err;
  2094. }
  2095. static void __exit exit_btrfs_fs(void)
  2096. {
  2097. destroy_inodecache();
  2098. unregister_filesystem(&btrfs_fs_type);
  2099. subsystem_unregister(&btrfs_subsys);
  2100. printk("btrfs unloaded\n");
  2101. }
  2102. module_init(init_btrfs_fs)
  2103. module_exit(exit_btrfs_fs)
  2104. MODULE_LICENSE("GPL");