super.c 64 KB

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