super.c 64 KB

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