super.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056
  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. printk("adding new root for inode %lu root %p (found %p)\n", inode->i_ino, sub_root, BTRFS_I(inode)->root);
  454. igrab(inode);
  455. sub_root->inode = inode;
  456. }
  457. BTRFS_I(inode)->root = sub_root;
  458. memcpy(&BTRFS_I(inode)->location, &location,
  459. sizeof(location));
  460. btrfs_read_locked_inode(inode);
  461. unlock_new_inode(inode);
  462. }
  463. }
  464. return d_splice_alias(inode, dentry);
  465. }
  466. static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
  467. {
  468. struct inode *inode = filp->f_path.dentry->d_inode;
  469. struct btrfs_root *root = BTRFS_I(inode)->root;
  470. struct btrfs_item *item;
  471. struct btrfs_dir_item *di;
  472. struct btrfs_key key;
  473. struct btrfs_path *path;
  474. int ret;
  475. u32 nritems;
  476. struct btrfs_leaf *leaf;
  477. int slot;
  478. int advance;
  479. unsigned char d_type = DT_UNKNOWN;
  480. int over = 0;
  481. int key_type = BTRFS_DIR_INDEX_KEY;
  482. /* FIXME, use a real flag for deciding about the key type */
  483. if (root->fs_info->tree_root == root)
  484. key_type = BTRFS_DIR_ITEM_KEY;
  485. mutex_lock(&root->fs_info->fs_mutex);
  486. key.objectid = inode->i_ino;
  487. key.flags = 0;
  488. btrfs_set_key_type(&key, key_type);
  489. key.offset = filp->f_pos;
  490. path = btrfs_alloc_path();
  491. btrfs_init_path(path);
  492. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  493. if (ret < 0)
  494. goto err;
  495. advance = 0;
  496. while(1) {
  497. leaf = btrfs_buffer_leaf(path->nodes[0]);
  498. nritems = btrfs_header_nritems(&leaf->header);
  499. slot = path->slots[0];
  500. if (advance || slot >= nritems) {
  501. if (slot >= nritems -1) {
  502. ret = btrfs_next_leaf(root, path);
  503. if (ret)
  504. break;
  505. leaf = btrfs_buffer_leaf(path->nodes[0]);
  506. nritems = btrfs_header_nritems(&leaf->header);
  507. slot = path->slots[0];
  508. } else {
  509. slot++;
  510. path->slots[0]++;
  511. }
  512. }
  513. advance = 1;
  514. item = leaf->items + slot;
  515. if (btrfs_disk_key_objectid(&item->key) != key.objectid)
  516. break;
  517. if (key_type == BTRFS_DIR_INDEX_KEY &&
  518. btrfs_disk_key_offset(&item->key) > root->highest_inode)
  519. break;
  520. if (btrfs_disk_key_type(&item->key) != key_type)
  521. continue;
  522. if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
  523. continue;
  524. filp->f_pos = btrfs_disk_key_offset(&item->key);
  525. advance = 1;
  526. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  527. over = filldir(dirent, (const char *)(di + 1),
  528. btrfs_dir_name_len(di),
  529. btrfs_disk_key_offset(&item->key),
  530. btrfs_disk_key_objectid(&di->location), d_type);
  531. if (over)
  532. goto nopos;
  533. }
  534. filp->f_pos++;
  535. nopos:
  536. ret = 0;
  537. err:
  538. btrfs_release_path(root, path);
  539. btrfs_free_path(path);
  540. mutex_unlock(&root->fs_info->fs_mutex);
  541. return ret;
  542. }
  543. static void btrfs_put_super (struct super_block * sb)
  544. {
  545. struct btrfs_root *root = btrfs_sb(sb);
  546. int ret;
  547. ret = close_ctree(root);
  548. if (ret) {
  549. printk("close ctree returns %d\n", ret);
  550. }
  551. sb->s_fs_info = NULL;
  552. }
  553. static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
  554. {
  555. struct inode * inode;
  556. struct dentry * root_dentry;
  557. struct btrfs_super_block *disk_super;
  558. struct btrfs_root *tree_root;
  559. struct btrfs_inode *bi;
  560. sb->s_maxbytes = MAX_LFS_FILESIZE;
  561. sb->s_magic = BTRFS_SUPER_MAGIC;
  562. sb->s_op = &btrfs_super_ops;
  563. sb->s_time_gran = 1;
  564. tree_root = open_ctree(sb);
  565. if (!tree_root) {
  566. printk("btrfs: open_ctree failed\n");
  567. return -EIO;
  568. }
  569. sb->s_fs_info = tree_root;
  570. disk_super = tree_root->fs_info->disk_super;
  571. printk("read in super total blocks %Lu root %Lu\n",
  572. btrfs_super_total_blocks(disk_super),
  573. btrfs_super_root_dir(disk_super));
  574. inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
  575. tree_root);
  576. bi = BTRFS_I(inode);
  577. bi->location.objectid = inode->i_ino;
  578. bi->location.offset = 0;
  579. bi->location.flags = 0;
  580. bi->root = tree_root;
  581. btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
  582. if (!inode)
  583. return -ENOMEM;
  584. if (inode->i_state & I_NEW) {
  585. btrfs_read_locked_inode(inode);
  586. unlock_new_inode(inode);
  587. }
  588. root_dentry = d_alloc_root(inode);
  589. if (!root_dentry) {
  590. iput(inode);
  591. return -ENOMEM;
  592. }
  593. sb->s_root = root_dentry;
  594. return 0;
  595. }
  596. static void fill_inode_item(struct btrfs_inode_item *item,
  597. struct inode *inode)
  598. {
  599. btrfs_set_inode_uid(item, inode->i_uid);
  600. btrfs_set_inode_gid(item, inode->i_gid);
  601. btrfs_set_inode_size(item, inode->i_size);
  602. btrfs_set_inode_mode(item, inode->i_mode);
  603. btrfs_set_inode_nlink(item, inode->i_nlink);
  604. btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
  605. btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
  606. btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
  607. btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
  608. btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
  609. btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
  610. btrfs_set_inode_nblocks(item, inode->i_blocks);
  611. btrfs_set_inode_generation(item, inode->i_generation);
  612. }
  613. static int btrfs_update_inode(struct btrfs_trans_handle *trans,
  614. struct btrfs_root *root,
  615. struct inode *inode)
  616. {
  617. struct btrfs_inode_item *inode_item;
  618. struct btrfs_path *path;
  619. int ret;
  620. path = btrfs_alloc_path();
  621. BUG_ON(!path);
  622. btrfs_init_path(path);
  623. ret = btrfs_lookup_inode(trans, root, path,
  624. &BTRFS_I(inode)->location, 1);
  625. if (ret) {
  626. if (ret > 0)
  627. ret = -ENOENT;
  628. goto failed;
  629. }
  630. inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  631. path->slots[0],
  632. struct btrfs_inode_item);
  633. fill_inode_item(inode_item, inode);
  634. btrfs_mark_buffer_dirty(path->nodes[0]);
  635. ret = 0;
  636. failed:
  637. btrfs_release_path(root, path);
  638. btrfs_free_path(path);
  639. return ret;
  640. }
  641. static int btrfs_write_inode(struct inode *inode, int wait)
  642. {
  643. struct btrfs_root *root = BTRFS_I(inode)->root;
  644. struct btrfs_trans_handle *trans;
  645. int ret;
  646. mutex_lock(&root->fs_info->fs_mutex);
  647. trans = btrfs_start_transaction(root, 1);
  648. ret = btrfs_update_inode(trans, root, inode);
  649. if (wait)
  650. btrfs_commit_transaction(trans, root);
  651. else
  652. btrfs_end_transaction(trans, root);
  653. mutex_unlock(&root->fs_info->fs_mutex);
  654. return ret;
  655. }
  656. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  657. struct btrfs_root *root,
  658. u64 objectid, int mode)
  659. {
  660. struct inode *inode;
  661. struct btrfs_inode_item inode_item;
  662. struct btrfs_key *location;
  663. int ret;
  664. inode = new_inode(root->fs_info->sb);
  665. if (!inode)
  666. return ERR_PTR(-ENOMEM);
  667. BTRFS_I(inode)->root = root;
  668. inode->i_uid = current->fsuid;
  669. inode->i_gid = current->fsgid;
  670. inode->i_mode = mode;
  671. inode->i_ino = objectid;
  672. inode->i_blocks = 0;
  673. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  674. fill_inode_item(&inode_item, inode);
  675. location = &BTRFS_I(inode)->location;
  676. location->objectid = objectid;
  677. location->flags = 0;
  678. location->offset = 0;
  679. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  680. ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
  681. BUG_ON(ret);
  682. insert_inode_hash(inode);
  683. return inode;
  684. }
  685. static int btrfs_add_link(struct btrfs_trans_handle *trans,
  686. struct dentry *dentry, struct inode *inode)
  687. {
  688. int ret;
  689. struct btrfs_key key;
  690. struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
  691. key.objectid = inode->i_ino;
  692. key.flags = 0;
  693. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  694. key.offset = 0;
  695. ret = btrfs_insert_dir_item(trans, root,
  696. dentry->d_name.name, dentry->d_name.len,
  697. dentry->d_parent->d_inode->i_ino,
  698. &key, 0);
  699. if (ret == 0) {
  700. dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
  701. ret = btrfs_update_inode(trans, root,
  702. dentry->d_parent->d_inode);
  703. }
  704. return ret;
  705. }
  706. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  707. struct dentry *dentry, struct inode *inode)
  708. {
  709. int err = btrfs_add_link(trans, dentry, inode);
  710. if (!err) {
  711. d_instantiate(dentry, inode);
  712. return 0;
  713. }
  714. if (err > 0)
  715. err = -EEXIST;
  716. return err;
  717. }
  718. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  719. int mode, struct nameidata *nd)
  720. {
  721. struct btrfs_trans_handle *trans;
  722. struct btrfs_root *root = BTRFS_I(dir)->root;
  723. struct inode *inode;
  724. int err;
  725. int drop_inode = 0;
  726. u64 objectid;
  727. mutex_lock(&root->fs_info->fs_mutex);
  728. trans = btrfs_start_transaction(root, 1);
  729. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  730. if (err) {
  731. err = -ENOSPC;
  732. goto out_unlock;
  733. }
  734. inode = btrfs_new_inode(trans, root, objectid, mode);
  735. err = PTR_ERR(inode);
  736. if (IS_ERR(inode))
  737. goto out_unlock;
  738. // FIXME mark the inode dirty
  739. err = btrfs_add_nondir(trans, dentry, inode);
  740. if (err)
  741. drop_inode = 1;
  742. else {
  743. inode->i_mapping->a_ops = &btrfs_aops;
  744. inode->i_fop = &btrfs_file_operations;
  745. inode->i_op = &btrfs_file_inode_operations;
  746. }
  747. dir->i_sb->s_dirt = 1;
  748. out_unlock:
  749. btrfs_end_transaction(trans, root);
  750. mutex_unlock(&root->fs_info->fs_mutex);
  751. if (drop_inode) {
  752. inode_dec_link_count(inode);
  753. iput(inode);
  754. }
  755. return err;
  756. }
  757. static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
  758. struct btrfs_root *root,
  759. u64 objectid, u64 dirid)
  760. {
  761. int ret;
  762. char buf[2];
  763. struct btrfs_key key;
  764. buf[0] = '.';
  765. buf[1] = '.';
  766. key.objectid = objectid;
  767. key.offset = 0;
  768. key.flags = 0;
  769. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  770. ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
  771. &key, 1);
  772. if (ret)
  773. goto error;
  774. key.objectid = dirid;
  775. ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
  776. &key, 1);
  777. if (ret)
  778. goto error;
  779. error:
  780. return ret;
  781. }
  782. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  783. {
  784. struct inode *inode;
  785. struct btrfs_trans_handle *trans;
  786. struct btrfs_root *root = BTRFS_I(dir)->root;
  787. int err = 0;
  788. int drop_on_err = 0;
  789. u64 objectid;
  790. mutex_lock(&root->fs_info->fs_mutex);
  791. trans = btrfs_start_transaction(root, 1);
  792. if (IS_ERR(trans)) {
  793. err = PTR_ERR(trans);
  794. goto out_unlock;
  795. }
  796. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  797. if (err) {
  798. err = -ENOSPC;
  799. goto out_unlock;
  800. }
  801. inode = btrfs_new_inode(trans, root, objectid, S_IFDIR | mode);
  802. if (IS_ERR(inode)) {
  803. err = PTR_ERR(inode);
  804. goto out_fail;
  805. }
  806. drop_on_err = 1;
  807. inode->i_op = &btrfs_dir_inode_operations;
  808. inode->i_fop = &btrfs_dir_file_operations;
  809. err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
  810. if (err)
  811. goto out_fail;
  812. inode->i_size = 6;
  813. err = btrfs_update_inode(trans, root, inode);
  814. if (err)
  815. goto out_fail;
  816. err = btrfs_add_link(trans, dentry, inode);
  817. if (err)
  818. goto out_fail;
  819. d_instantiate(dentry, inode);
  820. drop_on_err = 0;
  821. out_fail:
  822. btrfs_end_transaction(trans, root);
  823. out_unlock:
  824. mutex_unlock(&root->fs_info->fs_mutex);
  825. if (drop_on_err)
  826. iput(inode);
  827. return err;
  828. }
  829. static int btrfs_sync_fs(struct super_block *sb, int wait)
  830. {
  831. struct btrfs_trans_handle *trans;
  832. struct btrfs_root *root;
  833. int ret;
  834. root = btrfs_sb(sb);
  835. sb->s_dirt = 0;
  836. if (!wait) {
  837. filemap_flush(root->fs_info->btree_inode->i_mapping);
  838. return 0;
  839. }
  840. filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
  841. mutex_lock(&root->fs_info->fs_mutex);
  842. trans = btrfs_start_transaction(root, 1);
  843. ret = btrfs_commit_transaction(trans, root);
  844. sb->s_dirt = 0;
  845. BUG_ON(ret);
  846. printk("btrfs sync_fs\n");
  847. mutex_unlock(&root->fs_info->fs_mutex);
  848. return 0;
  849. }
  850. #if 0
  851. static int btrfs_get_block_inline(struct inode *inode, sector_t iblock,
  852. struct buffer_head *result, int create)
  853. {
  854. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  855. struct btrfs_path *path;
  856. struct btrfs_key key;
  857. struct btrfs_leaf *leaf;
  858. int num_bytes = result->b_size;
  859. int item_size;
  860. int ret;
  861. u64 pos;
  862. char *ptr;
  863. int copy_size;
  864. int err = 0;
  865. char *safe_ptr;
  866. char *data_ptr;
  867. path = btrfs_alloc_path();
  868. BUG_ON(!path);
  869. WARN_ON(create);
  870. if (create) {
  871. return 0;
  872. }
  873. pos = iblock << inode->i_blkbits;
  874. key.objectid = inode->i_ino;
  875. key.flags = 0;
  876. btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
  877. ptr = kmap(result->b_page);
  878. safe_ptr = ptr;
  879. ptr += (pos & (PAGE_CACHE_SIZE -1));
  880. again:
  881. key.offset = pos;
  882. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  883. if (ret) {
  884. if (ret < 0)
  885. err = ret;
  886. else
  887. err = 0;
  888. goto out;
  889. }
  890. leaf = btrfs_buffer_leaf(path->nodes[0]);
  891. item_size = btrfs_item_size(leaf->items + path->slots[0]);
  892. copy_size = min(num_bytes, item_size);
  893. data_ptr = btrfs_item_ptr(leaf, path->slots[0], char);
  894. WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size);
  895. memcpy(ptr, data_ptr, copy_size);
  896. pos += copy_size;
  897. num_bytes -= copy_size;
  898. WARN_ON(num_bytes < 0);
  899. ptr += copy_size;
  900. btrfs_release_path(root, path);
  901. if (num_bytes != 0) {
  902. if (pos >= i_size_read(inode))
  903. memset(ptr, 0, num_bytes);
  904. else
  905. goto again;
  906. }
  907. set_buffer_uptodate(result);
  908. map_bh(result, inode->i_sb, 0);
  909. err = 0;
  910. out:
  911. btrfs_free_path(path);
  912. kunmap(result->b_page);
  913. return err;
  914. }
  915. #endif
  916. static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
  917. struct buffer_head *result, int create)
  918. {
  919. int ret;
  920. int err = 0;
  921. u64 blocknr;
  922. u64 extent_start = 0;
  923. u64 extent_end = 0;
  924. u64 objectid = inode->i_ino;
  925. struct btrfs_path *path;
  926. struct btrfs_root *root = BTRFS_I(inode)->root;
  927. struct btrfs_trans_handle *trans = NULL;
  928. struct btrfs_file_extent_item *item;
  929. struct btrfs_leaf *leaf;
  930. struct btrfs_disk_key *found_key;
  931. path = btrfs_alloc_path();
  932. BUG_ON(!path);
  933. btrfs_init_path(path);
  934. if (create)
  935. trans = btrfs_start_transaction(root, 1);
  936. ret = btrfs_lookup_file_extent(trans, root, path,
  937. inode->i_ino,
  938. iblock << inode->i_blkbits, create);
  939. if (ret < 0) {
  940. err = ret;
  941. goto out;
  942. }
  943. if (ret != 0) {
  944. if (path->slots[0] == 0) {
  945. btrfs_release_path(root, path);
  946. goto allocate;
  947. }
  948. path->slots[0]--;
  949. }
  950. item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  951. struct btrfs_file_extent_item);
  952. leaf = btrfs_buffer_leaf(path->nodes[0]);
  953. blocknr = btrfs_file_extent_disk_blocknr(item);
  954. blocknr += btrfs_file_extent_offset(item);
  955. /* exact match found, use it, FIXME, deal with extents
  956. * other than the page size
  957. */
  958. if (ret == 0) {
  959. err = 0;
  960. BUG_ON(btrfs_file_extent_disk_num_blocks(item) != 1);
  961. if (create &&
  962. btrfs_file_extent_generation(item) != trans->transid) {
  963. struct btrfs_key ins;
  964. ret = btrfs_alloc_extent(trans, root, 1,
  965. blocknr, (u64)-1, &ins);
  966. BUG_ON(ret);
  967. btrfs_set_file_extent_disk_blocknr(item, ins.objectid);
  968. mark_buffer_dirty(path->nodes[0]);
  969. ret = btrfs_free_extent(trans, root,
  970. blocknr, 1, 0);
  971. BUG_ON(ret);
  972. blocknr = ins.objectid;
  973. }
  974. map_bh(result, inode->i_sb, blocknr);
  975. goto out;
  976. }
  977. /* are we inside the extent that was found? */
  978. found_key = &leaf->items[path->slots[0]].key;
  979. if (btrfs_disk_key_objectid(found_key) != objectid ||
  980. btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
  981. extent_end = 0;
  982. extent_start = 0;
  983. btrfs_release_path(root, path);
  984. goto allocate;
  985. }
  986. extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
  987. extent_start = extent_start >> inode->i_blkbits;
  988. extent_start += btrfs_file_extent_offset(item);
  989. extent_end = extent_start + btrfs_file_extent_num_blocks(item);
  990. if (iblock >= extent_start && iblock < extent_end) {
  991. err = 0;
  992. map_bh(result, inode->i_sb, blocknr + iblock - extent_start);
  993. goto out;
  994. }
  995. allocate:
  996. /* ok, create a new extent */
  997. if (!create) {
  998. err = 0;
  999. goto out;
  1000. }
  1001. ret = btrfs_alloc_file_extent(trans, root, objectid,
  1002. iblock << inode->i_blkbits,
  1003. 1, extent_end, &blocknr);
  1004. if (ret) {
  1005. err = ret;
  1006. goto out;
  1007. }
  1008. inode->i_blocks += inode->i_sb->s_blocksize >> 9;
  1009. set_buffer_new(result);
  1010. map_bh(result, inode->i_sb, blocknr);
  1011. out:
  1012. btrfs_release_path(root, path);
  1013. btrfs_free_path(path);
  1014. if (trans)
  1015. btrfs_end_transaction(trans, root);
  1016. return err;
  1017. }
  1018. static int btrfs_get_block(struct inode *inode, sector_t iblock,
  1019. struct buffer_head *result, int create)
  1020. {
  1021. int err;
  1022. struct btrfs_root *root = BTRFS_I(inode)->root;
  1023. mutex_lock(&root->fs_info->fs_mutex);
  1024. err = btrfs_get_block_lock(inode, iblock, result, create);
  1025. // err = btrfs_get_block_inline(inode, iblock, result, create);
  1026. mutex_unlock(&root->fs_info->fs_mutex);
  1027. return err;
  1028. }
  1029. static int btrfs_prepare_write(struct file *file, struct page *page,
  1030. unsigned from, unsigned to)
  1031. {
  1032. return nobh_prepare_write(page, from, to, btrfs_get_block);
  1033. }
  1034. static int btrfs_commit_write(struct file *file, struct page *page,
  1035. unsigned from, unsigned to)
  1036. {
  1037. return nobh_commit_write(file, page, from, to);
  1038. }
  1039. static void btrfs_write_super(struct super_block *sb)
  1040. {
  1041. btrfs_sync_fs(sb, 1);
  1042. }
  1043. static int btrfs_readpage(struct file *file, struct page *page)
  1044. {
  1045. return mpage_readpage(page, btrfs_get_block);
  1046. }
  1047. static int btrfs_readpages(struct file *file, struct address_space *mapping,
  1048. struct list_head *pages, unsigned nr_pages)
  1049. {
  1050. return mpage_readpages(mapping, pages, nr_pages, btrfs_get_block);
  1051. }
  1052. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  1053. {
  1054. return nobh_writepage(page, btrfs_get_block, wbc);
  1055. }
  1056. static void btrfs_truncate(struct inode *inode)
  1057. {
  1058. struct btrfs_root *root = BTRFS_I(inode)->root;
  1059. int ret;
  1060. struct btrfs_trans_handle *trans;
  1061. if (!S_ISREG(inode->i_mode))
  1062. return;
  1063. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1064. return;
  1065. nobh_truncate_page(inode->i_mapping, inode->i_size);
  1066. /* FIXME, add redo link to tree so we don't leak on crash */
  1067. mutex_lock(&root->fs_info->fs_mutex);
  1068. trans = btrfs_start_transaction(root, 1);
  1069. ret = btrfs_truncate_in_trans(trans, root, inode);
  1070. BUG_ON(ret);
  1071. ret = btrfs_end_transaction(trans, root);
  1072. BUG_ON(ret);
  1073. mutex_unlock(&root->fs_info->fs_mutex);
  1074. mark_inode_dirty(inode);
  1075. }
  1076. static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
  1077. struct page **prepared_pages,
  1078. const char __user * buf)
  1079. {
  1080. long page_fault = 0;
  1081. int i;
  1082. int offset = pos & (PAGE_CACHE_SIZE - 1);
  1083. for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
  1084. size_t count = min_t(size_t,
  1085. PAGE_CACHE_SIZE - offset, write_bytes);
  1086. struct page *page = prepared_pages[i];
  1087. fault_in_pages_readable(buf, count);
  1088. /* Copy data from userspace to the current page */
  1089. kmap(page);
  1090. page_fault = __copy_from_user(page_address(page) + offset,
  1091. buf, count);
  1092. /* Flush processor's dcache for this page */
  1093. flush_dcache_page(page);
  1094. kunmap(page);
  1095. buf += count;
  1096. write_bytes -= count;
  1097. if (page_fault)
  1098. break;
  1099. }
  1100. return page_fault ? -EFAULT : 0;
  1101. }
  1102. static void btrfs_drop_pages(struct page **pages, size_t num_pages)
  1103. {
  1104. size_t i;
  1105. for (i = 0; i < num_pages; i++) {
  1106. if (!pages[i])
  1107. break;
  1108. unlock_page(pages[i]);
  1109. mark_page_accessed(pages[i]);
  1110. page_cache_release(pages[i]);
  1111. }
  1112. }
  1113. static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
  1114. struct btrfs_root *root,
  1115. struct file *file,
  1116. struct page **pages,
  1117. size_t num_pages,
  1118. loff_t pos,
  1119. size_t write_bytes)
  1120. {
  1121. int i;
  1122. int offset;
  1123. int err = 0;
  1124. int ret;
  1125. int this_write;
  1126. struct inode *inode = file->f_path.dentry->d_inode;
  1127. for (i = 0; i < num_pages; i++) {
  1128. offset = pos & (PAGE_CACHE_SIZE -1);
  1129. this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
  1130. /* FIXME, one block at a time */
  1131. mutex_lock(&root->fs_info->fs_mutex);
  1132. trans = btrfs_start_transaction(root, 1);
  1133. btrfs_csum_file_block(trans, root, inode->i_ino,
  1134. pages[i]->index << PAGE_CACHE_SHIFT,
  1135. kmap(pages[i]), PAGE_CACHE_SIZE);
  1136. kunmap(pages[i]);
  1137. SetPageChecked(pages[i]);
  1138. ret = btrfs_end_transaction(trans, root);
  1139. BUG_ON(ret);
  1140. mutex_unlock(&root->fs_info->fs_mutex);
  1141. ret = nobh_commit_write(file, pages[i], offset,
  1142. offset + this_write);
  1143. pos += this_write;
  1144. if (ret) {
  1145. err = ret;
  1146. goto failed;
  1147. }
  1148. WARN_ON(this_write > write_bytes);
  1149. write_bytes -= this_write;
  1150. }
  1151. failed:
  1152. return err;
  1153. }
  1154. static int prepare_pages(struct btrfs_trans_handle *trans,
  1155. struct btrfs_root *root,
  1156. struct file *file,
  1157. struct page **pages,
  1158. size_t num_pages,
  1159. loff_t pos,
  1160. unsigned long first_index,
  1161. unsigned long last_index,
  1162. size_t write_bytes)
  1163. {
  1164. int i;
  1165. unsigned long index = pos >> PAGE_CACHE_SHIFT;
  1166. struct inode *inode = file->f_path.dentry->d_inode;
  1167. int offset;
  1168. int err = 0;
  1169. int ret;
  1170. int this_write;
  1171. loff_t isize = i_size_read(inode);
  1172. memset(pages, 0, num_pages * sizeof(struct page *));
  1173. for (i = 0; i < num_pages; i++) {
  1174. pages[i] = grab_cache_page(inode->i_mapping, index + i);
  1175. if (!pages[i]) {
  1176. err = -ENOMEM;
  1177. goto failed_release;
  1178. }
  1179. offset = pos & (PAGE_CACHE_SIZE -1);
  1180. this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
  1181. if (!PageUptodate(pages[i]) &&
  1182. (pages[i]->index == first_index ||
  1183. pages[i]->index == last_index) && pos < isize) {
  1184. ret = mpage_readpage(pages[i], btrfs_get_block);
  1185. BUG_ON(ret);
  1186. lock_page(pages[i]);
  1187. }
  1188. ret = nobh_prepare_write(pages[i], offset,
  1189. offset + this_write,
  1190. btrfs_get_block);
  1191. pos += this_write;
  1192. if (ret) {
  1193. err = ret;
  1194. goto failed_truncate;
  1195. }
  1196. WARN_ON(this_write > write_bytes);
  1197. write_bytes -= this_write;
  1198. }
  1199. return 0;
  1200. failed_release:
  1201. btrfs_drop_pages(pages, num_pages);
  1202. return err;
  1203. failed_truncate:
  1204. btrfs_drop_pages(pages, num_pages);
  1205. if (pos > isize)
  1206. vmtruncate(inode, isize);
  1207. return err;
  1208. }
  1209. static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
  1210. size_t count, loff_t *ppos)
  1211. {
  1212. loff_t pos;
  1213. size_t num_written = 0;
  1214. int err = 0;
  1215. int ret = 0;
  1216. struct inode *inode = file->f_path.dentry->d_inode;
  1217. struct btrfs_root *root = BTRFS_I(inode)->root;
  1218. struct page *pages[1];
  1219. unsigned long first_index;
  1220. unsigned long last_index;
  1221. if (file->f_flags & O_DIRECT)
  1222. return -EINVAL;
  1223. pos = *ppos;
  1224. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1225. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  1226. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  1227. if (err)
  1228. goto out;
  1229. if (count == 0)
  1230. goto out;
  1231. err = remove_suid(file->f_path.dentry);
  1232. if (err)
  1233. goto out;
  1234. file_update_time(file);
  1235. mutex_lock(&inode->i_mutex);
  1236. first_index = pos >> PAGE_CACHE_SHIFT;
  1237. last_index = (pos + count) >> PAGE_CACHE_SHIFT;
  1238. while(count > 0) {
  1239. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  1240. size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
  1241. size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
  1242. PAGE_CACHE_SHIFT;
  1243. ret = prepare_pages(NULL, root, file, pages, num_pages,
  1244. pos, first_index, last_index, write_bytes);
  1245. BUG_ON(ret);
  1246. ret = btrfs_copy_from_user(pos, num_pages,
  1247. write_bytes, pages, buf);
  1248. BUG_ON(ret);
  1249. ret = dirty_and_release_pages(NULL, root, file, pages,
  1250. num_pages, pos, write_bytes);
  1251. BUG_ON(ret);
  1252. btrfs_drop_pages(pages, num_pages);
  1253. buf += write_bytes;
  1254. count -= write_bytes;
  1255. pos += write_bytes;
  1256. num_written += write_bytes;
  1257. balance_dirty_pages_ratelimited(inode->i_mapping);
  1258. cond_resched();
  1259. }
  1260. mutex_unlock(&inode->i_mutex);
  1261. out:
  1262. *ppos = pos;
  1263. current->backing_dev_info = NULL;
  1264. return num_written ? num_written : err;
  1265. }
  1266. #if 0
  1267. static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode,
  1268. struct page *page, loff_t pos,
  1269. size_t offset, size_t write_bytes)
  1270. {
  1271. struct btrfs_path *path;
  1272. struct btrfs_trans_handle *trans;
  1273. struct btrfs_key key;
  1274. struct btrfs_leaf *leaf;
  1275. struct btrfs_key found_key;
  1276. int ret;
  1277. size_t copy_size = 0;
  1278. char *dst = NULL;
  1279. int err = 0;
  1280. size_t num_written = 0;
  1281. path = btrfs_alloc_path();
  1282. BUG_ON(!path);
  1283. mutex_lock(&root->fs_info->fs_mutex);
  1284. trans = btrfs_start_transaction(root, 1);
  1285. key.objectid = inode->i_ino;
  1286. key.flags = 0;
  1287. btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
  1288. again:
  1289. key.offset = pos;
  1290. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1291. if (ret < 0) {
  1292. err = ret;
  1293. goto out;
  1294. }
  1295. if (ret == 0) {
  1296. leaf = btrfs_buffer_leaf(path->nodes[0]);
  1297. btrfs_disk_key_to_cpu(&found_key,
  1298. &leaf->items[path->slots[0]].key);
  1299. copy_size = btrfs_item_size(leaf->items + path->slots[0]);
  1300. dst = btrfs_item_ptr(leaf, path->slots[0], char);
  1301. copy_size = min(write_bytes, copy_size);
  1302. goto copyit;
  1303. } else {
  1304. int slot = path->slots[0];
  1305. if (slot > 0) {
  1306. slot--;
  1307. }
  1308. // FIXME find max key
  1309. leaf = btrfs_buffer_leaf(path->nodes[0]);
  1310. btrfs_disk_key_to_cpu(&found_key,
  1311. &leaf->items[slot].key);
  1312. if (found_key.objectid != inode->i_ino)
  1313. goto insert;
  1314. if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY)
  1315. goto insert;
  1316. copy_size = btrfs_item_size(leaf->items + slot);
  1317. if (found_key.offset + copy_size <= pos)
  1318. goto insert;
  1319. dst = btrfs_item_ptr(leaf, path->slots[0], char);
  1320. dst += pos - found_key.offset;
  1321. copy_size = copy_size - (pos - found_key.offset);
  1322. BUG_ON(copy_size < 0);
  1323. copy_size = min(write_bytes, copy_size);
  1324. WARN_ON(copy_size == 0);
  1325. goto copyit;
  1326. }
  1327. insert:
  1328. btrfs_release_path(root, path);
  1329. copy_size = min(write_bytes,
  1330. (size_t)BTRFS_LEAF_DATA_SIZE(root) -
  1331. sizeof(struct btrfs_item) * 4);
  1332. ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size);
  1333. BUG_ON(ret);
  1334. dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  1335. path->slots[0], char);
  1336. copyit:
  1337. WARN_ON(copy_size == 0);
  1338. WARN_ON(dst + copy_size >
  1339. btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  1340. path->slots[0], char) +
  1341. btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items +
  1342. path->slots[0]));
  1343. btrfs_memcpy(root, path->nodes[0]->b_data, dst,
  1344. page_address(page) + offset, copy_size);
  1345. mark_buffer_dirty(path->nodes[0]);
  1346. btrfs_release_path(root, path);
  1347. pos += copy_size;
  1348. offset += copy_size;
  1349. num_written += copy_size;
  1350. write_bytes -= copy_size;
  1351. if (write_bytes)
  1352. goto again;
  1353. out:
  1354. btrfs_free_path(path);
  1355. ret = btrfs_end_transaction(trans, root);
  1356. BUG_ON(ret);
  1357. mutex_unlock(&root->fs_info->fs_mutex);
  1358. return num_written ? num_written : err;
  1359. }
  1360. static ssize_t btrfs_file_inline_write(struct file *file,
  1361. const char __user *buf,
  1362. size_t count, loff_t *ppos)
  1363. {
  1364. loff_t pos;
  1365. size_t num_written = 0;
  1366. int err = 0;
  1367. int ret = 0;
  1368. struct inode *inode = file->f_path.dentry->d_inode;
  1369. struct btrfs_root *root = BTRFS_I(inode)->root;
  1370. unsigned long page_index;
  1371. if (file->f_flags & O_DIRECT)
  1372. return -EINVAL;
  1373. pos = *ppos;
  1374. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1375. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  1376. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  1377. if (err)
  1378. goto out;
  1379. if (count == 0)
  1380. goto out;
  1381. err = remove_suid(file->f_path.dentry);
  1382. if (err)
  1383. goto out;
  1384. file_update_time(file);
  1385. mutex_lock(&inode->i_mutex);
  1386. while(count > 0) {
  1387. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  1388. size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
  1389. struct page *page;
  1390. page_index = pos >> PAGE_CACHE_SHIFT;
  1391. page = grab_cache_page(inode->i_mapping, page_index);
  1392. if (!PageUptodate(page)) {
  1393. ret = mpage_readpage(page, btrfs_get_block);
  1394. BUG_ON(ret);
  1395. lock_page(page);
  1396. }
  1397. ret = btrfs_copy_from_user(pos, 1,
  1398. write_bytes, &page, buf);
  1399. BUG_ON(ret);
  1400. write_bytes = inline_one_page(root, inode, page, pos,
  1401. offset, write_bytes);
  1402. SetPageUptodate(page);
  1403. if (write_bytes > 0 && pos + write_bytes > inode->i_size) {
  1404. i_size_write(inode, pos + write_bytes);
  1405. mark_inode_dirty(inode);
  1406. }
  1407. page_cache_release(page);
  1408. unlock_page(page);
  1409. if (write_bytes < 0)
  1410. goto out_unlock;
  1411. buf += write_bytes;
  1412. count -= write_bytes;
  1413. pos += write_bytes;
  1414. num_written += write_bytes;
  1415. balance_dirty_pages_ratelimited(inode->i_mapping);
  1416. cond_resched();
  1417. }
  1418. out_unlock:
  1419. mutex_unlock(&inode->i_mutex);
  1420. out:
  1421. *ppos = pos;
  1422. current->backing_dev_info = NULL;
  1423. return num_written ? num_written : err;
  1424. }
  1425. #endif
  1426. static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
  1427. unsigned long offset, unsigned long size)
  1428. {
  1429. char *kaddr;
  1430. unsigned long left, count = desc->count;
  1431. struct inode *inode = page->mapping->host;
  1432. if (size > count)
  1433. size = count;
  1434. if (!PageChecked(page)) {
  1435. /* FIXME, do it per block */
  1436. struct btrfs_root *root = BTRFS_I(inode)->root;
  1437. int ret = btrfs_csum_verify_file_block(root,
  1438. page->mapping->host->i_ino,
  1439. page->index << PAGE_CACHE_SHIFT,
  1440. kmap(page), PAGE_CACHE_SIZE);
  1441. if (ret) {
  1442. printk("failed to verify ino %lu page %lu\n",
  1443. page->mapping->host->i_ino,
  1444. page->index);
  1445. memset(page_address(page), 0, PAGE_CACHE_SIZE);
  1446. }
  1447. SetPageChecked(page);
  1448. kunmap(page);
  1449. }
  1450. /*
  1451. * Faults on the destination of a read are common, so do it before
  1452. * taking the kmap.
  1453. */
  1454. if (!fault_in_pages_writeable(desc->arg.buf, size)) {
  1455. kaddr = kmap_atomic(page, KM_USER0);
  1456. left = __copy_to_user_inatomic(desc->arg.buf,
  1457. kaddr + offset, size);
  1458. kunmap_atomic(kaddr, KM_USER0);
  1459. if (left == 0)
  1460. goto success;
  1461. }
  1462. /* Do it the slow way */
  1463. kaddr = kmap(page);
  1464. left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
  1465. kunmap(page);
  1466. if (left) {
  1467. size -= left;
  1468. desc->error = -EFAULT;
  1469. }
  1470. success:
  1471. desc->count = count - size;
  1472. desc->written += size;
  1473. desc->arg.buf += size;
  1474. return size;
  1475. }
  1476. /**
  1477. * btrfs_file_aio_read - filesystem read routine
  1478. * @iocb: kernel I/O control block
  1479. * @iov: io vector request
  1480. * @nr_segs: number of segments in the iovec
  1481. * @pos: current file position
  1482. */
  1483. static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
  1484. unsigned long nr_segs, loff_t pos)
  1485. {
  1486. struct file *filp = iocb->ki_filp;
  1487. ssize_t retval;
  1488. unsigned long seg;
  1489. size_t count;
  1490. loff_t *ppos = &iocb->ki_pos;
  1491. count = 0;
  1492. for (seg = 0; seg < nr_segs; seg++) {
  1493. const struct iovec *iv = &iov[seg];
  1494. /*
  1495. * If any segment has a negative length, or the cumulative
  1496. * length ever wraps negative then return -EINVAL.
  1497. */
  1498. count += iv->iov_len;
  1499. if (unlikely((ssize_t)(count|iv->iov_len) < 0))
  1500. return -EINVAL;
  1501. if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
  1502. continue;
  1503. if (seg == 0)
  1504. return -EFAULT;
  1505. nr_segs = seg;
  1506. count -= iv->iov_len; /* This segment is no good */
  1507. break;
  1508. }
  1509. retval = 0;
  1510. if (count) {
  1511. for (seg = 0; seg < nr_segs; seg++) {
  1512. read_descriptor_t desc;
  1513. desc.written = 0;
  1514. desc.arg.buf = iov[seg].iov_base;
  1515. desc.count = iov[seg].iov_len;
  1516. if (desc.count == 0)
  1517. continue;
  1518. desc.error = 0;
  1519. do_generic_file_read(filp, ppos, &desc,
  1520. btrfs_read_actor);
  1521. retval += desc.written;
  1522. if (desc.error) {
  1523. retval = retval ?: desc.error;
  1524. break;
  1525. }
  1526. }
  1527. }
  1528. return retval;
  1529. }
  1530. static int create_subvol(struct btrfs_root *root, char *name, int namelen)
  1531. {
  1532. struct btrfs_trans_handle *trans;
  1533. struct btrfs_key key;
  1534. struct btrfs_root_item root_item;
  1535. struct btrfs_inode_item *inode_item;
  1536. struct buffer_head *subvol;
  1537. struct btrfs_leaf *leaf;
  1538. struct btrfs_root *new_root;
  1539. struct inode *inode;
  1540. int ret;
  1541. u64 objectid;
  1542. u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
  1543. mutex_lock(&root->fs_info->fs_mutex);
  1544. trans = btrfs_start_transaction(root, 1);
  1545. BUG_ON(!trans);
  1546. subvol = btrfs_alloc_free_block(trans, root);
  1547. leaf = btrfs_buffer_leaf(subvol);
  1548. btrfs_set_header_nritems(&leaf->header, 0);
  1549. btrfs_set_header_level(&leaf->header, 0);
  1550. btrfs_set_header_blocknr(&leaf->header, subvol->b_blocknr);
  1551. btrfs_set_header_generation(&leaf->header, trans->transid);
  1552. memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
  1553. sizeof(leaf->header.fsid));
  1554. inode_item = &root_item.inode;
  1555. memset(inode_item, 0, sizeof(*inode_item));
  1556. btrfs_set_inode_generation(inode_item, 1);
  1557. btrfs_set_inode_size(inode_item, 3);
  1558. btrfs_set_inode_nlink(inode_item, 1);
  1559. btrfs_set_inode_nblocks(inode_item, 1);
  1560. btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
  1561. btrfs_set_root_blocknr(&root_item, subvol->b_blocknr);
  1562. btrfs_set_root_refs(&root_item, 1);
  1563. mark_buffer_dirty(subvol);
  1564. brelse(subvol);
  1565. subvol = NULL;
  1566. ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
  1567. 0, &objectid);
  1568. BUG_ON(ret);
  1569. btrfs_set_root_dirid(&root_item, new_dirid);
  1570. key.objectid = objectid;
  1571. key.offset = 1;
  1572. key.flags = 0;
  1573. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  1574. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  1575. &root_item);
  1576. BUG_ON(ret);
  1577. /*
  1578. * insert the directory item
  1579. */
  1580. key.offset = (u64)-1;
  1581. ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
  1582. name, namelen,
  1583. root->fs_info->sb->s_root->d_inode->i_ino,
  1584. &key, 0);
  1585. BUG_ON(ret);
  1586. ret = btrfs_commit_transaction(trans, root);
  1587. BUG_ON(ret);
  1588. new_root = btrfs_read_fs_root(root->fs_info, &key);
  1589. BUG_ON(!new_root);
  1590. trans = btrfs_start_transaction(new_root, 1);
  1591. BUG_ON(!trans);
  1592. inode = btrfs_new_inode(trans, new_root, new_dirid, S_IFDIR | 0700);
  1593. inode->i_op = &btrfs_dir_inode_operations;
  1594. inode->i_fop = &btrfs_dir_file_operations;
  1595. ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
  1596. BUG_ON(ret);
  1597. inode->i_nlink = 1;
  1598. inode->i_size = 6;
  1599. ret = btrfs_update_inode(trans, new_root, inode);
  1600. BUG_ON(ret);
  1601. ret = btrfs_commit_transaction(trans, new_root);
  1602. BUG_ON(ret);
  1603. iput(inode);
  1604. mutex_unlock(&root->fs_info->fs_mutex);
  1605. return 0;
  1606. }
  1607. static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
  1608. {
  1609. struct btrfs_trans_handle *trans;
  1610. struct btrfs_key key;
  1611. struct btrfs_root_item new_root_item;
  1612. int ret;
  1613. u64 objectid;
  1614. if (!root->ref_cows)
  1615. return -EINVAL;
  1616. mutex_lock(&root->fs_info->fs_mutex);
  1617. trans = btrfs_start_transaction(root, 1);
  1618. BUG_ON(!trans);
  1619. ret = btrfs_update_inode(trans, root, root->inode);
  1620. BUG_ON(ret);
  1621. ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
  1622. 0, &objectid);
  1623. BUG_ON(ret);
  1624. memcpy(&new_root_item, &root->root_item,
  1625. sizeof(new_root_item));
  1626. key.objectid = objectid;
  1627. key.offset = 1;
  1628. key.flags = 0;
  1629. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  1630. btrfs_set_root_blocknr(&new_root_item, root->node->b_blocknr);
  1631. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  1632. &new_root_item);
  1633. BUG_ON(ret);
  1634. printk("adding snapshot name %.*s root %Lu %Lu %u\n", namelen, name, key.objectid, key.offset, key.flags);
  1635. /*
  1636. * insert the directory item
  1637. */
  1638. key.offset = (u64)-1;
  1639. ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
  1640. name, namelen,
  1641. root->fs_info->sb->s_root->d_inode->i_ino,
  1642. &key, 0);
  1643. BUG_ON(ret);
  1644. ret = btrfs_inc_root_ref(trans, root);
  1645. BUG_ON(ret);
  1646. ret = btrfs_commit_transaction(trans, root);
  1647. BUG_ON(ret);
  1648. mutex_unlock(&root->fs_info->fs_mutex);
  1649. return 0;
  1650. }
  1651. static int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
  1652. cmd, unsigned long arg)
  1653. {
  1654. struct btrfs_root *root = BTRFS_I(inode)->root;
  1655. struct btrfs_ioctl_vol_args vol_args;
  1656. int ret;
  1657. int namelen;
  1658. struct btrfs_path *path;
  1659. u64 root_dirid;
  1660. switch (cmd) {
  1661. case BTRFS_IOC_SNAP_CREATE:
  1662. if (copy_from_user(&vol_args,
  1663. (struct btrfs_ioctl_vol_args __user *)arg,
  1664. sizeof(vol_args)))
  1665. return -EFAULT;
  1666. namelen = strlen(vol_args.name);
  1667. if (namelen > BTRFS_VOL_NAME_MAX)
  1668. return -EINVAL;
  1669. path = btrfs_alloc_path();
  1670. if (!path)
  1671. return -ENOMEM;
  1672. root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
  1673. mutex_lock(&root->fs_info->fs_mutex);
  1674. ret = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
  1675. path, root_dirid,
  1676. vol_args.name, namelen, 0);
  1677. mutex_unlock(&root->fs_info->fs_mutex);
  1678. btrfs_free_path(path);
  1679. if (ret == 0)
  1680. return -EEXIST;
  1681. if (root == root->fs_info->tree_root)
  1682. ret = create_subvol(root, vol_args.name, namelen);
  1683. else
  1684. ret = create_snapshot(root, vol_args.name, namelen);
  1685. WARN_ON(ret);
  1686. break;
  1687. default:
  1688. return -ENOTTY;
  1689. }
  1690. return 0;
  1691. }
  1692. static struct kmem_cache *btrfs_inode_cachep;
  1693. struct kmem_cache *btrfs_trans_handle_cachep;
  1694. struct kmem_cache *btrfs_transaction_cachep;
  1695. struct kmem_cache *btrfs_bit_radix_cachep;
  1696. struct kmem_cache *btrfs_path_cachep;
  1697. /*
  1698. * Called inside transaction, so use GFP_NOFS
  1699. */
  1700. static struct inode *btrfs_alloc_inode(struct super_block *sb)
  1701. {
  1702. struct btrfs_inode *ei;
  1703. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  1704. if (!ei)
  1705. return NULL;
  1706. return &ei->vfs_inode;
  1707. }
  1708. static void btrfs_destroy_inode(struct inode *inode)
  1709. {
  1710. WARN_ON(!list_empty(&inode->i_dentry));
  1711. WARN_ON(inode->i_data.nrpages);
  1712. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  1713. }
  1714. static void init_once(void * foo, struct kmem_cache * cachep,
  1715. unsigned long flags)
  1716. {
  1717. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  1718. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  1719. SLAB_CTOR_CONSTRUCTOR) {
  1720. inode_init_once(&ei->vfs_inode);
  1721. }
  1722. }
  1723. static int init_inodecache(void)
  1724. {
  1725. btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
  1726. sizeof(struct btrfs_inode),
  1727. 0, (SLAB_RECLAIM_ACCOUNT|
  1728. SLAB_MEM_SPREAD),
  1729. init_once, NULL);
  1730. btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
  1731. sizeof(struct btrfs_trans_handle),
  1732. 0, (SLAB_RECLAIM_ACCOUNT|
  1733. SLAB_MEM_SPREAD),
  1734. NULL, NULL);
  1735. btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
  1736. sizeof(struct btrfs_transaction),
  1737. 0, (SLAB_RECLAIM_ACCOUNT|
  1738. SLAB_MEM_SPREAD),
  1739. NULL, NULL);
  1740. btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
  1741. sizeof(struct btrfs_transaction),
  1742. 0, (SLAB_RECLAIM_ACCOUNT|
  1743. SLAB_MEM_SPREAD),
  1744. NULL, NULL);
  1745. btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
  1746. 256,
  1747. 0, (SLAB_RECLAIM_ACCOUNT|
  1748. SLAB_MEM_SPREAD |
  1749. SLAB_DESTROY_BY_RCU),
  1750. NULL, NULL);
  1751. if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
  1752. btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
  1753. return -ENOMEM;
  1754. return 0;
  1755. }
  1756. static void destroy_inodecache(void)
  1757. {
  1758. kmem_cache_destroy(btrfs_inode_cachep);
  1759. kmem_cache_destroy(btrfs_trans_handle_cachep);
  1760. kmem_cache_destroy(btrfs_transaction_cachep);
  1761. kmem_cache_destroy(btrfs_bit_radix_cachep);
  1762. kmem_cache_destroy(btrfs_path_cachep);
  1763. }
  1764. static int btrfs_get_sb(struct file_system_type *fs_type,
  1765. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  1766. {
  1767. return get_sb_bdev(fs_type, flags, dev_name, data,
  1768. btrfs_fill_super, mnt);
  1769. }
  1770. static struct file_system_type btrfs_fs_type = {
  1771. .owner = THIS_MODULE,
  1772. .name = "btrfs",
  1773. .get_sb = btrfs_get_sb,
  1774. .kill_sb = kill_block_super,
  1775. .fs_flags = FS_REQUIRES_DEV,
  1776. };
  1777. static struct super_operations btrfs_super_ops = {
  1778. .statfs = simple_statfs,
  1779. .delete_inode = btrfs_delete_inode,
  1780. .put_super = btrfs_put_super,
  1781. .read_inode = btrfs_read_locked_inode,
  1782. .write_super = btrfs_write_super,
  1783. .sync_fs = btrfs_sync_fs,
  1784. .write_inode = btrfs_write_inode,
  1785. .alloc_inode = btrfs_alloc_inode,
  1786. .destroy_inode = btrfs_destroy_inode,
  1787. };
  1788. static struct inode_operations btrfs_dir_inode_operations = {
  1789. .lookup = btrfs_lookup,
  1790. .create = btrfs_create,
  1791. .unlink = btrfs_unlink,
  1792. .mkdir = btrfs_mkdir,
  1793. .rmdir = btrfs_rmdir,
  1794. };
  1795. static struct inode_operations btrfs_dir_ro_inode_operations = {
  1796. .lookup = btrfs_lookup,
  1797. };
  1798. static struct file_operations btrfs_dir_file_operations = {
  1799. .llseek = generic_file_llseek,
  1800. .read = generic_read_dir,
  1801. .readdir = btrfs_readdir,
  1802. .ioctl = btrfs_ioctl,
  1803. };
  1804. static struct address_space_operations btrfs_aops = {
  1805. .readpage = btrfs_readpage,
  1806. .readpages = btrfs_readpages,
  1807. .writepage = btrfs_writepage,
  1808. .sync_page = block_sync_page,
  1809. .prepare_write = btrfs_prepare_write,
  1810. .commit_write = btrfs_commit_write,
  1811. };
  1812. static struct inode_operations btrfs_file_inode_operations = {
  1813. .truncate = btrfs_truncate,
  1814. };
  1815. static struct file_operations btrfs_file_operations = {
  1816. .llseek = generic_file_llseek,
  1817. .read = do_sync_read,
  1818. .aio_read = btrfs_file_aio_read,
  1819. .write = btrfs_file_write,
  1820. .mmap = generic_file_mmap,
  1821. .open = generic_file_open,
  1822. .ioctl = btrfs_ioctl,
  1823. };
  1824. static int __init init_btrfs_fs(void)
  1825. {
  1826. int err;
  1827. printk("btrfs loaded!\n");
  1828. err = init_inodecache();
  1829. if (err)
  1830. return err;
  1831. kset_set_kset_s(&btrfs_subsys, fs_subsys);
  1832. err = subsystem_register(&btrfs_subsys);
  1833. if (err)
  1834. goto out;
  1835. return register_filesystem(&btrfs_fs_type);
  1836. out:
  1837. destroy_inodecache();
  1838. return err;
  1839. }
  1840. static void __exit exit_btrfs_fs(void)
  1841. {
  1842. destroy_inodecache();
  1843. unregister_filesystem(&btrfs_fs_type);
  1844. subsystem_unregister(&btrfs_subsys);
  1845. printk("btrfs unloaded\n");
  1846. }
  1847. module_init(init_btrfs_fs)
  1848. module_exit(exit_btrfs_fs)
  1849. MODULE_LICENSE("GPL");