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