super.c 49 KB

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