super.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930
  1. #include <linux/module.h>
  2. #include <linux/buffer_head.h>
  3. #include <linux/fs.h>
  4. #include <linux/pagemap.h>
  5. #include <linux/highmem.h>
  6. #include <linux/time.h>
  7. #include <linux/init.h>
  8. #include <linux/string.h>
  9. #include <linux/smp_lock.h>
  10. #include <linux/backing-dev.h>
  11. #include <linux/mpage.h>
  12. #include <linux/swap.h>
  13. #include <linux/writeback.h>
  14. #include "ctree.h"
  15. #include "disk-io.h"
  16. #include "transaction.h"
  17. #include "btrfs_inode.h"
  18. #include "ioctl.h"
  19. void btrfs_fsinfo_release(struct kobject *obj)
  20. {
  21. struct btrfs_fs_info *fsinfo = container_of(obj,
  22. struct btrfs_fs_info, kobj);
  23. kfree(fsinfo);
  24. }
  25. struct kobj_type btrfs_fsinfo_ktype = {
  26. .release = btrfs_fsinfo_release,
  27. };
  28. struct btrfs_iget_args {
  29. u64 ino;
  30. struct btrfs_root *root;
  31. };
  32. decl_subsys(btrfs, &btrfs_fsinfo_ktype, NULL);
  33. #define BTRFS_SUPER_MAGIC 0x9123682E
  34. static struct inode_operations btrfs_dir_inode_operations;
  35. static struct inode_operations btrfs_dir_ro_inode_operations;
  36. static struct super_operations btrfs_super_ops;
  37. static struct file_operations btrfs_dir_file_operations;
  38. static struct inode_operations btrfs_file_inode_operations;
  39. static struct address_space_operations btrfs_aops;
  40. static struct file_operations btrfs_file_operations;
  41. static void btrfs_read_locked_inode(struct inode *inode)
  42. {
  43. struct btrfs_path *path;
  44. struct btrfs_inode_item *inode_item;
  45. struct btrfs_root *root = BTRFS_I(inode)->root;
  46. struct btrfs_key location;
  47. int ret;
  48. path = btrfs_alloc_path();
  49. BUG_ON(!path);
  50. btrfs_init_path(path);
  51. mutex_lock(&root->fs_info->fs_mutex);
  52. memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
  53. ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
  54. if (ret) {
  55. btrfs_free_path(path);
  56. goto make_bad;
  57. }
  58. inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  59. path->slots[0],
  60. struct btrfs_inode_item);
  61. inode->i_mode = btrfs_inode_mode(inode_item);
  62. inode->i_nlink = btrfs_inode_nlink(inode_item);
  63. inode->i_uid = btrfs_inode_uid(inode_item);
  64. inode->i_gid = btrfs_inode_gid(inode_item);
  65. inode->i_size = btrfs_inode_size(inode_item);
  66. inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
  67. inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
  68. inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
  69. inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
  70. inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
  71. inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
  72. inode->i_blocks = btrfs_inode_nblocks(inode_item);
  73. inode->i_generation = btrfs_inode_generation(inode_item);
  74. btrfs_free_path(path);
  75. inode_item = NULL;
  76. mutex_unlock(&root->fs_info->fs_mutex);
  77. switch (inode->i_mode & S_IFMT) {
  78. #if 0
  79. default:
  80. init_special_inode(inode, inode->i_mode,
  81. btrfs_inode_rdev(inode_item));
  82. break;
  83. #endif
  84. case S_IFREG:
  85. inode->i_mapping->a_ops = &btrfs_aops;
  86. inode->i_fop = &btrfs_file_operations;
  87. inode->i_op = &btrfs_file_inode_operations;
  88. break;
  89. case S_IFDIR:
  90. inode->i_fop = &btrfs_dir_file_operations;
  91. if (root == root->fs_info->tree_root)
  92. inode->i_op = &btrfs_dir_ro_inode_operations;
  93. else
  94. inode->i_op = &btrfs_dir_inode_operations;
  95. break;
  96. case S_IFLNK:
  97. // inode->i_op = &page_symlink_inode_operations;
  98. break;
  99. }
  100. return;
  101. make_bad:
  102. btrfs_release_path(root, path);
  103. btrfs_free_path(path);
  104. mutex_unlock(&root->fs_info->fs_mutex);
  105. make_bad_inode(inode);
  106. }
  107. static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
  108. struct btrfs_root *root,
  109. struct inode *dir,
  110. struct dentry *dentry)
  111. {
  112. struct btrfs_path *path;
  113. const char *name = dentry->d_name.name;
  114. int name_len = dentry->d_name.len;
  115. int ret;
  116. u64 objectid;
  117. struct btrfs_dir_item *di;
  118. path = btrfs_alloc_path();
  119. BUG_ON(!path);
  120. btrfs_init_path(path);
  121. ret = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  122. name, name_len, -1);
  123. if (ret < 0)
  124. goto err;
  125. if (ret > 0) {
  126. ret = -ENOENT;
  127. goto err;
  128. }
  129. di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  130. struct btrfs_dir_item);
  131. objectid = btrfs_disk_key_objectid(&di->location);
  132. ret = btrfs_del_item(trans, root, path);
  133. BUG_ON(ret);
  134. btrfs_release_path(root, path);
  135. ret = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
  136. objectid, -1);
  137. BUG_ON(ret);
  138. ret = btrfs_del_item(trans, root, path);
  139. BUG_ON(ret);
  140. dentry->d_inode->i_ctime = dir->i_ctime;
  141. err:
  142. btrfs_release_path(root, path);
  143. btrfs_free_path(path);
  144. if (ret == 0) {
  145. inode_dec_link_count(dentry->d_inode);
  146. dir->i_size -= name_len * 2;
  147. mark_inode_dirty(dir);
  148. }
  149. return ret;
  150. }
  151. static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
  152. {
  153. struct btrfs_root *root;
  154. struct btrfs_trans_handle *trans;
  155. int ret;
  156. root = BTRFS_I(dir)->root;
  157. mutex_lock(&root->fs_info->fs_mutex);
  158. trans = btrfs_start_transaction(root, 1);
  159. ret = btrfs_unlink_trans(trans, root, dir, dentry);
  160. btrfs_end_transaction(trans, root);
  161. mutex_unlock(&root->fs_info->fs_mutex);
  162. return ret;
  163. }
  164. static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
  165. {
  166. struct inode *inode = dentry->d_inode;
  167. int err;
  168. int ret;
  169. struct btrfs_root *root = BTRFS_I(dir)->root;
  170. struct btrfs_path *path;
  171. struct btrfs_key key;
  172. struct btrfs_trans_handle *trans;
  173. struct btrfs_key found_key;
  174. int found_type;
  175. struct btrfs_leaf *leaf;
  176. char *goodnames = "..";
  177. path = btrfs_alloc_path();
  178. BUG_ON(!path);
  179. btrfs_init_path(path);
  180. mutex_lock(&root->fs_info->fs_mutex);
  181. trans = btrfs_start_transaction(root, 1);
  182. key.objectid = inode->i_ino;
  183. key.offset = (u64)-1;
  184. key.flags = (u32)-1;
  185. while(1) {
  186. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  187. if (ret < 0) {
  188. err = ret;
  189. goto out;
  190. }
  191. BUG_ON(ret == 0);
  192. if (path->slots[0] == 0) {
  193. err = -ENOENT;
  194. goto out;
  195. }
  196. path->slots[0]--;
  197. leaf = btrfs_buffer_leaf(path->nodes[0]);
  198. btrfs_disk_key_to_cpu(&found_key,
  199. &leaf->items[path->slots[0]].key);
  200. found_type = btrfs_key_type(&found_key);
  201. if (found_key.objectid != inode->i_ino) {
  202. err = -ENOENT;
  203. goto out;
  204. }
  205. if ((found_type != BTRFS_DIR_ITEM_KEY &&
  206. found_type != BTRFS_DIR_INDEX_KEY) ||
  207. (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
  208. !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
  209. err = -ENOTEMPTY;
  210. goto out;
  211. }
  212. ret = btrfs_del_item(trans, root, path);
  213. BUG_ON(ret);
  214. if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
  215. break;
  216. btrfs_release_path(root, path);
  217. }
  218. ret = 0;
  219. btrfs_release_path(root, path);
  220. /* now the directory is empty */
  221. err = btrfs_unlink_trans(trans, root, dir, dentry);
  222. if (!err) {
  223. inode->i_size = 0;
  224. }
  225. out:
  226. btrfs_release_path(root, path);
  227. btrfs_free_path(path);
  228. mutex_unlock(&root->fs_info->fs_mutex);
  229. ret = btrfs_end_transaction(trans, root);
  230. if (ret && !err)
  231. err = ret;
  232. return err;
  233. }
  234. static int btrfs_free_inode(struct btrfs_trans_handle *trans,
  235. struct btrfs_root *root,
  236. struct inode *inode)
  237. {
  238. struct btrfs_path *path;
  239. int ret;
  240. clear_inode(inode);
  241. path = btrfs_alloc_path();
  242. BUG_ON(!path);
  243. btrfs_init_path(path);
  244. ret = btrfs_lookup_inode(trans, root, path,
  245. &BTRFS_I(inode)->location, -1);
  246. BUG_ON(ret);
  247. ret = btrfs_del_item(trans, root, path);
  248. BUG_ON(ret);
  249. btrfs_free_path(path);
  250. return ret;
  251. }
  252. static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
  253. struct btrfs_root *root,
  254. struct inode *inode)
  255. {
  256. int ret;
  257. struct btrfs_path *path;
  258. struct btrfs_key key;
  259. struct btrfs_disk_key *found_key;
  260. struct btrfs_leaf *leaf;
  261. struct btrfs_file_extent_item *fi = NULL;
  262. u64 extent_start = 0;
  263. u64 extent_num_blocks = 0;
  264. int found_extent;
  265. path = btrfs_alloc_path();
  266. BUG_ON(!path);
  267. /* FIXME, add redo link to tree so we don't leak on crash */
  268. key.objectid = inode->i_ino;
  269. key.offset = (u64)-1;
  270. key.flags = 0;
  271. /*
  272. * use BTRFS_CSUM_ITEM_KEY because it is larger than inline keys
  273. * or extent data
  274. */
  275. btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY);
  276. while(1) {
  277. btrfs_init_path(path);
  278. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  279. if (ret < 0) {
  280. goto error;
  281. }
  282. if (ret > 0) {
  283. BUG_ON(path->slots[0] == 0);
  284. path->slots[0]--;
  285. }
  286. leaf = btrfs_buffer_leaf(path->nodes[0]);
  287. found_key = &leaf->items[path->slots[0]].key;
  288. if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
  289. break;
  290. if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY &&
  291. btrfs_disk_key_type(found_key) != BTRFS_INLINE_DATA_KEY &&
  292. btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY)
  293. break;
  294. if (btrfs_disk_key_offset(found_key) < inode->i_size)
  295. break;
  296. if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) {
  297. fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  298. path->slots[0],
  299. struct btrfs_file_extent_item);
  300. extent_start = btrfs_file_extent_disk_blocknr(fi);
  301. extent_num_blocks =
  302. btrfs_file_extent_disk_num_blocks(fi);
  303. inode->i_blocks -=
  304. btrfs_file_extent_num_blocks(fi) >> 9;
  305. found_extent = 1;
  306. } else {
  307. found_extent = 0;
  308. }
  309. ret = btrfs_del_item(trans, root, path);
  310. BUG_ON(ret);
  311. btrfs_release_path(root, path);
  312. if (found_extent) {
  313. ret = btrfs_free_extent(trans, root, extent_start,
  314. extent_num_blocks, 0);
  315. BUG_ON(ret);
  316. }
  317. }
  318. ret = 0;
  319. error:
  320. btrfs_release_path(root, path);
  321. btrfs_free_path(path);
  322. return ret;
  323. }
  324. static void btrfs_delete_inode(struct inode *inode)
  325. {
  326. struct btrfs_trans_handle *trans;
  327. struct btrfs_root *root = BTRFS_I(inode)->root;
  328. int ret;
  329. truncate_inode_pages(&inode->i_data, 0);
  330. if (is_bad_inode(inode)) {
  331. goto no_delete;
  332. }
  333. inode->i_size = 0;
  334. mutex_lock(&root->fs_info->fs_mutex);
  335. trans = btrfs_start_transaction(root, 1);
  336. if (S_ISREG(inode->i_mode)) {
  337. ret = btrfs_truncate_in_trans(trans, root, inode);
  338. BUG_ON(ret);
  339. }
  340. btrfs_free_inode(trans, root, inode);
  341. btrfs_end_transaction(trans, root);
  342. mutex_unlock(&root->fs_info->fs_mutex);
  343. return;
  344. no_delete:
  345. clear_inode(inode);
  346. }
  347. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  348. struct btrfs_key *location)
  349. {
  350. const char *name = dentry->d_name.name;
  351. int namelen = dentry->d_name.len;
  352. struct btrfs_dir_item *di;
  353. struct btrfs_path *path;
  354. struct btrfs_root *root = BTRFS_I(dir)->root;
  355. int ret;
  356. path = btrfs_alloc_path();
  357. BUG_ON(!path);
  358. btrfs_init_path(path);
  359. ret = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
  360. namelen, 0);
  361. if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) {
  362. location->objectid = 0;
  363. ret = 0;
  364. goto out;
  365. }
  366. di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  367. struct btrfs_dir_item);
  368. btrfs_disk_key_to_cpu(location, &di->location);
  369. out:
  370. btrfs_release_path(root, path);
  371. btrfs_free_path(path);
  372. return ret;
  373. }
  374. int fixup_tree_root_location(struct btrfs_root *root,
  375. struct btrfs_key *location,
  376. struct btrfs_root **sub_root)
  377. {
  378. struct btrfs_path *path;
  379. struct btrfs_root_item *ri;
  380. if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
  381. return 0;
  382. if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
  383. return 0;
  384. path = btrfs_alloc_path();
  385. BUG_ON(!path);
  386. mutex_lock(&root->fs_info->fs_mutex);
  387. *sub_root = btrfs_read_fs_root(root->fs_info, location);
  388. if (IS_ERR(*sub_root))
  389. return PTR_ERR(*sub_root);
  390. ri = &(*sub_root)->root_item;
  391. location->objectid = btrfs_root_dirid(ri);
  392. location->flags = 0;
  393. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  394. location->offset = 0;
  395. btrfs_free_path(path);
  396. mutex_unlock(&root->fs_info->fs_mutex);
  397. return 0;
  398. }
  399. int btrfs_init_locked_inode(struct inode *inode, void *p)
  400. {
  401. struct btrfs_iget_args *args = p;
  402. inode->i_ino = args->ino;
  403. BTRFS_I(inode)->root = args->root;
  404. return 0;
  405. }
  406. int btrfs_find_actor(struct inode *inode, void *opaque)
  407. {
  408. struct btrfs_iget_args *args = opaque;
  409. return (args->ino == inode->i_ino &&
  410. args->root == BTRFS_I(inode)->root);
  411. }
  412. struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
  413. struct btrfs_root *root)
  414. {
  415. struct inode *inode;
  416. struct btrfs_iget_args args;
  417. args.ino = objectid;
  418. args.root = root;
  419. inode = iget5_locked(s, objectid, btrfs_find_actor,
  420. btrfs_init_locked_inode,
  421. (void *)&args);
  422. return inode;
  423. }
  424. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  425. struct nameidata *nd)
  426. {
  427. struct inode * inode;
  428. struct btrfs_inode *bi = BTRFS_I(dir);
  429. struct btrfs_root *root = bi->root;
  430. struct btrfs_root *sub_root = root;
  431. struct btrfs_key location;
  432. int ret;
  433. if (dentry->d_name.len > BTRFS_NAME_LEN)
  434. return ERR_PTR(-ENAMETOOLONG);
  435. mutex_lock(&root->fs_info->fs_mutex);
  436. ret = btrfs_inode_by_name(dir, dentry, &location);
  437. mutex_unlock(&root->fs_info->fs_mutex);
  438. if (ret < 0)
  439. return ERR_PTR(ret);
  440. inode = NULL;
  441. if (location.objectid) {
  442. ret = fixup_tree_root_location(root, &location, &sub_root);
  443. if (ret < 0)
  444. return ERR_PTR(ret);
  445. if (ret > 0)
  446. return ERR_PTR(-ENOENT);
  447. inode = btrfs_iget_locked(dir->i_sb, location.objectid,
  448. sub_root);
  449. if (!inode)
  450. return ERR_PTR(-EACCES);
  451. if (inode->i_state & I_NEW) {
  452. if (sub_root != root) {
  453. ret = radix_tree_insert(
  454. &root->fs_info->fs_roots_radix,
  455. (unsigned long)sub_root,
  456. sub_root);
  457. printk("adding new root for inode %lu root %p (found %p)\n", inode->i_ino, sub_root, BTRFS_I(inode)->root);
  458. igrab(inode);
  459. sub_root->inode = inode;
  460. }
  461. BTRFS_I(inode)->root = sub_root;
  462. memcpy(&BTRFS_I(inode)->location, &location,
  463. sizeof(location));
  464. btrfs_read_locked_inode(inode);
  465. unlock_new_inode(inode);
  466. }
  467. }
  468. return d_splice_alias(inode, dentry);
  469. }
  470. static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
  471. {
  472. struct inode *inode = filp->f_path.dentry->d_inode;
  473. struct btrfs_root *root = BTRFS_I(inode)->root;
  474. struct btrfs_item *item;
  475. struct btrfs_dir_item *di;
  476. struct btrfs_key key;
  477. struct btrfs_path *path;
  478. int ret;
  479. u32 nritems;
  480. struct btrfs_leaf *leaf;
  481. int slot;
  482. int advance;
  483. unsigned char d_type = DT_UNKNOWN;
  484. int over = 0;
  485. int key_type = BTRFS_DIR_INDEX_KEY;
  486. /* FIXME, use a real flag for deciding about the key type */
  487. if (root->fs_info->tree_root == root)
  488. key_type = BTRFS_DIR_ITEM_KEY;
  489. mutex_lock(&root->fs_info->fs_mutex);
  490. key.objectid = inode->i_ino;
  491. key.flags = 0;
  492. btrfs_set_key_type(&key, key_type);
  493. key.offset = filp->f_pos;
  494. path = btrfs_alloc_path();
  495. btrfs_init_path(path);
  496. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  497. if (ret < 0)
  498. goto err;
  499. advance = 0;
  500. while(1) {
  501. leaf = btrfs_buffer_leaf(path->nodes[0]);
  502. nritems = btrfs_header_nritems(&leaf->header);
  503. slot = path->slots[0];
  504. if (advance || slot >= nritems) {
  505. if (slot >= nritems -1) {
  506. ret = btrfs_next_leaf(root, path);
  507. if (ret)
  508. break;
  509. leaf = btrfs_buffer_leaf(path->nodes[0]);
  510. nritems = btrfs_header_nritems(&leaf->header);
  511. slot = path->slots[0];
  512. } else {
  513. slot++;
  514. path->slots[0]++;
  515. }
  516. }
  517. advance = 1;
  518. item = leaf->items + slot;
  519. if (btrfs_disk_key_objectid(&item->key) != key.objectid)
  520. break;
  521. if (key_type == BTRFS_DIR_INDEX_KEY &&
  522. btrfs_disk_key_offset(&item->key) > root->highest_inode)
  523. break;
  524. if (btrfs_disk_key_type(&item->key) != key_type)
  525. continue;
  526. if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
  527. continue;
  528. filp->f_pos = btrfs_disk_key_offset(&item->key);
  529. advance = 1;
  530. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  531. over = filldir(dirent, (const char *)(di + 1),
  532. btrfs_dir_name_len(di),
  533. btrfs_disk_key_offset(&item->key),
  534. btrfs_disk_key_objectid(&di->location), d_type);
  535. if (over)
  536. goto nopos;
  537. }
  538. filp->f_pos++;
  539. nopos:
  540. ret = 0;
  541. err:
  542. btrfs_release_path(root, path);
  543. btrfs_free_path(path);
  544. mutex_unlock(&root->fs_info->fs_mutex);
  545. return ret;
  546. }
  547. static void btrfs_put_super (struct super_block * sb)
  548. {
  549. struct btrfs_root *root = btrfs_sb(sb);
  550. int ret;
  551. ret = close_ctree(root);
  552. if (ret) {
  553. printk("close ctree returns %d\n", ret);
  554. }
  555. sb->s_fs_info = NULL;
  556. }
  557. static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
  558. {
  559. struct inode * inode;
  560. struct dentry * root_dentry;
  561. struct btrfs_super_block *disk_super;
  562. struct btrfs_root *tree_root;
  563. struct btrfs_inode *bi;
  564. sb->s_maxbytes = MAX_LFS_FILESIZE;
  565. sb->s_magic = BTRFS_SUPER_MAGIC;
  566. sb->s_op = &btrfs_super_ops;
  567. sb->s_time_gran = 1;
  568. tree_root = open_ctree(sb);
  569. if (!tree_root) {
  570. printk("btrfs: open_ctree failed\n");
  571. return -EIO;
  572. }
  573. sb->s_fs_info = tree_root;
  574. disk_super = tree_root->fs_info->disk_super;
  575. printk("read in super total blocks %Lu root %Lu\n",
  576. btrfs_super_total_blocks(disk_super),
  577. btrfs_super_root_dir(disk_super));
  578. inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
  579. tree_root);
  580. bi = BTRFS_I(inode);
  581. bi->location.objectid = inode->i_ino;
  582. bi->location.offset = 0;
  583. bi->location.flags = 0;
  584. bi->root = tree_root;
  585. btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
  586. if (!inode)
  587. return -ENOMEM;
  588. if (inode->i_state & I_NEW) {
  589. btrfs_read_locked_inode(inode);
  590. unlock_new_inode(inode);
  591. }
  592. root_dentry = d_alloc_root(inode);
  593. if (!root_dentry) {
  594. iput(inode);
  595. return -ENOMEM;
  596. }
  597. sb->s_root = root_dentry;
  598. return 0;
  599. }
  600. static void fill_inode_item(struct btrfs_inode_item *item,
  601. struct inode *inode)
  602. {
  603. btrfs_set_inode_uid(item, inode->i_uid);
  604. btrfs_set_inode_gid(item, inode->i_gid);
  605. btrfs_set_inode_size(item, inode->i_size);
  606. btrfs_set_inode_mode(item, inode->i_mode);
  607. btrfs_set_inode_nlink(item, inode->i_nlink);
  608. btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
  609. btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
  610. btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
  611. btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
  612. btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
  613. btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
  614. btrfs_set_inode_nblocks(item, inode->i_blocks);
  615. btrfs_set_inode_generation(item, inode->i_generation);
  616. }
  617. static int btrfs_update_inode(struct btrfs_trans_handle *trans,
  618. struct btrfs_root *root,
  619. struct inode *inode)
  620. {
  621. struct btrfs_inode_item *inode_item;
  622. struct btrfs_path *path;
  623. int ret;
  624. path = btrfs_alloc_path();
  625. BUG_ON(!path);
  626. btrfs_init_path(path);
  627. ret = btrfs_lookup_inode(trans, root, path,
  628. &BTRFS_I(inode)->location, 1);
  629. if (ret) {
  630. if (ret > 0)
  631. ret = -ENOENT;
  632. goto failed;
  633. }
  634. inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  635. path->slots[0],
  636. struct btrfs_inode_item);
  637. fill_inode_item(inode_item, inode);
  638. btrfs_mark_buffer_dirty(path->nodes[0]);
  639. ret = 0;
  640. failed:
  641. btrfs_release_path(root, path);
  642. btrfs_free_path(path);
  643. return ret;
  644. }
  645. static int btrfs_write_inode(struct inode *inode, int wait)
  646. {
  647. struct btrfs_root *root = BTRFS_I(inode)->root;
  648. struct btrfs_trans_handle *trans;
  649. int ret;
  650. mutex_lock(&root->fs_info->fs_mutex);
  651. trans = btrfs_start_transaction(root, 1);
  652. ret = btrfs_update_inode(trans, root, inode);
  653. if (wait)
  654. btrfs_commit_transaction(trans, root);
  655. else
  656. btrfs_end_transaction(trans, root);
  657. mutex_unlock(&root->fs_info->fs_mutex);
  658. return ret;
  659. }
  660. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  661. struct inode *dir, int mode)
  662. {
  663. struct inode *inode;
  664. struct btrfs_inode_item inode_item;
  665. struct btrfs_root *root = BTRFS_I(dir)->root;
  666. struct btrfs_key *location;
  667. int ret;
  668. u64 objectid;
  669. inode = new_inode(dir->i_sb);
  670. if (!inode)
  671. return ERR_PTR(-ENOMEM);
  672. BTRFS_I(inode)->root = BTRFS_I(dir)->root;
  673. ret = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  674. BUG_ON(ret);
  675. inode->i_uid = current->fsuid;
  676. inode->i_gid = current->fsgid;
  677. inode->i_mode = mode;
  678. inode->i_ino = objectid;
  679. inode->i_blocks = 0;
  680. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  681. fill_inode_item(&inode_item, inode);
  682. location = &BTRFS_I(inode)->location;
  683. location->objectid = objectid;
  684. location->flags = 0;
  685. location->offset = 0;
  686. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  687. ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
  688. BUG_ON(ret);
  689. insert_inode_hash(inode);
  690. return inode;
  691. }
  692. static int btrfs_add_link(struct btrfs_trans_handle *trans,
  693. struct dentry *dentry, struct inode *inode)
  694. {
  695. int ret;
  696. struct btrfs_key key;
  697. struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
  698. key.objectid = inode->i_ino;
  699. key.flags = 0;
  700. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  701. key.offset = 0;
  702. ret = btrfs_insert_dir_item(trans, root,
  703. dentry->d_name.name, dentry->d_name.len,
  704. dentry->d_parent->d_inode->i_ino,
  705. &key, 0);
  706. if (ret == 0) {
  707. dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
  708. ret = btrfs_update_inode(trans, root,
  709. dentry->d_parent->d_inode);
  710. }
  711. return ret;
  712. }
  713. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  714. struct dentry *dentry, struct inode *inode)
  715. {
  716. int err = btrfs_add_link(trans, dentry, inode);
  717. if (!err) {
  718. d_instantiate(dentry, inode);
  719. return 0;
  720. }
  721. if (err > 0)
  722. err = -EEXIST;
  723. return err;
  724. }
  725. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  726. int mode, struct nameidata *nd)
  727. {
  728. struct btrfs_trans_handle *trans;
  729. struct btrfs_root *root = BTRFS_I(dir)->root;
  730. struct inode *inode;
  731. int err;
  732. int drop_inode = 0;
  733. mutex_lock(&root->fs_info->fs_mutex);
  734. trans = btrfs_start_transaction(root, 1);
  735. inode = btrfs_new_inode(trans, dir, mode);
  736. err = PTR_ERR(inode);
  737. if (IS_ERR(inode))
  738. goto out_unlock;
  739. // FIXME mark the inode dirty
  740. err = btrfs_add_nondir(trans, dentry, inode);
  741. if (err)
  742. drop_inode = 1;
  743. else {
  744. inode->i_mapping->a_ops = &btrfs_aops;
  745. inode->i_fop = &btrfs_file_operations;
  746. inode->i_op = &btrfs_file_inode_operations;
  747. }
  748. dir->i_sb->s_dirt = 1;
  749. out_unlock:
  750. btrfs_end_transaction(trans, root);
  751. mutex_unlock(&root->fs_info->fs_mutex);
  752. if (drop_inode) {
  753. inode_dec_link_count(inode);
  754. iput(inode);
  755. }
  756. return err;
  757. }
  758. static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
  759. struct inode *inode, struct inode *dir)
  760. {
  761. struct btrfs_root *root = BTRFS_I(dir)->root;
  762. int ret;
  763. char buf[2];
  764. struct btrfs_key key;
  765. buf[0] = '.';
  766. buf[1] = '.';
  767. key.objectid = inode->i_ino;
  768. key.offset = 0;
  769. key.flags = 0;
  770. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  771. ret = btrfs_insert_dir_item(trans, root, buf, 1, inode->i_ino,
  772. &key, 1);
  773. if (ret)
  774. goto error;
  775. key.objectid = dir->i_ino;
  776. ret = btrfs_insert_dir_item(trans, root, buf, 2, inode->i_ino,
  777. &key, 1);
  778. if (ret)
  779. goto error;
  780. inode->i_size = 6;
  781. ret = btrfs_update_inode(trans, root, inode);
  782. error:
  783. return ret;
  784. }
  785. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  786. {
  787. struct inode *inode;
  788. struct btrfs_trans_handle *trans;
  789. struct btrfs_root *root = BTRFS_I(dir)->root;
  790. int err = 0;
  791. int drop_on_err = 0;
  792. mutex_lock(&root->fs_info->fs_mutex);
  793. trans = btrfs_start_transaction(root, 1);
  794. if (IS_ERR(trans)) {
  795. err = PTR_ERR(trans);
  796. goto out_unlock;
  797. }
  798. inode = btrfs_new_inode(trans, dir, S_IFDIR | mode);
  799. if (IS_ERR(inode)) {
  800. err = PTR_ERR(inode);
  801. goto out_fail;
  802. }
  803. drop_on_err = 1;
  804. inode->i_op = &btrfs_dir_inode_operations;
  805. inode->i_fop = &btrfs_dir_file_operations;
  806. err = btrfs_make_empty_dir(trans, inode, dir);
  807. if (err)
  808. goto out_fail;
  809. err = btrfs_add_link(trans, dentry, inode);
  810. if (err)
  811. goto out_fail;
  812. d_instantiate(dentry, inode);
  813. drop_on_err = 0;
  814. out_fail:
  815. btrfs_end_transaction(trans, root);
  816. out_unlock:
  817. mutex_unlock(&root->fs_info->fs_mutex);
  818. if (drop_on_err)
  819. iput(inode);
  820. return err;
  821. }
  822. static int btrfs_sync_fs(struct super_block *sb, int wait)
  823. {
  824. struct btrfs_trans_handle *trans;
  825. struct btrfs_root *root;
  826. int ret;
  827. root = btrfs_sb(sb);
  828. sb->s_dirt = 0;
  829. if (!wait) {
  830. filemap_flush(root->fs_info->btree_inode->i_mapping);
  831. return 0;
  832. }
  833. filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
  834. mutex_lock(&root->fs_info->fs_mutex);
  835. trans = btrfs_start_transaction(root, 1);
  836. ret = btrfs_commit_transaction(trans, root);
  837. sb->s_dirt = 0;
  838. BUG_ON(ret);
  839. printk("btrfs sync_fs\n");
  840. mutex_unlock(&root->fs_info->fs_mutex);
  841. return 0;
  842. }
  843. #if 0
  844. static int btrfs_get_block_inline(struct inode *inode, sector_t iblock,
  845. struct buffer_head *result, int create)
  846. {
  847. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  848. struct btrfs_path *path;
  849. struct btrfs_key key;
  850. struct btrfs_leaf *leaf;
  851. int num_bytes = result->b_size;
  852. int item_size;
  853. int ret;
  854. u64 pos;
  855. char *ptr;
  856. int copy_size;
  857. int err = 0;
  858. char *safe_ptr;
  859. char *data_ptr;
  860. path = btrfs_alloc_path();
  861. BUG_ON(!path);
  862. WARN_ON(create);
  863. if (create) {
  864. return 0;
  865. }
  866. pos = iblock << inode->i_blkbits;
  867. key.objectid = inode->i_ino;
  868. key.flags = 0;
  869. btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
  870. ptr = kmap(result->b_page);
  871. safe_ptr = ptr;
  872. ptr += (pos & (PAGE_CACHE_SIZE -1));
  873. again:
  874. key.offset = pos;
  875. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  876. if (ret) {
  877. if (ret < 0)
  878. err = ret;
  879. else
  880. err = 0;
  881. goto out;
  882. }
  883. leaf = btrfs_buffer_leaf(path->nodes[0]);
  884. item_size = btrfs_item_size(leaf->items + path->slots[0]);
  885. copy_size = min(num_bytes, item_size);
  886. data_ptr = btrfs_item_ptr(leaf, path->slots[0], char);
  887. WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size);
  888. memcpy(ptr, data_ptr, copy_size);
  889. pos += copy_size;
  890. num_bytes -= copy_size;
  891. WARN_ON(num_bytes < 0);
  892. ptr += copy_size;
  893. btrfs_release_path(root, path);
  894. if (num_bytes != 0) {
  895. if (pos >= i_size_read(inode))
  896. memset(ptr, 0, num_bytes);
  897. else
  898. goto again;
  899. }
  900. set_buffer_uptodate(result);
  901. map_bh(result, inode->i_sb, 0);
  902. err = 0;
  903. out:
  904. btrfs_free_path(path);
  905. kunmap(result->b_page);
  906. return err;
  907. }
  908. #endif
  909. static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
  910. struct buffer_head *result, int create)
  911. {
  912. int ret;
  913. int err = 0;
  914. u64 blocknr;
  915. u64 extent_start = 0;
  916. u64 extent_end = 0;
  917. u64 objectid = inode->i_ino;
  918. struct btrfs_path *path;
  919. struct btrfs_root *root = BTRFS_I(inode)->root;
  920. struct btrfs_trans_handle *trans = NULL;
  921. struct btrfs_file_extent_item *item;
  922. struct btrfs_leaf *leaf;
  923. struct btrfs_disk_key *found_key;
  924. path = btrfs_alloc_path();
  925. BUG_ON(!path);
  926. btrfs_init_path(path);
  927. if (create)
  928. trans = btrfs_start_transaction(root, 1);
  929. ret = btrfs_lookup_file_extent(trans, root, path,
  930. inode->i_ino,
  931. iblock << inode->i_blkbits, create);
  932. if (ret < 0) {
  933. err = ret;
  934. goto out;
  935. }
  936. if (ret != 0) {
  937. if (path->slots[0] == 0) {
  938. btrfs_release_path(root, path);
  939. goto allocate;
  940. }
  941. path->slots[0]--;
  942. }
  943. item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  944. struct btrfs_file_extent_item);
  945. leaf = btrfs_buffer_leaf(path->nodes[0]);
  946. blocknr = btrfs_file_extent_disk_blocknr(item);
  947. blocknr += btrfs_file_extent_offset(item);
  948. /* exact match found, use it, FIXME, deal with extents
  949. * other than the page size
  950. */
  951. if (ret == 0) {
  952. err = 0;
  953. BUG_ON(btrfs_file_extent_disk_num_blocks(item) != 1);
  954. if (create &&
  955. btrfs_file_extent_generation(item) != trans->transid) {
  956. struct btrfs_key ins;
  957. ret = btrfs_alloc_extent(trans, root, 1,
  958. blocknr, (u64)-1, &ins);
  959. BUG_ON(ret);
  960. btrfs_set_file_extent_disk_blocknr(item, ins.objectid);
  961. mark_buffer_dirty(path->nodes[0]);
  962. ret = btrfs_free_extent(trans, root,
  963. blocknr, 1, 0);
  964. BUG_ON(ret);
  965. blocknr = ins.objectid;
  966. }
  967. map_bh(result, inode->i_sb, blocknr);
  968. goto out;
  969. }
  970. /* are we inside the extent that was found? */
  971. found_key = &leaf->items[path->slots[0]].key;
  972. if (btrfs_disk_key_objectid(found_key) != objectid ||
  973. btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
  974. extent_end = 0;
  975. extent_start = 0;
  976. btrfs_release_path(root, path);
  977. goto allocate;
  978. }
  979. extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
  980. extent_start = extent_start >> inode->i_blkbits;
  981. extent_start += btrfs_file_extent_offset(item);
  982. extent_end = extent_start + btrfs_file_extent_num_blocks(item);
  983. if (iblock >= extent_start && iblock < extent_end) {
  984. err = 0;
  985. map_bh(result, inode->i_sb, blocknr + iblock - extent_start);
  986. goto out;
  987. }
  988. allocate:
  989. /* ok, create a new extent */
  990. if (!create) {
  991. err = 0;
  992. goto out;
  993. }
  994. ret = btrfs_alloc_file_extent(trans, root, objectid,
  995. iblock << inode->i_blkbits,
  996. 1, extent_end, &blocknr);
  997. if (ret) {
  998. err = ret;
  999. goto out;
  1000. }
  1001. inode->i_blocks += inode->i_sb->s_blocksize >> 9;
  1002. set_buffer_new(result);
  1003. map_bh(result, inode->i_sb, blocknr);
  1004. out:
  1005. btrfs_release_path(root, path);
  1006. btrfs_free_path(path);
  1007. if (trans)
  1008. btrfs_end_transaction(trans, root);
  1009. return err;
  1010. }
  1011. static int btrfs_get_block(struct inode *inode, sector_t iblock,
  1012. struct buffer_head *result, int create)
  1013. {
  1014. int err;
  1015. struct btrfs_root *root = BTRFS_I(inode)->root;
  1016. mutex_lock(&root->fs_info->fs_mutex);
  1017. err = btrfs_get_block_lock(inode, iblock, result, create);
  1018. // err = btrfs_get_block_inline(inode, iblock, result, create);
  1019. mutex_unlock(&root->fs_info->fs_mutex);
  1020. return err;
  1021. }
  1022. static int btrfs_prepare_write(struct file *file, struct page *page,
  1023. unsigned from, unsigned to)
  1024. {
  1025. return nobh_prepare_write(page, from, to, btrfs_get_block);
  1026. }
  1027. static int btrfs_commit_write(struct file *file, struct page *page,
  1028. unsigned from, unsigned to)
  1029. {
  1030. return nobh_commit_write(file, page, from, to);
  1031. }
  1032. static void btrfs_write_super(struct super_block *sb)
  1033. {
  1034. btrfs_sync_fs(sb, 1);
  1035. }
  1036. static int btrfs_readpage(struct file *file, struct page *page)
  1037. {
  1038. return mpage_readpage(page, btrfs_get_block);
  1039. }
  1040. static int btrfs_readpages(struct file *file, struct address_space *mapping,
  1041. struct list_head *pages, unsigned nr_pages)
  1042. {
  1043. return mpage_readpages(mapping, pages, nr_pages, btrfs_get_block);
  1044. }
  1045. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  1046. {
  1047. return nobh_writepage(page, btrfs_get_block, wbc);
  1048. }
  1049. static void btrfs_truncate(struct inode *inode)
  1050. {
  1051. struct btrfs_root *root = BTRFS_I(inode)->root;
  1052. int ret;
  1053. struct btrfs_trans_handle *trans;
  1054. if (!S_ISREG(inode->i_mode))
  1055. return;
  1056. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1057. return;
  1058. nobh_truncate_page(inode->i_mapping, inode->i_size);
  1059. /* FIXME, add redo link to tree so we don't leak on crash */
  1060. mutex_lock(&root->fs_info->fs_mutex);
  1061. trans = btrfs_start_transaction(root, 1);
  1062. ret = btrfs_truncate_in_trans(trans, root, inode);
  1063. BUG_ON(ret);
  1064. ret = btrfs_end_transaction(trans, root);
  1065. BUG_ON(ret);
  1066. mutex_unlock(&root->fs_info->fs_mutex);
  1067. mark_inode_dirty(inode);
  1068. }
  1069. static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
  1070. struct page **prepared_pages,
  1071. const char __user * buf)
  1072. {
  1073. long page_fault = 0;
  1074. int i;
  1075. int offset = pos & (PAGE_CACHE_SIZE - 1);
  1076. for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
  1077. size_t count = min_t(size_t,
  1078. PAGE_CACHE_SIZE - offset, write_bytes);
  1079. struct page *page = prepared_pages[i];
  1080. fault_in_pages_readable(buf, count);
  1081. /* Copy data from userspace to the current page */
  1082. kmap(page);
  1083. page_fault = __copy_from_user(page_address(page) + offset,
  1084. buf, count);
  1085. /* Flush processor's dcache for this page */
  1086. flush_dcache_page(page);
  1087. kunmap(page);
  1088. buf += count;
  1089. write_bytes -= count;
  1090. if (page_fault)
  1091. break;
  1092. }
  1093. return page_fault ? -EFAULT : 0;
  1094. }
  1095. static void btrfs_drop_pages(struct page **pages, size_t num_pages)
  1096. {
  1097. size_t i;
  1098. for (i = 0; i < num_pages; i++) {
  1099. if (!pages[i])
  1100. break;
  1101. unlock_page(pages[i]);
  1102. mark_page_accessed(pages[i]);
  1103. page_cache_release(pages[i]);
  1104. }
  1105. }
  1106. static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
  1107. struct btrfs_root *root,
  1108. struct file *file,
  1109. struct page **pages,
  1110. size_t num_pages,
  1111. loff_t pos,
  1112. size_t write_bytes)
  1113. {
  1114. int i;
  1115. int offset;
  1116. int err = 0;
  1117. int ret;
  1118. int this_write;
  1119. struct inode *inode = file->f_path.dentry->d_inode;
  1120. for (i = 0; i < num_pages; i++) {
  1121. offset = pos & (PAGE_CACHE_SIZE -1);
  1122. this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
  1123. /* FIXME, one block at a time */
  1124. mutex_lock(&root->fs_info->fs_mutex);
  1125. trans = btrfs_start_transaction(root, 1);
  1126. btrfs_csum_file_block(trans, root, inode->i_ino,
  1127. pages[i]->index << PAGE_CACHE_SHIFT,
  1128. kmap(pages[i]), PAGE_CACHE_SIZE);
  1129. kunmap(pages[i]);
  1130. SetPageChecked(pages[i]);
  1131. ret = btrfs_end_transaction(trans, root);
  1132. BUG_ON(ret);
  1133. mutex_unlock(&root->fs_info->fs_mutex);
  1134. ret = nobh_commit_write(file, pages[i], offset,
  1135. offset + this_write);
  1136. pos += this_write;
  1137. if (ret) {
  1138. err = ret;
  1139. goto failed;
  1140. }
  1141. WARN_ON(this_write > write_bytes);
  1142. write_bytes -= this_write;
  1143. }
  1144. failed:
  1145. return err;
  1146. }
  1147. static int prepare_pages(struct btrfs_trans_handle *trans,
  1148. struct btrfs_root *root,
  1149. struct file *file,
  1150. struct page **pages,
  1151. size_t num_pages,
  1152. loff_t pos,
  1153. unsigned long first_index,
  1154. unsigned long last_index,
  1155. size_t write_bytes)
  1156. {
  1157. int i;
  1158. unsigned long index = pos >> PAGE_CACHE_SHIFT;
  1159. struct inode *inode = file->f_path.dentry->d_inode;
  1160. int offset;
  1161. int err = 0;
  1162. int ret;
  1163. int this_write;
  1164. loff_t isize = i_size_read(inode);
  1165. memset(pages, 0, num_pages * sizeof(struct page *));
  1166. for (i = 0; i < num_pages; i++) {
  1167. pages[i] = grab_cache_page(inode->i_mapping, index + i);
  1168. if (!pages[i]) {
  1169. err = -ENOMEM;
  1170. goto failed_release;
  1171. }
  1172. offset = pos & (PAGE_CACHE_SIZE -1);
  1173. this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
  1174. if (!PageUptodate(pages[i]) &&
  1175. (pages[i]->index == first_index ||
  1176. pages[i]->index == last_index) && pos < isize) {
  1177. ret = mpage_readpage(pages[i], btrfs_get_block);
  1178. BUG_ON(ret);
  1179. lock_page(pages[i]);
  1180. }
  1181. ret = nobh_prepare_write(pages[i], offset,
  1182. offset + this_write,
  1183. btrfs_get_block);
  1184. pos += this_write;
  1185. if (ret) {
  1186. err = ret;
  1187. goto failed_truncate;
  1188. }
  1189. WARN_ON(this_write > write_bytes);
  1190. write_bytes -= this_write;
  1191. }
  1192. return 0;
  1193. failed_release:
  1194. btrfs_drop_pages(pages, num_pages);
  1195. return err;
  1196. failed_truncate:
  1197. btrfs_drop_pages(pages, num_pages);
  1198. if (pos > isize)
  1199. vmtruncate(inode, isize);
  1200. return err;
  1201. }
  1202. static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
  1203. size_t count, loff_t *ppos)
  1204. {
  1205. loff_t pos;
  1206. size_t num_written = 0;
  1207. int err = 0;
  1208. int ret = 0;
  1209. struct inode *inode = file->f_path.dentry->d_inode;
  1210. struct btrfs_root *root = BTRFS_I(inode)->root;
  1211. struct page *pages[1];
  1212. unsigned long first_index;
  1213. unsigned long last_index;
  1214. if (file->f_flags & O_DIRECT)
  1215. return -EINVAL;
  1216. pos = *ppos;
  1217. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1218. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  1219. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  1220. if (err)
  1221. goto out;
  1222. if (count == 0)
  1223. goto out;
  1224. err = remove_suid(file->f_path.dentry);
  1225. if (err)
  1226. goto out;
  1227. file_update_time(file);
  1228. mutex_lock(&inode->i_mutex);
  1229. first_index = pos >> PAGE_CACHE_SHIFT;
  1230. last_index = (pos + count) >> PAGE_CACHE_SHIFT;
  1231. while(count > 0) {
  1232. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  1233. size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
  1234. size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
  1235. PAGE_CACHE_SHIFT;
  1236. ret = prepare_pages(NULL, root, file, pages, num_pages,
  1237. pos, first_index, last_index, write_bytes);
  1238. BUG_ON(ret);
  1239. ret = btrfs_copy_from_user(pos, num_pages,
  1240. write_bytes, pages, buf);
  1241. BUG_ON(ret);
  1242. ret = dirty_and_release_pages(NULL, root, file, pages,
  1243. num_pages, pos, write_bytes);
  1244. BUG_ON(ret);
  1245. btrfs_drop_pages(pages, num_pages);
  1246. buf += write_bytes;
  1247. count -= write_bytes;
  1248. pos += write_bytes;
  1249. num_written += write_bytes;
  1250. balance_dirty_pages_ratelimited(inode->i_mapping);
  1251. cond_resched();
  1252. }
  1253. mutex_unlock(&inode->i_mutex);
  1254. out:
  1255. *ppos = pos;
  1256. current->backing_dev_info = NULL;
  1257. return num_written ? num_written : err;
  1258. }
  1259. #if 0
  1260. static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode,
  1261. struct page *page, loff_t pos,
  1262. size_t offset, size_t write_bytes)
  1263. {
  1264. struct btrfs_path *path;
  1265. struct btrfs_trans_handle *trans;
  1266. struct btrfs_key key;
  1267. struct btrfs_leaf *leaf;
  1268. struct btrfs_key found_key;
  1269. int ret;
  1270. size_t copy_size = 0;
  1271. char *dst = NULL;
  1272. int err = 0;
  1273. size_t num_written = 0;
  1274. path = btrfs_alloc_path();
  1275. BUG_ON(!path);
  1276. mutex_lock(&root->fs_info->fs_mutex);
  1277. trans = btrfs_start_transaction(root, 1);
  1278. key.objectid = inode->i_ino;
  1279. key.flags = 0;
  1280. btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
  1281. again:
  1282. key.offset = pos;
  1283. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1284. if (ret < 0) {
  1285. err = ret;
  1286. goto out;
  1287. }
  1288. if (ret == 0) {
  1289. leaf = btrfs_buffer_leaf(path->nodes[0]);
  1290. btrfs_disk_key_to_cpu(&found_key,
  1291. &leaf->items[path->slots[0]].key);
  1292. copy_size = btrfs_item_size(leaf->items + path->slots[0]);
  1293. dst = btrfs_item_ptr(leaf, path->slots[0], char);
  1294. copy_size = min(write_bytes, copy_size);
  1295. goto copyit;
  1296. } else {
  1297. int slot = path->slots[0];
  1298. if (slot > 0) {
  1299. slot--;
  1300. }
  1301. // FIXME find max key
  1302. leaf = btrfs_buffer_leaf(path->nodes[0]);
  1303. btrfs_disk_key_to_cpu(&found_key,
  1304. &leaf->items[slot].key);
  1305. if (found_key.objectid != inode->i_ino)
  1306. goto insert;
  1307. if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY)
  1308. goto insert;
  1309. copy_size = btrfs_item_size(leaf->items + slot);
  1310. if (found_key.offset + copy_size <= pos)
  1311. goto insert;
  1312. dst = btrfs_item_ptr(leaf, path->slots[0], char);
  1313. dst += pos - found_key.offset;
  1314. copy_size = copy_size - (pos - found_key.offset);
  1315. BUG_ON(copy_size < 0);
  1316. copy_size = min(write_bytes, copy_size);
  1317. WARN_ON(copy_size == 0);
  1318. goto copyit;
  1319. }
  1320. insert:
  1321. btrfs_release_path(root, path);
  1322. copy_size = min(write_bytes,
  1323. (size_t)BTRFS_LEAF_DATA_SIZE(root) -
  1324. sizeof(struct btrfs_item) * 4);
  1325. ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size);
  1326. BUG_ON(ret);
  1327. dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  1328. path->slots[0], char);
  1329. copyit:
  1330. WARN_ON(copy_size == 0);
  1331. WARN_ON(dst + copy_size >
  1332. btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  1333. path->slots[0], char) +
  1334. btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items +
  1335. path->slots[0]));
  1336. btrfs_memcpy(root, path->nodes[0]->b_data, dst,
  1337. page_address(page) + offset, copy_size);
  1338. mark_buffer_dirty(path->nodes[0]);
  1339. btrfs_release_path(root, path);
  1340. pos += copy_size;
  1341. offset += copy_size;
  1342. num_written += copy_size;
  1343. write_bytes -= copy_size;
  1344. if (write_bytes)
  1345. goto again;
  1346. out:
  1347. btrfs_free_path(path);
  1348. ret = btrfs_end_transaction(trans, root);
  1349. BUG_ON(ret);
  1350. mutex_unlock(&root->fs_info->fs_mutex);
  1351. return num_written ? num_written : err;
  1352. }
  1353. static ssize_t btrfs_file_inline_write(struct file *file,
  1354. const char __user *buf,
  1355. size_t count, loff_t *ppos)
  1356. {
  1357. loff_t pos;
  1358. size_t num_written = 0;
  1359. int err = 0;
  1360. int ret = 0;
  1361. struct inode *inode = file->f_path.dentry->d_inode;
  1362. struct btrfs_root *root = BTRFS_I(inode)->root;
  1363. unsigned long page_index;
  1364. if (file->f_flags & O_DIRECT)
  1365. return -EINVAL;
  1366. pos = *ppos;
  1367. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1368. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  1369. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  1370. if (err)
  1371. goto out;
  1372. if (count == 0)
  1373. goto out;
  1374. err = remove_suid(file->f_path.dentry);
  1375. if (err)
  1376. goto out;
  1377. file_update_time(file);
  1378. mutex_lock(&inode->i_mutex);
  1379. while(count > 0) {
  1380. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  1381. size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
  1382. struct page *page;
  1383. page_index = pos >> PAGE_CACHE_SHIFT;
  1384. page = grab_cache_page(inode->i_mapping, page_index);
  1385. if (!PageUptodate(page)) {
  1386. ret = mpage_readpage(page, btrfs_get_block);
  1387. BUG_ON(ret);
  1388. lock_page(page);
  1389. }
  1390. ret = btrfs_copy_from_user(pos, 1,
  1391. write_bytes, &page, buf);
  1392. BUG_ON(ret);
  1393. write_bytes = inline_one_page(root, inode, page, pos,
  1394. offset, write_bytes);
  1395. SetPageUptodate(page);
  1396. if (write_bytes > 0 && pos + write_bytes > inode->i_size) {
  1397. i_size_write(inode, pos + write_bytes);
  1398. mark_inode_dirty(inode);
  1399. }
  1400. page_cache_release(page);
  1401. unlock_page(page);
  1402. if (write_bytes < 0)
  1403. goto out_unlock;
  1404. buf += write_bytes;
  1405. count -= write_bytes;
  1406. pos += write_bytes;
  1407. num_written += write_bytes;
  1408. balance_dirty_pages_ratelimited(inode->i_mapping);
  1409. cond_resched();
  1410. }
  1411. out_unlock:
  1412. mutex_unlock(&inode->i_mutex);
  1413. out:
  1414. *ppos = pos;
  1415. current->backing_dev_info = NULL;
  1416. return num_written ? num_written : err;
  1417. }
  1418. #endif
  1419. static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
  1420. unsigned long offset, unsigned long size)
  1421. {
  1422. char *kaddr;
  1423. unsigned long left, count = desc->count;
  1424. struct inode *inode = page->mapping->host;
  1425. if (size > count)
  1426. size = count;
  1427. if (!PageChecked(page)) {
  1428. /* FIXME, do it per block */
  1429. struct btrfs_root *root = BTRFS_I(inode)->root;
  1430. int ret = btrfs_csum_verify_file_block(root,
  1431. page->mapping->host->i_ino,
  1432. page->index << PAGE_CACHE_SHIFT,
  1433. kmap(page), PAGE_CACHE_SIZE);
  1434. if (ret) {
  1435. printk("failed to verify ino %lu page %lu\n",
  1436. page->mapping->host->i_ino,
  1437. page->index);
  1438. memset(page_address(page), 0, PAGE_CACHE_SIZE);
  1439. }
  1440. SetPageChecked(page);
  1441. kunmap(page);
  1442. }
  1443. /*
  1444. * Faults on the destination of a read are common, so do it before
  1445. * taking the kmap.
  1446. */
  1447. if (!fault_in_pages_writeable(desc->arg.buf, size)) {
  1448. kaddr = kmap_atomic(page, KM_USER0);
  1449. left = __copy_to_user_inatomic(desc->arg.buf,
  1450. kaddr + offset, size);
  1451. kunmap_atomic(kaddr, KM_USER0);
  1452. if (left == 0)
  1453. goto success;
  1454. }
  1455. /* Do it the slow way */
  1456. kaddr = kmap(page);
  1457. left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
  1458. kunmap(page);
  1459. if (left) {
  1460. size -= left;
  1461. desc->error = -EFAULT;
  1462. }
  1463. success:
  1464. desc->count = count - size;
  1465. desc->written += size;
  1466. desc->arg.buf += size;
  1467. return size;
  1468. }
  1469. /**
  1470. * btrfs_file_aio_read - filesystem read routine
  1471. * @iocb: kernel I/O control block
  1472. * @iov: io vector request
  1473. * @nr_segs: number of segments in the iovec
  1474. * @pos: current file position
  1475. */
  1476. static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
  1477. unsigned long nr_segs, loff_t pos)
  1478. {
  1479. struct file *filp = iocb->ki_filp;
  1480. ssize_t retval;
  1481. unsigned long seg;
  1482. size_t count;
  1483. loff_t *ppos = &iocb->ki_pos;
  1484. count = 0;
  1485. for (seg = 0; seg < nr_segs; seg++) {
  1486. const struct iovec *iv = &iov[seg];
  1487. /*
  1488. * If any segment has a negative length, or the cumulative
  1489. * length ever wraps negative then return -EINVAL.
  1490. */
  1491. count += iv->iov_len;
  1492. if (unlikely((ssize_t)(count|iv->iov_len) < 0))
  1493. return -EINVAL;
  1494. if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
  1495. continue;
  1496. if (seg == 0)
  1497. return -EFAULT;
  1498. nr_segs = seg;
  1499. count -= iv->iov_len; /* This segment is no good */
  1500. break;
  1501. }
  1502. retval = 0;
  1503. if (count) {
  1504. for (seg = 0; seg < nr_segs; seg++) {
  1505. read_descriptor_t desc;
  1506. desc.written = 0;
  1507. desc.arg.buf = iov[seg].iov_base;
  1508. desc.count = iov[seg].iov_len;
  1509. if (desc.count == 0)
  1510. continue;
  1511. desc.error = 0;
  1512. do_generic_file_read(filp, ppos, &desc,
  1513. btrfs_read_actor);
  1514. retval += desc.written;
  1515. if (desc.error) {
  1516. retval = retval ?: desc.error;
  1517. break;
  1518. }
  1519. }
  1520. }
  1521. return retval;
  1522. }
  1523. static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
  1524. {
  1525. struct btrfs_trans_handle *trans;
  1526. struct btrfs_key key;
  1527. struct btrfs_root_item new_root_item;
  1528. int ret;
  1529. u64 objectid;
  1530. mutex_lock(&root->fs_info->fs_mutex);
  1531. trans = btrfs_start_transaction(root, 1);
  1532. BUG_ON(!trans);
  1533. ret = btrfs_update_inode(trans, root, root->inode);
  1534. BUG_ON(ret);
  1535. ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
  1536. 0, &objectid);
  1537. BUG_ON(ret);
  1538. memset(&new_root_item, 0, sizeof(new_root_item));
  1539. memcpy(&new_root_item, &root->root_item,
  1540. sizeof(new_root_item));
  1541. key.objectid = objectid;
  1542. key.offset = 1;
  1543. key.flags = 0;
  1544. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  1545. btrfs_set_root_blocknr(&new_root_item, root->node->b_blocknr);
  1546. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  1547. &new_root_item);
  1548. BUG_ON(ret);
  1549. printk("adding snapshot name %.*s root %Lu %Lu %u\n", namelen, name, key.objectid, key.offset, key.flags);
  1550. /*
  1551. * insert the directory item
  1552. */
  1553. key.offset = (u64)-1;
  1554. ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
  1555. name, namelen,
  1556. root->fs_info->sb->s_root->d_inode->i_ino,
  1557. &key, 0);
  1558. BUG_ON(ret);
  1559. ret = btrfs_inc_root_ref(trans, root);
  1560. BUG_ON(ret);
  1561. ret = btrfs_commit_transaction(trans, root);
  1562. BUG_ON(ret);
  1563. mutex_unlock(&root->fs_info->fs_mutex);
  1564. return 0;
  1565. }
  1566. static int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
  1567. cmd, unsigned long arg)
  1568. {
  1569. struct btrfs_root *root = BTRFS_I(inode)->root;
  1570. struct btrfs_ioctl_vol_args vol_args;
  1571. int ret;
  1572. int namelen;
  1573. if (!root->ref_cows)
  1574. return -EINVAL;
  1575. switch (cmd) {
  1576. case BTRFS_IOC_SNAP_CREATE:
  1577. if (copy_from_user(&vol_args,
  1578. (struct btrfs_ioctl_vol_args __user *)arg,
  1579. sizeof(vol_args)))
  1580. return -EFAULT;
  1581. namelen = strlen(vol_args.name);
  1582. if (namelen > BTRFS_VOL_NAME_MAX)
  1583. return -EINVAL;
  1584. ret = create_snapshot(root, vol_args.name, namelen);
  1585. WARN_ON(ret);
  1586. break;
  1587. default:
  1588. return -ENOTTY;
  1589. }
  1590. return 0;
  1591. }
  1592. static struct kmem_cache *btrfs_inode_cachep;
  1593. struct kmem_cache *btrfs_trans_handle_cachep;
  1594. struct kmem_cache *btrfs_transaction_cachep;
  1595. struct kmem_cache *btrfs_bit_radix_cachep;
  1596. struct kmem_cache *btrfs_path_cachep;
  1597. /*
  1598. * Called inside transaction, so use GFP_NOFS
  1599. */
  1600. static struct inode *btrfs_alloc_inode(struct super_block *sb)
  1601. {
  1602. struct btrfs_inode *ei;
  1603. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  1604. if (!ei)
  1605. return NULL;
  1606. return &ei->vfs_inode;
  1607. }
  1608. static void btrfs_destroy_inode(struct inode *inode)
  1609. {
  1610. WARN_ON(!list_empty(&inode->i_dentry));
  1611. WARN_ON(inode->i_data.nrpages);
  1612. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  1613. }
  1614. static void init_once(void * foo, struct kmem_cache * cachep,
  1615. unsigned long flags)
  1616. {
  1617. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  1618. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  1619. SLAB_CTOR_CONSTRUCTOR) {
  1620. inode_init_once(&ei->vfs_inode);
  1621. }
  1622. }
  1623. static int init_inodecache(void)
  1624. {
  1625. btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
  1626. sizeof(struct btrfs_inode),
  1627. 0, (SLAB_RECLAIM_ACCOUNT|
  1628. SLAB_MEM_SPREAD),
  1629. init_once, NULL);
  1630. btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
  1631. sizeof(struct btrfs_trans_handle),
  1632. 0, (SLAB_RECLAIM_ACCOUNT|
  1633. SLAB_MEM_SPREAD),
  1634. NULL, NULL);
  1635. btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
  1636. sizeof(struct btrfs_transaction),
  1637. 0, (SLAB_RECLAIM_ACCOUNT|
  1638. SLAB_MEM_SPREAD),
  1639. NULL, NULL);
  1640. btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
  1641. sizeof(struct btrfs_transaction),
  1642. 0, (SLAB_RECLAIM_ACCOUNT|
  1643. SLAB_MEM_SPREAD),
  1644. NULL, NULL);
  1645. btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
  1646. 256,
  1647. 0, (SLAB_RECLAIM_ACCOUNT|
  1648. SLAB_MEM_SPREAD |
  1649. SLAB_DESTROY_BY_RCU),
  1650. NULL, NULL);
  1651. if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
  1652. btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
  1653. return -ENOMEM;
  1654. return 0;
  1655. }
  1656. static void destroy_inodecache(void)
  1657. {
  1658. kmem_cache_destroy(btrfs_inode_cachep);
  1659. kmem_cache_destroy(btrfs_trans_handle_cachep);
  1660. kmem_cache_destroy(btrfs_transaction_cachep);
  1661. kmem_cache_destroy(btrfs_bit_radix_cachep);
  1662. kmem_cache_destroy(btrfs_path_cachep);
  1663. }
  1664. static int btrfs_get_sb(struct file_system_type *fs_type,
  1665. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  1666. {
  1667. return get_sb_bdev(fs_type, flags, dev_name, data,
  1668. btrfs_fill_super, mnt);
  1669. }
  1670. static struct file_system_type btrfs_fs_type = {
  1671. .owner = THIS_MODULE,
  1672. .name = "btrfs",
  1673. .get_sb = btrfs_get_sb,
  1674. .kill_sb = kill_block_super,
  1675. .fs_flags = FS_REQUIRES_DEV,
  1676. };
  1677. static struct super_operations btrfs_super_ops = {
  1678. .statfs = simple_statfs,
  1679. .delete_inode = btrfs_delete_inode,
  1680. .put_super = btrfs_put_super,
  1681. .read_inode = btrfs_read_locked_inode,
  1682. .write_super = btrfs_write_super,
  1683. .sync_fs = btrfs_sync_fs,
  1684. .write_inode = btrfs_write_inode,
  1685. .alloc_inode = btrfs_alloc_inode,
  1686. .destroy_inode = btrfs_destroy_inode,
  1687. };
  1688. static struct inode_operations btrfs_dir_inode_operations = {
  1689. .lookup = btrfs_lookup,
  1690. .create = btrfs_create,
  1691. .unlink = btrfs_unlink,
  1692. .mkdir = btrfs_mkdir,
  1693. .rmdir = btrfs_rmdir,
  1694. };
  1695. static struct inode_operations btrfs_dir_ro_inode_operations = {
  1696. .lookup = btrfs_lookup,
  1697. };
  1698. static struct file_operations btrfs_dir_file_operations = {
  1699. .llseek = generic_file_llseek,
  1700. .read = generic_read_dir,
  1701. .readdir = btrfs_readdir,
  1702. .ioctl = btrfs_ioctl,
  1703. };
  1704. static struct address_space_operations btrfs_aops = {
  1705. .readpage = btrfs_readpage,
  1706. .readpages = btrfs_readpages,
  1707. .writepage = btrfs_writepage,
  1708. .sync_page = block_sync_page,
  1709. .prepare_write = btrfs_prepare_write,
  1710. .commit_write = btrfs_commit_write,
  1711. };
  1712. static struct inode_operations btrfs_file_inode_operations = {
  1713. .truncate = btrfs_truncate,
  1714. };
  1715. static struct file_operations btrfs_file_operations = {
  1716. .llseek = generic_file_llseek,
  1717. .read = do_sync_read,
  1718. .aio_read = btrfs_file_aio_read,
  1719. .write = btrfs_file_write,
  1720. .mmap = generic_file_mmap,
  1721. .open = generic_file_open,
  1722. .ioctl = btrfs_ioctl,
  1723. };
  1724. static int __init init_btrfs_fs(void)
  1725. {
  1726. int err;
  1727. printk("btrfs loaded!\n");
  1728. err = init_inodecache();
  1729. if (err)
  1730. return err;
  1731. kset_set_kset_s(&btrfs_subsys, fs_subsys);
  1732. err = subsystem_register(&btrfs_subsys);
  1733. if (err)
  1734. goto out;
  1735. return register_filesystem(&btrfs_fs_type);
  1736. out:
  1737. destroy_inodecache();
  1738. return err;
  1739. }
  1740. static void __exit exit_btrfs_fs(void)
  1741. {
  1742. destroy_inodecache();
  1743. unregister_filesystem(&btrfs_fs_type);
  1744. subsystem_unregister(&btrfs_subsys);
  1745. printk("btrfs unloaded\n");
  1746. }
  1747. module_init(init_btrfs_fs)
  1748. module_exit(exit_btrfs_fs)
  1749. MODULE_LICENSE("GPL");