super.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463
  1. #include <linux/module.h>
  2. #include <linux/buffer_head.h>
  3. #include <linux/fs.h>
  4. #include <linux/pagemap.h>
  5. #include <linux/highmem.h>
  6. #include <linux/time.h>
  7. #include <linux/init.h>
  8. #include <linux/string.h>
  9. #include <linux/smp_lock.h>
  10. #include <linux/backing-dev.h>
  11. #include <linux/mpage.h>
  12. #include <linux/swap.h>
  13. #include <linux/writeback.h>
  14. #include "ctree.h"
  15. #include "disk-io.h"
  16. #include "transaction.h"
  17. #include "btrfs_inode.h"
  18. #define BTRFS_SUPER_MAGIC 0x9123682E
  19. static struct inode_operations btrfs_dir_inode_operations;
  20. static struct super_operations btrfs_super_ops;
  21. static struct file_operations btrfs_dir_file_operations;
  22. static struct inode_operations btrfs_file_inode_operations;
  23. static struct address_space_operations btrfs_aops;
  24. static struct file_operations btrfs_file_operations;
  25. static int check_inode(struct inode *inode)
  26. {
  27. struct btrfs_inode *ei = BTRFS_I(inode);
  28. WARN_ON(ei->magic != 0xDEADBEEF);
  29. WARN_ON(ei->magic2 != 0xDEADBEAF);
  30. return 0;
  31. }
  32. static void btrfs_read_locked_inode(struct inode *inode)
  33. {
  34. struct btrfs_path *path;
  35. struct btrfs_inode_item *inode_item;
  36. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  37. int ret;
  38. path = btrfs_alloc_path();
  39. BUG_ON(!path);
  40. btrfs_init_path(path);
  41. mutex_lock(&root->fs_info->fs_mutex);
  42. check_inode(inode);
  43. ret = btrfs_lookup_inode(NULL, root, path, inode->i_ino, 0);
  44. if (ret) {
  45. btrfs_release_path(root, path);
  46. btrfs_free_path(path);
  47. mutex_unlock(&root->fs_info->fs_mutex);
  48. make_bad_inode(inode);
  49. return;
  50. }
  51. check_inode(inode);
  52. inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  53. path->slots[0],
  54. struct btrfs_inode_item);
  55. inode->i_mode = btrfs_inode_mode(inode_item);
  56. inode->i_nlink = btrfs_inode_nlink(inode_item);
  57. inode->i_uid = btrfs_inode_uid(inode_item);
  58. inode->i_gid = btrfs_inode_gid(inode_item);
  59. inode->i_size = btrfs_inode_size(inode_item);
  60. inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
  61. inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
  62. inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
  63. inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
  64. inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
  65. inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
  66. inode->i_blocks = btrfs_inode_nblocks(inode_item);
  67. inode->i_generation = btrfs_inode_generation(inode_item);
  68. btrfs_release_path(root, path);
  69. btrfs_free_path(path);
  70. inode_item = NULL;
  71. mutex_unlock(&root->fs_info->fs_mutex);
  72. check_inode(inode);
  73. switch (inode->i_mode & S_IFMT) {
  74. #if 0
  75. default:
  76. init_special_inode(inode, inode->i_mode,
  77. btrfs_inode_rdev(inode_item));
  78. break;
  79. #endif
  80. case S_IFREG:
  81. inode->i_mapping->a_ops = &btrfs_aops;
  82. inode->i_fop = &btrfs_file_operations;
  83. inode->i_op = &btrfs_file_inode_operations;
  84. break;
  85. case S_IFDIR:
  86. inode->i_op = &btrfs_dir_inode_operations;
  87. inode->i_fop = &btrfs_dir_file_operations;
  88. break;
  89. case S_IFLNK:
  90. // inode->i_op = &page_symlink_inode_operations;
  91. break;
  92. }
  93. check_inode(inode);
  94. return;
  95. }
  96. static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
  97. struct btrfs_root *root,
  98. struct inode *dir,
  99. struct dentry *dentry)
  100. {
  101. struct btrfs_path *path;
  102. const char *name = dentry->d_name.name;
  103. int name_len = dentry->d_name.len;
  104. int ret;
  105. u64 objectid;
  106. struct btrfs_dir_item *di;
  107. path = btrfs_alloc_path();
  108. BUG_ON(!path);
  109. btrfs_init_path(path);
  110. ret = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  111. name, name_len, -1);
  112. if (ret < 0)
  113. goto err;
  114. if (ret > 0) {
  115. ret = -ENOENT;
  116. goto err;
  117. }
  118. di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  119. struct btrfs_dir_item);
  120. objectid = btrfs_dir_objectid(di);
  121. ret = btrfs_del_item(trans, root, path);
  122. BUG_ON(ret);
  123. dentry->d_inode->i_ctime = dir->i_ctime;
  124. err:
  125. btrfs_release_path(root, path);
  126. btrfs_free_path(path);
  127. if (ret == 0)
  128. inode_dec_link_count(dentry->d_inode);
  129. return ret;
  130. }
  131. static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
  132. {
  133. struct btrfs_root *root;
  134. struct btrfs_trans_handle *trans;
  135. int ret;
  136. root = btrfs_sb(dir->i_sb);
  137. mutex_lock(&root->fs_info->fs_mutex);
  138. trans = btrfs_start_transaction(root, 1);
  139. ret = btrfs_unlink_trans(trans, root, dir, dentry);
  140. btrfs_end_transaction(trans, root);
  141. mutex_unlock(&root->fs_info->fs_mutex);
  142. return ret;
  143. }
  144. static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
  145. {
  146. struct inode *inode = dentry->d_inode;
  147. int err;
  148. int ret;
  149. struct btrfs_root *root = btrfs_sb(dir->i_sb);
  150. struct btrfs_path *path;
  151. struct btrfs_key key;
  152. struct btrfs_trans_handle *trans;
  153. struct btrfs_disk_key *found_key;
  154. struct btrfs_leaf *leaf;
  155. path = btrfs_alloc_path();
  156. BUG_ON(!path);
  157. btrfs_init_path(path);
  158. mutex_lock(&root->fs_info->fs_mutex);
  159. trans = btrfs_start_transaction(root, 1);
  160. key.objectid = inode->i_ino;
  161. key.offset = (u64)-1;
  162. key.flags = 0;
  163. btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
  164. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  165. if (ret < 0) {
  166. err = ret;
  167. goto out;
  168. }
  169. BUG_ON(ret == 0);
  170. BUG_ON(path->slots[0] == 0);
  171. path->slots[0]--;
  172. leaf = btrfs_buffer_leaf(path->nodes[0]);
  173. found_key = &leaf->items[path->slots[0]].key;
  174. if (btrfs_disk_key_objectid(found_key) != inode->i_ino) {
  175. err = -ENOENT;
  176. goto out;
  177. }
  178. if (btrfs_disk_key_type(found_key) != BTRFS_DIR_ITEM_KEY ||
  179. btrfs_disk_key_offset(found_key) != 2) {
  180. err = -ENOTEMPTY;
  181. goto out;
  182. }
  183. ret = btrfs_del_item(trans, root, path);
  184. BUG_ON(ret);
  185. btrfs_release_path(root, path);
  186. key.offset = 1;
  187. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  188. if (ret < 0) {
  189. err = ret;
  190. goto out;
  191. }
  192. if (ret > 0) {
  193. err = -ENOTEMPTY;
  194. goto out;
  195. }
  196. ret = btrfs_del_item(trans, root, path);
  197. if (ret) {
  198. err = ret;
  199. goto out;
  200. }
  201. btrfs_release_path(root, path);
  202. /* now the directory is empty */
  203. err = btrfs_unlink_trans(trans, root, dir, dentry);
  204. if (!err) {
  205. inode->i_size = 0;
  206. }
  207. out:
  208. btrfs_release_path(root, path);
  209. btrfs_free_path(path);
  210. mutex_unlock(&root->fs_info->fs_mutex);
  211. ret = btrfs_end_transaction(trans, root);
  212. if (ret && !err)
  213. err = ret;
  214. return err;
  215. }
  216. static int btrfs_free_inode(struct btrfs_trans_handle *trans,
  217. struct btrfs_root *root,
  218. struct inode *inode)
  219. {
  220. u64 objectid = inode->i_ino;
  221. struct btrfs_path *path;
  222. struct btrfs_inode_map_item *map;
  223. struct btrfs_key stat_data_key;
  224. int ret;
  225. clear_inode(inode);
  226. path = btrfs_alloc_path();
  227. BUG_ON(!path);
  228. btrfs_init_path(path);
  229. ret = btrfs_lookup_inode_map(trans, root, path, objectid, -1);
  230. if (ret) {
  231. if (ret > 0)
  232. ret = -ENOENT;
  233. goto error;
  234. }
  235. map = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  236. struct btrfs_inode_map_item);
  237. btrfs_disk_key_to_cpu(&stat_data_key, &map->key);
  238. ret = btrfs_del_item(trans, root->fs_info->inode_root, path);
  239. BUG_ON(ret);
  240. btrfs_release_path(root, path);
  241. ret = btrfs_lookup_inode(trans, root, path, objectid, -1);
  242. BUG_ON(ret);
  243. ret = btrfs_del_item(trans, root, path);
  244. BUG_ON(ret);
  245. error:
  246. btrfs_release_path(root, path);
  247. btrfs_free_path(path);
  248. return ret;
  249. }
  250. static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
  251. struct btrfs_root *root,
  252. struct inode *inode)
  253. {
  254. int ret;
  255. struct btrfs_path *path;
  256. struct btrfs_key key;
  257. struct btrfs_disk_key *found_key;
  258. struct btrfs_leaf *leaf;
  259. struct btrfs_file_extent_item *fi = NULL;
  260. u64 extent_start = 0;
  261. u64 extent_num_blocks = 0;
  262. int found_extent;
  263. path = btrfs_alloc_path();
  264. BUG_ON(!path);
  265. /* FIXME, add redo link to tree so we don't leak on crash */
  266. key.objectid = inode->i_ino;
  267. key.offset = (u64)-1;
  268. key.flags = 0;
  269. btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY);
  270. while(1) {
  271. btrfs_init_path(path);
  272. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  273. if (ret < 0) {
  274. goto error;
  275. }
  276. if (ret > 0) {
  277. BUG_ON(path->slots[0] == 0);
  278. path->slots[0]--;
  279. }
  280. leaf = btrfs_buffer_leaf(path->nodes[0]);
  281. found_key = &leaf->items[path->slots[0]].key;
  282. if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
  283. break;
  284. if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY &&
  285. btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY)
  286. break;
  287. if (btrfs_disk_key_offset(found_key) < inode->i_size)
  288. break;
  289. if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) {
  290. fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  291. path->slots[0],
  292. struct btrfs_file_extent_item);
  293. extent_start = btrfs_file_extent_disk_blocknr(fi);
  294. extent_num_blocks =
  295. btrfs_file_extent_disk_num_blocks(fi);
  296. inode->i_blocks -=
  297. btrfs_file_extent_num_blocks(fi) >> 9;
  298. found_extent = 1;
  299. } else {
  300. found_extent = 0;
  301. }
  302. ret = btrfs_del_item(trans, root, path);
  303. BUG_ON(ret);
  304. btrfs_release_path(root, path);
  305. if (found_extent) {
  306. ret = btrfs_free_extent(trans, root, extent_start,
  307. extent_num_blocks, 0);
  308. BUG_ON(ret);
  309. }
  310. }
  311. ret = 0;
  312. error:
  313. btrfs_release_path(root, path);
  314. btrfs_free_path(path);
  315. return ret;
  316. }
  317. static void btrfs_delete_inode(struct inode *inode)
  318. {
  319. struct btrfs_trans_handle *trans;
  320. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  321. int ret;
  322. truncate_inode_pages(&inode->i_data, 0);
  323. if (is_bad_inode(inode)) {
  324. goto no_delete;
  325. }
  326. inode->i_size = 0;
  327. mutex_lock(&root->fs_info->fs_mutex);
  328. trans = btrfs_start_transaction(root, 1);
  329. if (S_ISREG(inode->i_mode)) {
  330. ret = btrfs_truncate_in_trans(trans, root, inode);
  331. BUG_ON(ret);
  332. }
  333. btrfs_free_inode(trans, root, inode);
  334. btrfs_end_transaction(trans, root);
  335. mutex_unlock(&root->fs_info->fs_mutex);
  336. return;
  337. no_delete:
  338. clear_inode(inode);
  339. }
  340. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  341. ino_t *ino)
  342. {
  343. const char *name = dentry->d_name.name;
  344. int namelen = dentry->d_name.len;
  345. struct btrfs_dir_item *di;
  346. struct btrfs_path *path;
  347. struct btrfs_root *root = btrfs_sb(dir->i_sb);
  348. int ret;
  349. path = btrfs_alloc_path();
  350. BUG_ON(!path);
  351. btrfs_init_path(path);
  352. ret = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
  353. namelen, 0);
  354. if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) {
  355. *ino = 0;
  356. ret = 0;
  357. goto out;
  358. }
  359. di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  360. struct btrfs_dir_item);
  361. *ino = btrfs_dir_objectid(di);
  362. out:
  363. btrfs_release_path(root, path);
  364. btrfs_free_path(path);
  365. check_inode(dir);
  366. return ret;
  367. }
  368. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  369. struct nameidata *nd)
  370. {
  371. struct inode * inode;
  372. struct btrfs_root *root = btrfs_sb(dir->i_sb);
  373. ino_t ino;
  374. int ret;
  375. if (dentry->d_name.len > BTRFS_NAME_LEN)
  376. return ERR_PTR(-ENAMETOOLONG);
  377. mutex_lock(&root->fs_info->fs_mutex);
  378. ret = btrfs_inode_by_name(dir, dentry, &ino);
  379. mutex_unlock(&root->fs_info->fs_mutex);
  380. if (ret < 0)
  381. return ERR_PTR(ret);
  382. inode = NULL;
  383. if (ino) {
  384. inode = iget(dir->i_sb, ino);
  385. if (!inode)
  386. return ERR_PTR(-EACCES);
  387. check_inode(inode);
  388. }
  389. check_inode(dir);
  390. return d_splice_alias(inode, dentry);
  391. }
  392. static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
  393. {
  394. struct inode *inode = filp->f_path.dentry->d_inode;
  395. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  396. struct btrfs_item *item;
  397. struct btrfs_dir_item *di;
  398. struct btrfs_key key;
  399. struct btrfs_path *path;
  400. int ret;
  401. u32 nritems;
  402. struct btrfs_leaf *leaf;
  403. int slot;
  404. int advance;
  405. unsigned char d_type = DT_UNKNOWN;
  406. int over = 0;
  407. mutex_lock(&root->fs_info->fs_mutex);
  408. key.objectid = inode->i_ino;
  409. key.flags = 0;
  410. btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
  411. key.offset = filp->f_pos;
  412. path = btrfs_alloc_path();
  413. btrfs_init_path(path);
  414. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  415. if (ret < 0) {
  416. goto err;
  417. }
  418. advance = 0;
  419. while(1) {
  420. leaf = btrfs_buffer_leaf(path->nodes[0]);
  421. nritems = btrfs_header_nritems(&leaf->header);
  422. slot = path->slots[0];
  423. if (advance || slot >= nritems) {
  424. if (slot >= nritems -1) {
  425. ret = btrfs_next_leaf(root, path);
  426. if (ret)
  427. break;
  428. leaf = btrfs_buffer_leaf(path->nodes[0]);
  429. nritems = btrfs_header_nritems(&leaf->header);
  430. slot = path->slots[0];
  431. } else {
  432. slot++;
  433. path->slots[0]++;
  434. }
  435. }
  436. advance = 1;
  437. item = leaf->items + slot;
  438. if (btrfs_disk_key_objectid(&item->key) != key.objectid)
  439. break;
  440. if (btrfs_disk_key_type(&item->key) != BTRFS_DIR_ITEM_KEY)
  441. continue;
  442. if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
  443. continue;
  444. advance = 1;
  445. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  446. over = filldir(dirent, (const char *)(di + 1),
  447. btrfs_dir_name_len(di),
  448. btrfs_disk_key_offset(&item->key),
  449. btrfs_dir_objectid(di), d_type);
  450. if (over) {
  451. filp->f_pos = btrfs_disk_key_offset(&item->key);
  452. break;
  453. }
  454. filp->f_pos = btrfs_disk_key_offset(&item->key) + 1;
  455. }
  456. ret = 0;
  457. err:
  458. btrfs_release_path(root, path);
  459. btrfs_free_path(path);
  460. mutex_unlock(&root->fs_info->fs_mutex);
  461. return ret;
  462. }
  463. static void btrfs_put_super (struct super_block * sb)
  464. {
  465. struct btrfs_root *root = btrfs_sb(sb);
  466. int ret;
  467. ret = close_ctree(root);
  468. if (ret) {
  469. printk("close ctree returns %d\n", ret);
  470. }
  471. sb->s_fs_info = NULL;
  472. }
  473. static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
  474. {
  475. struct inode * inode;
  476. struct dentry * root_dentry;
  477. struct btrfs_super_block *disk_super;
  478. struct btrfs_root *root;
  479. sb->s_maxbytes = MAX_LFS_FILESIZE;
  480. sb->s_magic = BTRFS_SUPER_MAGIC;
  481. sb->s_op = &btrfs_super_ops;
  482. sb->s_time_gran = 1;
  483. root = open_ctree(sb);
  484. if (!root) {
  485. printk("btrfs: open_ctree failed\n");
  486. return -EIO;
  487. }
  488. sb->s_fs_info = root;
  489. disk_super = root->fs_info->disk_super;
  490. printk("read in super total blocks %Lu root %Lu\n",
  491. btrfs_super_total_blocks(disk_super),
  492. btrfs_super_root_dir(disk_super));
  493. inode = iget_locked(sb, btrfs_super_root_dir(disk_super));
  494. if (!inode)
  495. return -ENOMEM;
  496. if (inode->i_state & I_NEW) {
  497. btrfs_read_locked_inode(inode);
  498. unlock_new_inode(inode);
  499. }
  500. root_dentry = d_alloc_root(inode);
  501. if (!root_dentry) {
  502. iput(inode);
  503. return -ENOMEM;
  504. }
  505. sb->s_root = root_dentry;
  506. return 0;
  507. }
  508. static void fill_inode_item(struct btrfs_inode_item *item,
  509. struct inode *inode)
  510. {
  511. btrfs_set_inode_uid(item, inode->i_uid);
  512. btrfs_set_inode_gid(item, inode->i_gid);
  513. btrfs_set_inode_size(item, inode->i_size);
  514. btrfs_set_inode_mode(item, inode->i_mode);
  515. btrfs_set_inode_nlink(item, inode->i_nlink);
  516. btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
  517. btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
  518. btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
  519. btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
  520. btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
  521. btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
  522. btrfs_set_inode_nblocks(item, inode->i_blocks);
  523. btrfs_set_inode_generation(item, inode->i_generation);
  524. check_inode(inode);
  525. }
  526. static int btrfs_update_inode(struct btrfs_trans_handle *trans,
  527. struct btrfs_root *root,
  528. struct inode *inode)
  529. {
  530. struct btrfs_inode_item *inode_item;
  531. struct btrfs_path *path;
  532. int ret;
  533. path = btrfs_alloc_path();
  534. BUG_ON(!path);
  535. btrfs_init_path(path);
  536. ret = btrfs_lookup_inode(trans, root, path, inode->i_ino, 1);
  537. if (ret) {
  538. if (ret > 0)
  539. ret = -ENOENT;
  540. goto failed;
  541. }
  542. inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  543. path->slots[0],
  544. struct btrfs_inode_item);
  545. fill_inode_item(inode_item, inode);
  546. btrfs_mark_buffer_dirty(path->nodes[0]);
  547. failed:
  548. btrfs_release_path(root, path);
  549. btrfs_free_path(path);
  550. check_inode(inode);
  551. return 0;
  552. }
  553. static int btrfs_write_inode(struct inode *inode, int wait)
  554. {
  555. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  556. struct btrfs_trans_handle *trans;
  557. int ret;
  558. mutex_lock(&root->fs_info->fs_mutex);
  559. trans = btrfs_start_transaction(root, 1);
  560. ret = btrfs_update_inode(trans, root, inode);
  561. if (wait)
  562. btrfs_commit_transaction(trans, root);
  563. else
  564. btrfs_end_transaction(trans, root);
  565. mutex_unlock(&root->fs_info->fs_mutex);
  566. check_inode(inode);
  567. return ret;
  568. }
  569. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  570. struct inode *dir, int mode)
  571. {
  572. struct inode *inode;
  573. struct btrfs_inode_item inode_item;
  574. struct btrfs_root *root = btrfs_sb(dir->i_sb);
  575. struct btrfs_key key;
  576. int ret;
  577. u64 objectid;
  578. inode = new_inode(dir->i_sb);
  579. if (!inode)
  580. return ERR_PTR(-ENOMEM);
  581. check_inode(inode);
  582. ret = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  583. BUG_ON(ret);
  584. inode->i_uid = current->fsuid;
  585. inode->i_gid = current->fsgid;
  586. inode->i_mode = mode;
  587. inode->i_ino = objectid;
  588. inode->i_blocks = 0;
  589. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
  590. fill_inode_item(&inode_item, inode);
  591. key.objectid = objectid;
  592. key.flags = 0;
  593. key.offset = 0;
  594. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  595. ret = btrfs_insert_inode_map(trans, root, objectid, &key);
  596. BUG_ON(ret);
  597. ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
  598. BUG_ON(ret);
  599. insert_inode_hash(inode);
  600. check_inode(inode);
  601. check_inode(dir);
  602. return inode;
  603. }
  604. static int btrfs_add_link(struct btrfs_trans_handle *trans,
  605. struct dentry *dentry, struct inode *inode)
  606. {
  607. int ret;
  608. ret = btrfs_insert_dir_item(trans, btrfs_sb(inode->i_sb),
  609. dentry->d_name.name, dentry->d_name.len,
  610. dentry->d_parent->d_inode->i_ino,
  611. inode->i_ino, 0);
  612. if (ret == 0) {
  613. dentry->d_parent->d_inode->i_size += dentry->d_name.len;
  614. ret = btrfs_update_inode(trans, btrfs_sb(inode->i_sb),
  615. dentry->d_parent->d_inode);
  616. }
  617. check_inode(inode);
  618. check_inode(dentry->d_parent->d_inode);
  619. return ret;
  620. }
  621. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  622. struct dentry *dentry, struct inode *inode)
  623. {
  624. int err = btrfs_add_link(trans, dentry, inode);
  625. if (!err) {
  626. d_instantiate(dentry, inode);
  627. return 0;
  628. }
  629. if (err > 0)
  630. err = -EEXIST;
  631. check_inode(inode);
  632. return err;
  633. }
  634. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  635. int mode, struct nameidata *nd)
  636. {
  637. struct btrfs_trans_handle *trans;
  638. struct btrfs_root *root = btrfs_sb(dir->i_sb);
  639. struct inode *inode;
  640. int err;
  641. int drop_inode = 0;
  642. mutex_lock(&root->fs_info->fs_mutex);
  643. trans = btrfs_start_transaction(root, 1);
  644. inode = btrfs_new_inode(trans, dir, mode);
  645. err = PTR_ERR(inode);
  646. if (IS_ERR(inode))
  647. goto out_unlock;
  648. // FIXME mark the inode dirty
  649. err = btrfs_add_nondir(trans, dentry, inode);
  650. if (err)
  651. drop_inode = 1;
  652. else {
  653. inode->i_mapping->a_ops = &btrfs_aops;
  654. inode->i_fop = &btrfs_file_operations;
  655. inode->i_op = &btrfs_file_inode_operations;
  656. }
  657. dir->i_sb->s_dirt = 1;
  658. out_unlock:
  659. btrfs_end_transaction(trans, root);
  660. mutex_unlock(&root->fs_info->fs_mutex);
  661. check_inode(inode);
  662. check_inode(dir);
  663. if (drop_inode) {
  664. inode_dec_link_count(inode);
  665. iput(inode);
  666. }
  667. return err;
  668. }
  669. static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
  670. struct inode *inode, struct inode *dir)
  671. {
  672. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  673. int ret;
  674. char buf[2];
  675. buf[0] = '.';
  676. buf[1] = '.';
  677. ret = btrfs_insert_dir_item(trans, root, buf, 1, inode->i_ino,
  678. inode->i_ino, 1);
  679. if (ret)
  680. goto error;
  681. ret = btrfs_insert_dir_item(trans, root, buf, 2, inode->i_ino,
  682. dir->i_ino, 1);
  683. if (ret)
  684. goto error;
  685. inode->i_size = 3;
  686. ret = btrfs_update_inode(trans, root, inode);
  687. error:
  688. return ret;
  689. }
  690. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  691. {
  692. struct inode *inode;
  693. struct btrfs_trans_handle *trans;
  694. struct btrfs_root *root = btrfs_sb(dir->i_sb);
  695. int err = 0;
  696. int drop_on_err = 0;
  697. mutex_lock(&root->fs_info->fs_mutex);
  698. trans = btrfs_start_transaction(root, 1);
  699. if (IS_ERR(trans)) {
  700. err = PTR_ERR(trans);
  701. goto out_unlock;
  702. }
  703. inode = btrfs_new_inode(trans, dir, S_IFDIR | mode);
  704. if (IS_ERR(inode)) {
  705. err = PTR_ERR(inode);
  706. goto out_fail;
  707. }
  708. drop_on_err = 1;
  709. inode->i_op = &btrfs_dir_inode_operations;
  710. inode->i_fop = &btrfs_dir_file_operations;
  711. err = btrfs_make_empty_dir(trans, inode, dir);
  712. if (err)
  713. goto out_fail;
  714. err = btrfs_add_link(trans, dentry, inode);
  715. if (err)
  716. goto out_fail;
  717. d_instantiate(dentry, inode);
  718. drop_on_err = 0;
  719. out_fail:
  720. btrfs_end_transaction(trans, root);
  721. out_unlock:
  722. mutex_unlock(&root->fs_info->fs_mutex);
  723. if (drop_on_err)
  724. iput(inode);
  725. return err;
  726. }
  727. static int btrfs_sync_fs(struct super_block *sb, int wait)
  728. {
  729. struct btrfs_trans_handle *trans;
  730. struct btrfs_root *root;
  731. int ret;
  732. root = btrfs_sb(sb);
  733. sb->s_dirt = 0;
  734. if (!wait) {
  735. filemap_flush(root->fs_info->btree_inode->i_mapping);
  736. return 0;
  737. }
  738. filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
  739. mutex_lock(&root->fs_info->fs_mutex);
  740. trans = btrfs_start_transaction(root, 1);
  741. ret = btrfs_commit_transaction(trans, root);
  742. sb->s_dirt = 0;
  743. BUG_ON(ret);
  744. printk("btrfs sync_fs\n");
  745. mutex_unlock(&root->fs_info->fs_mutex);
  746. return 0;
  747. }
  748. static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
  749. struct buffer_head *result, int create)
  750. {
  751. int ret;
  752. int err = 0;
  753. u64 blocknr;
  754. u64 extent_start = 0;
  755. u64 extent_end = 0;
  756. u64 objectid = inode->i_ino;
  757. struct btrfs_path *path;
  758. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  759. struct btrfs_trans_handle *trans = NULL;
  760. struct btrfs_file_extent_item *item;
  761. struct btrfs_leaf *leaf;
  762. struct btrfs_disk_key *found_key;
  763. path = btrfs_alloc_path();
  764. BUG_ON(!path);
  765. btrfs_init_path(path);
  766. if (create)
  767. trans = btrfs_start_transaction(root, 1);
  768. ret = btrfs_lookup_file_extent(trans, root, path,
  769. inode->i_ino,
  770. iblock << inode->i_blkbits, 0);
  771. if (ret < 0) {
  772. err = ret;
  773. goto out;
  774. }
  775. if (ret != 0) {
  776. if (path->slots[0] == 0) {
  777. btrfs_release_path(root, path);
  778. goto allocate;
  779. }
  780. path->slots[0]--;
  781. }
  782. item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  783. struct btrfs_file_extent_item);
  784. leaf = btrfs_buffer_leaf(path->nodes[0]);
  785. blocknr = btrfs_file_extent_disk_blocknr(item);
  786. blocknr += btrfs_file_extent_offset(item);
  787. /* exact match found, use it */
  788. if (ret == 0) {
  789. err = 0;
  790. map_bh(result, inode->i_sb, blocknr);
  791. goto out;
  792. }
  793. /* are we inside the extent that was found? */
  794. found_key = &leaf->items[path->slots[0]].key;
  795. if (btrfs_disk_key_objectid(found_key) != objectid ||
  796. btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
  797. extent_end = 0;
  798. extent_start = 0;
  799. btrfs_release_path(root, path);
  800. goto allocate;
  801. }
  802. extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
  803. extent_start = extent_start >> inode->i_blkbits;
  804. extent_start += btrfs_file_extent_offset(item);
  805. extent_end = extent_start + btrfs_file_extent_num_blocks(item);
  806. if (iblock >= extent_start && iblock < extent_end) {
  807. err = 0;
  808. map_bh(result, inode->i_sb, blocknr + iblock - extent_start);
  809. goto out;
  810. }
  811. allocate:
  812. /* ok, create a new extent */
  813. if (!create) {
  814. err = 0;
  815. goto out;
  816. }
  817. ret = btrfs_alloc_file_extent(trans, root, objectid,
  818. iblock << inode->i_blkbits,
  819. 1, extent_end, &blocknr);
  820. if (ret) {
  821. err = ret;
  822. goto out;
  823. }
  824. inode->i_blocks += inode->i_sb->s_blocksize >> 9;
  825. set_buffer_new(result);
  826. map_bh(result, inode->i_sb, blocknr);
  827. out:
  828. btrfs_release_path(root, path);
  829. btrfs_free_path(path);
  830. if (trans)
  831. btrfs_end_transaction(trans, root);
  832. return err;
  833. }
  834. static int btrfs_get_block(struct inode *inode, sector_t iblock,
  835. struct buffer_head *result, int create)
  836. {
  837. int err;
  838. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  839. mutex_lock(&root->fs_info->fs_mutex);
  840. err = btrfs_get_block_lock(inode, iblock, result, create);
  841. mutex_unlock(&root->fs_info->fs_mutex);
  842. return err;
  843. }
  844. static int btrfs_prepare_write(struct file *file, struct page *page,
  845. unsigned from, unsigned to)
  846. {
  847. WARN_ON(1);
  848. return nobh_prepare_write(page, from, to, btrfs_get_block);
  849. }
  850. static int btrfs_commit_write(struct file *file, struct page *page,
  851. unsigned from, unsigned to)
  852. {
  853. WARN_ON(1);
  854. return nobh_commit_write(file, page, from, to);
  855. }
  856. static void btrfs_write_super(struct super_block *sb)
  857. {
  858. btrfs_sync_fs(sb, 1);
  859. }
  860. static int btrfs_readpage(struct file *file, struct page *page)
  861. {
  862. return mpage_readpage(page, btrfs_get_block);
  863. }
  864. static int btrfs_readpages(struct file *file, struct address_space *mapping,
  865. struct list_head *pages, unsigned nr_pages)
  866. {
  867. return mpage_readpages(mapping, pages, nr_pages, btrfs_get_block);
  868. }
  869. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  870. {
  871. return nobh_writepage(page, btrfs_get_block, wbc);
  872. }
  873. static void btrfs_truncate(struct inode *inode)
  874. {
  875. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  876. int ret;
  877. struct btrfs_trans_handle *trans;
  878. if (!S_ISREG(inode->i_mode))
  879. return;
  880. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  881. return;
  882. nobh_truncate_page(inode->i_mapping, inode->i_size);
  883. /* FIXME, add redo link to tree so we don't leak on crash */
  884. mutex_lock(&root->fs_info->fs_mutex);
  885. trans = btrfs_start_transaction(root, 1);
  886. ret = btrfs_truncate_in_trans(trans, root, inode);
  887. BUG_ON(ret);
  888. ret = btrfs_end_transaction(trans, root);
  889. BUG_ON(ret);
  890. mutex_unlock(&root->fs_info->fs_mutex);
  891. mark_inode_dirty(inode);
  892. }
  893. static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
  894. struct page **prepared_pages,
  895. const char __user * buf)
  896. {
  897. long page_fault = 0;
  898. int i;
  899. int offset = pos & (PAGE_CACHE_SIZE - 1);
  900. for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
  901. size_t count = min_t(size_t,
  902. PAGE_CACHE_SIZE - offset, write_bytes);
  903. struct page *page = prepared_pages[i];
  904. fault_in_pages_readable(buf, count);
  905. /* Copy data from userspace to the current page */
  906. kmap(page);
  907. page_fault = __copy_from_user(page_address(page) + offset,
  908. buf, count);
  909. /* Flush processor's dcache for this page */
  910. flush_dcache_page(page);
  911. kunmap(page);
  912. buf += count;
  913. write_bytes -= count;
  914. if (page_fault)
  915. break;
  916. }
  917. return page_fault ? -EFAULT : 0;
  918. }
  919. static void btrfs_drop_pages(struct page **pages, size_t num_pages)
  920. {
  921. size_t i;
  922. for (i = 0; i < num_pages; i++) {
  923. if (!pages[i])
  924. break;
  925. unlock_page(pages[i]);
  926. mark_page_accessed(pages[i]);
  927. page_cache_release(pages[i]);
  928. }
  929. }
  930. static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
  931. struct btrfs_root *root,
  932. struct file *file,
  933. struct page **pages,
  934. size_t num_pages,
  935. loff_t pos,
  936. size_t write_bytes)
  937. {
  938. int i;
  939. int offset;
  940. int err = 0;
  941. int ret;
  942. int this_write;
  943. struct inode *inode = file->f_path.dentry->d_inode;
  944. for (i = 0; i < num_pages; i++) {
  945. offset = pos & (PAGE_CACHE_SIZE -1);
  946. this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
  947. /* FIXME, one block at a time */
  948. mutex_lock(&root->fs_info->fs_mutex);
  949. trans = btrfs_start_transaction(root, 1);
  950. btrfs_csum_file_block(trans, root, inode->i_ino,
  951. pages[i]->index << PAGE_CACHE_SHIFT,
  952. kmap(pages[i]), PAGE_CACHE_SIZE);
  953. kunmap(pages[i]);
  954. SetPageChecked(pages[i]);
  955. ret = btrfs_end_transaction(trans, root);
  956. BUG_ON(ret);
  957. mutex_unlock(&root->fs_info->fs_mutex);
  958. ret = nobh_commit_write(file, pages[i], offset,
  959. offset + this_write);
  960. pos += this_write;
  961. if (ret) {
  962. err = ret;
  963. goto failed;
  964. }
  965. WARN_ON(this_write > write_bytes);
  966. write_bytes -= this_write;
  967. }
  968. failed:
  969. return err;
  970. }
  971. static int prepare_pages(struct btrfs_trans_handle *trans,
  972. struct btrfs_root *root,
  973. struct file *file,
  974. struct page **pages,
  975. size_t num_pages,
  976. loff_t pos,
  977. size_t write_bytes)
  978. {
  979. int i;
  980. unsigned long index = pos >> PAGE_CACHE_SHIFT;
  981. struct inode *inode = file->f_path.dentry->d_inode;
  982. int offset;
  983. int err = 0;
  984. int ret;
  985. int this_write;
  986. loff_t isize = i_size_read(inode);
  987. memset(pages, 0, num_pages * sizeof(struct page *));
  988. for (i = 0; i < num_pages; i++) {
  989. pages[i] = grab_cache_page(inode->i_mapping, index + i);
  990. if (!pages[i]) {
  991. err = -ENOMEM;
  992. goto failed_release;
  993. }
  994. offset = pos & (PAGE_CACHE_SIZE -1);
  995. this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
  996. ret = nobh_prepare_write(pages[i], offset,
  997. offset + this_write,
  998. btrfs_get_block);
  999. pos += this_write;
  1000. if (ret) {
  1001. err = ret;
  1002. goto failed_truncate;
  1003. }
  1004. WARN_ON(this_write > write_bytes);
  1005. write_bytes -= this_write;
  1006. }
  1007. return 0;
  1008. failed_release:
  1009. btrfs_drop_pages(pages, num_pages);
  1010. return err;
  1011. failed_truncate:
  1012. btrfs_drop_pages(pages, num_pages);
  1013. if (pos > isize)
  1014. vmtruncate(inode, isize);
  1015. return err;
  1016. }
  1017. static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
  1018. size_t count, loff_t *ppos)
  1019. {
  1020. loff_t pos;
  1021. size_t num_written = 0;
  1022. int err = 0;
  1023. int ret = 0;
  1024. struct inode *inode = file->f_path.dentry->d_inode;
  1025. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  1026. struct page *pages[1];
  1027. if (file->f_flags & O_DIRECT)
  1028. return -EINVAL;
  1029. pos = *ppos;
  1030. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1031. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  1032. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  1033. if (err)
  1034. goto out;
  1035. if (count == 0)
  1036. goto out;
  1037. err = remove_suid(file->f_path.dentry);
  1038. if (err)
  1039. goto out;
  1040. file_update_time(file);
  1041. mutex_lock(&inode->i_mutex);
  1042. while(count > 0) {
  1043. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  1044. size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
  1045. size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
  1046. PAGE_CACHE_SHIFT;
  1047. ret = prepare_pages(NULL, root, file, pages, num_pages,
  1048. pos, write_bytes);
  1049. BUG_ON(ret);
  1050. ret = btrfs_copy_from_user(pos, num_pages,
  1051. write_bytes, pages, buf);
  1052. BUG_ON(ret);
  1053. ret = dirty_and_release_pages(NULL, root, file, pages,
  1054. num_pages, pos, write_bytes);
  1055. BUG_ON(ret);
  1056. btrfs_drop_pages(pages, num_pages);
  1057. buf += write_bytes;
  1058. count -= write_bytes;
  1059. pos += write_bytes;
  1060. num_written += write_bytes;
  1061. balance_dirty_pages_ratelimited(inode->i_mapping);
  1062. cond_resched();
  1063. }
  1064. mutex_unlock(&inode->i_mutex);
  1065. out:
  1066. *ppos = pos;
  1067. current->backing_dev_info = NULL;
  1068. return num_written ? num_written : err;
  1069. }
  1070. static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
  1071. unsigned long offset, unsigned long size)
  1072. {
  1073. char *kaddr;
  1074. unsigned long left, count = desc->count;
  1075. if (size > count)
  1076. size = count;
  1077. if (!PageChecked(page)) {
  1078. /* FIXME, do it per block */
  1079. struct btrfs_root *root = btrfs_sb(page->mapping->host->i_sb);
  1080. int ret = btrfs_csum_verify_file_block(root,
  1081. page->mapping->host->i_ino,
  1082. page->index << PAGE_CACHE_SHIFT,
  1083. kmap(page), PAGE_CACHE_SIZE);
  1084. if (ret) {
  1085. printk("failed to verify ino %lu page %lu\n",
  1086. page->mapping->host->i_ino,
  1087. page->index);
  1088. memset(page_address(page), 0, PAGE_CACHE_SIZE);
  1089. }
  1090. SetPageChecked(page);
  1091. kunmap(page);
  1092. }
  1093. /*
  1094. * Faults on the destination of a read are common, so do it before
  1095. * taking the kmap.
  1096. */
  1097. if (!fault_in_pages_writeable(desc->arg.buf, size)) {
  1098. kaddr = kmap_atomic(page, KM_USER0);
  1099. left = __copy_to_user_inatomic(desc->arg.buf,
  1100. kaddr + offset, size);
  1101. kunmap_atomic(kaddr, KM_USER0);
  1102. if (left == 0)
  1103. goto success;
  1104. }
  1105. /* Do it the slow way */
  1106. kaddr = kmap(page);
  1107. left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
  1108. kunmap(page);
  1109. if (left) {
  1110. size -= left;
  1111. desc->error = -EFAULT;
  1112. }
  1113. success:
  1114. desc->count = count - size;
  1115. desc->written += size;
  1116. desc->arg.buf += size;
  1117. return size;
  1118. }
  1119. /**
  1120. * btrfs_file_aio_read - filesystem read routine
  1121. * @iocb: kernel I/O control block
  1122. * @iov: io vector request
  1123. * @nr_segs: number of segments in the iovec
  1124. * @pos: current file position
  1125. */
  1126. static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
  1127. unsigned long nr_segs, loff_t pos)
  1128. {
  1129. struct file *filp = iocb->ki_filp;
  1130. ssize_t retval;
  1131. unsigned long seg;
  1132. size_t count;
  1133. loff_t *ppos = &iocb->ki_pos;
  1134. count = 0;
  1135. for (seg = 0; seg < nr_segs; seg++) {
  1136. const struct iovec *iv = &iov[seg];
  1137. /*
  1138. * If any segment has a negative length, or the cumulative
  1139. * length ever wraps negative then return -EINVAL.
  1140. */
  1141. count += iv->iov_len;
  1142. if (unlikely((ssize_t)(count|iv->iov_len) < 0))
  1143. return -EINVAL;
  1144. if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
  1145. continue;
  1146. if (seg == 0)
  1147. return -EFAULT;
  1148. nr_segs = seg;
  1149. count -= iv->iov_len; /* This segment is no good */
  1150. break;
  1151. }
  1152. retval = 0;
  1153. if (count) {
  1154. for (seg = 0; seg < nr_segs; seg++) {
  1155. read_descriptor_t desc;
  1156. desc.written = 0;
  1157. desc.arg.buf = iov[seg].iov_base;
  1158. desc.count = iov[seg].iov_len;
  1159. if (desc.count == 0)
  1160. continue;
  1161. desc.error = 0;
  1162. do_generic_file_read(filp, ppos, &desc,
  1163. btrfs_read_actor);
  1164. retval += desc.written;
  1165. if (desc.error) {
  1166. retval = retval ?: desc.error;
  1167. break;
  1168. }
  1169. }
  1170. }
  1171. return retval;
  1172. }
  1173. static struct kmem_cache *btrfs_inode_cachep;
  1174. struct kmem_cache *btrfs_trans_handle_cachep;
  1175. struct kmem_cache *btrfs_transaction_cachep;
  1176. struct kmem_cache *btrfs_bit_radix_cachep;
  1177. struct kmem_cache *btrfs_path_cachep;
  1178. /*
  1179. * Called inside transaction, so use GFP_NOFS
  1180. */
  1181. static struct inode *btrfs_alloc_inode(struct super_block *sb)
  1182. {
  1183. struct btrfs_inode *ei;
  1184. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  1185. if (!ei)
  1186. return NULL;
  1187. ei->magic = 0xDEADBEEF;
  1188. ei->magic2 = 0xDEADBEAF;
  1189. return &ei->vfs_inode;
  1190. }
  1191. static void btrfs_destroy_inode(struct inode *inode)
  1192. {
  1193. struct btrfs_inode *ei = BTRFS_I(inode);
  1194. WARN_ON(ei->magic != 0xDEADBEEF);
  1195. WARN_ON(ei->magic2 != 0xDEADBEAF);
  1196. WARN_ON(!list_empty(&inode->i_dentry));
  1197. WARN_ON(inode->i_data.nrpages);
  1198. ei->magic = 0;
  1199. ei->magic2 = 0;
  1200. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  1201. }
  1202. static void init_once(void * foo, struct kmem_cache * cachep,
  1203. unsigned long flags)
  1204. {
  1205. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  1206. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  1207. SLAB_CTOR_CONSTRUCTOR) {
  1208. inode_init_once(&ei->vfs_inode);
  1209. }
  1210. }
  1211. static int init_inodecache(void)
  1212. {
  1213. btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
  1214. sizeof(struct btrfs_inode),
  1215. 0, (SLAB_RECLAIM_ACCOUNT|
  1216. SLAB_MEM_SPREAD),
  1217. init_once, NULL);
  1218. btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
  1219. sizeof(struct btrfs_trans_handle),
  1220. 0, (SLAB_RECLAIM_ACCOUNT|
  1221. SLAB_MEM_SPREAD),
  1222. NULL, NULL);
  1223. btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
  1224. sizeof(struct btrfs_transaction),
  1225. 0, (SLAB_RECLAIM_ACCOUNT|
  1226. SLAB_MEM_SPREAD),
  1227. NULL, NULL);
  1228. btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
  1229. sizeof(struct btrfs_transaction),
  1230. 0, (SLAB_RECLAIM_ACCOUNT|
  1231. SLAB_MEM_SPREAD),
  1232. NULL, NULL);
  1233. btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
  1234. 256,
  1235. 0, (SLAB_RECLAIM_ACCOUNT|
  1236. SLAB_MEM_SPREAD |
  1237. SLAB_DESTROY_BY_RCU),
  1238. NULL, NULL);
  1239. if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
  1240. btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
  1241. return -ENOMEM;
  1242. return 0;
  1243. }
  1244. static void destroy_inodecache(void)
  1245. {
  1246. kmem_cache_destroy(btrfs_inode_cachep);
  1247. kmem_cache_destroy(btrfs_trans_handle_cachep);
  1248. kmem_cache_destroy(btrfs_transaction_cachep);
  1249. kmem_cache_destroy(btrfs_bit_radix_cachep);
  1250. kmem_cache_destroy(btrfs_path_cachep);
  1251. }
  1252. static int btrfs_get_sb(struct file_system_type *fs_type,
  1253. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  1254. {
  1255. return get_sb_bdev(fs_type, flags, dev_name, data,
  1256. btrfs_fill_super, mnt);
  1257. }
  1258. static struct file_system_type btrfs_fs_type = {
  1259. .owner = THIS_MODULE,
  1260. .name = "btrfs",
  1261. .get_sb = btrfs_get_sb,
  1262. .kill_sb = kill_block_super,
  1263. .fs_flags = FS_REQUIRES_DEV,
  1264. };
  1265. static struct super_operations btrfs_super_ops = {
  1266. .statfs = simple_statfs,
  1267. .delete_inode = btrfs_delete_inode,
  1268. .put_super = btrfs_put_super,
  1269. .read_inode = btrfs_read_locked_inode,
  1270. .write_super = btrfs_write_super,
  1271. .sync_fs = btrfs_sync_fs,
  1272. .write_inode = btrfs_write_inode,
  1273. .alloc_inode = btrfs_alloc_inode,
  1274. .destroy_inode = btrfs_destroy_inode,
  1275. };
  1276. static struct inode_operations btrfs_dir_inode_operations = {
  1277. .lookup = btrfs_lookup,
  1278. .create = btrfs_create,
  1279. .unlink = btrfs_unlink,
  1280. .mkdir = btrfs_mkdir,
  1281. .rmdir = btrfs_rmdir,
  1282. };
  1283. static struct file_operations btrfs_dir_file_operations = {
  1284. .llseek = generic_file_llseek,
  1285. .read = generic_read_dir,
  1286. .readdir = btrfs_readdir,
  1287. };
  1288. static struct address_space_operations btrfs_aops = {
  1289. .readpage = btrfs_readpage,
  1290. .readpages = btrfs_readpages,
  1291. .writepage = btrfs_writepage,
  1292. .sync_page = block_sync_page,
  1293. .prepare_write = btrfs_prepare_write,
  1294. .commit_write = btrfs_commit_write,
  1295. };
  1296. static struct inode_operations btrfs_file_inode_operations = {
  1297. .truncate = btrfs_truncate,
  1298. };
  1299. static struct file_operations btrfs_file_operations = {
  1300. .llseek = generic_file_llseek,
  1301. .read = do_sync_read,
  1302. .aio_read = btrfs_file_aio_read,
  1303. .write = btrfs_file_write,
  1304. .mmap = generic_file_mmap,
  1305. .open = generic_file_open,
  1306. };
  1307. static int __init init_btrfs_fs(void)
  1308. {
  1309. int err;
  1310. printk("btrfs loaded!\n");
  1311. err = init_inodecache();
  1312. if (err)
  1313. return err;
  1314. return register_filesystem(&btrfs_fs_type);
  1315. }
  1316. static void __exit exit_btrfs_fs(void)
  1317. {
  1318. destroy_inodecache();
  1319. unregister_filesystem(&btrfs_fs_type);
  1320. printk("btrfs unloaded\n");
  1321. }
  1322. module_init(init_btrfs_fs)
  1323. module_exit(exit_btrfs_fs)
  1324. MODULE_LICENSE("GPL");