super.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662
  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. dentry->d_inode->i_ctime = dir->i_ctime;
  136. err:
  137. btrfs_release_path(root, path);
  138. btrfs_free_path(path);
  139. if (ret == 0) {
  140. inode_dec_link_count(dentry->d_inode);
  141. dir->i_size -= name_len * 2;
  142. mark_inode_dirty(dir);
  143. }
  144. return ret;
  145. }
  146. static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
  147. {
  148. struct btrfs_root *root;
  149. struct btrfs_trans_handle *trans;
  150. int ret;
  151. root = BTRFS_I(dir)->root;
  152. mutex_lock(&root->fs_info->fs_mutex);
  153. trans = btrfs_start_transaction(root, 1);
  154. ret = btrfs_unlink_trans(trans, root, dir, dentry);
  155. btrfs_end_transaction(trans, root);
  156. mutex_unlock(&root->fs_info->fs_mutex);
  157. return ret;
  158. }
  159. static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
  160. {
  161. struct inode *inode = dentry->d_inode;
  162. int err;
  163. int ret;
  164. struct btrfs_root *root = BTRFS_I(dir)->root;
  165. struct btrfs_path *path;
  166. struct btrfs_key key;
  167. struct btrfs_trans_handle *trans;
  168. struct btrfs_key found_key;
  169. int found_type;
  170. struct btrfs_leaf *leaf;
  171. char *goodnames = "..";
  172. path = btrfs_alloc_path();
  173. BUG_ON(!path);
  174. btrfs_init_path(path);
  175. mutex_lock(&root->fs_info->fs_mutex);
  176. trans = btrfs_start_transaction(root, 1);
  177. key.objectid = inode->i_ino;
  178. key.offset = (u64)-1;
  179. key.flags = (u32)-1;
  180. while(1) {
  181. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  182. if (ret < 0) {
  183. err = ret;
  184. goto out;
  185. }
  186. BUG_ON(ret == 0);
  187. if (path->slots[0] == 0) {
  188. err = -ENOENT;
  189. goto out;
  190. }
  191. path->slots[0]--;
  192. leaf = btrfs_buffer_leaf(path->nodes[0]);
  193. btrfs_disk_key_to_cpu(&found_key,
  194. &leaf->items[path->slots[0]].key);
  195. found_type = btrfs_key_type(&found_key);
  196. if (found_key.objectid != inode->i_ino) {
  197. err = -ENOENT;
  198. goto out;
  199. }
  200. if ((found_type != BTRFS_DIR_ITEM_KEY &&
  201. found_type != BTRFS_DIR_INDEX_KEY) ||
  202. (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
  203. !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
  204. err = -ENOTEMPTY;
  205. goto out;
  206. }
  207. ret = btrfs_del_item(trans, root, path);
  208. BUG_ON(ret);
  209. if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
  210. break;
  211. btrfs_release_path(root, path);
  212. }
  213. ret = 0;
  214. btrfs_release_path(root, path);
  215. /* now the directory is empty */
  216. err = btrfs_unlink_trans(trans, root, dir, dentry);
  217. if (!err) {
  218. inode->i_size = 0;
  219. }
  220. out:
  221. btrfs_release_path(root, path);
  222. btrfs_free_path(path);
  223. mutex_unlock(&root->fs_info->fs_mutex);
  224. ret = btrfs_end_transaction(trans, root);
  225. if (ret && !err)
  226. err = ret;
  227. return err;
  228. }
  229. static int btrfs_free_inode(struct btrfs_trans_handle *trans,
  230. struct btrfs_root *root,
  231. struct inode *inode)
  232. {
  233. struct btrfs_path *path;
  234. int ret;
  235. clear_inode(inode);
  236. path = btrfs_alloc_path();
  237. BUG_ON(!path);
  238. btrfs_init_path(path);
  239. ret = btrfs_lookup_inode(trans, root, path,
  240. &BTRFS_I(inode)->location, -1);
  241. BUG_ON(ret);
  242. ret = btrfs_del_item(trans, root, path);
  243. BUG_ON(ret);
  244. btrfs_free_path(path);
  245. return ret;
  246. }
  247. static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
  248. struct btrfs_root *root,
  249. struct inode *inode)
  250. {
  251. int ret;
  252. struct btrfs_path *path;
  253. struct btrfs_key key;
  254. struct btrfs_disk_key *found_key;
  255. struct btrfs_leaf *leaf;
  256. struct btrfs_file_extent_item *fi = NULL;
  257. u64 extent_start = 0;
  258. u64 extent_num_blocks = 0;
  259. int found_extent;
  260. path = btrfs_alloc_path();
  261. BUG_ON(!path);
  262. /* FIXME, add redo link to tree so we don't leak on crash */
  263. key.objectid = inode->i_ino;
  264. key.offset = (u64)-1;
  265. key.flags = 0;
  266. /*
  267. * use BTRFS_CSUM_ITEM_KEY because it is larger than inline keys
  268. * or extent data
  269. */
  270. btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY);
  271. while(1) {
  272. btrfs_init_path(path);
  273. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  274. if (ret < 0) {
  275. goto error;
  276. }
  277. if (ret > 0) {
  278. BUG_ON(path->slots[0] == 0);
  279. path->slots[0]--;
  280. }
  281. leaf = btrfs_buffer_leaf(path->nodes[0]);
  282. found_key = &leaf->items[path->slots[0]].key;
  283. if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
  284. break;
  285. if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY &&
  286. btrfs_disk_key_type(found_key) != BTRFS_INLINE_DATA_KEY &&
  287. btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY)
  288. break;
  289. if (btrfs_disk_key_offset(found_key) < inode->i_size)
  290. break;
  291. found_extent = 0;
  292. if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) {
  293. fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  294. path->slots[0],
  295. struct btrfs_file_extent_item);
  296. if (btrfs_file_extent_type(fi) !=
  297. BTRFS_FILE_EXTENT_INLINE) {
  298. extent_start =
  299. btrfs_file_extent_disk_blocknr(fi);
  300. extent_num_blocks =
  301. btrfs_file_extent_disk_num_blocks(fi);
  302. /* FIXME blocksize != 4096 */
  303. inode->i_blocks -=
  304. btrfs_file_extent_num_blocks(fi) << 3;
  305. found_extent = 1;
  306. }
  307. }
  308. ret = btrfs_del_item(trans, root, path);
  309. BUG_ON(ret);
  310. btrfs_release_path(root, path);
  311. if (found_extent) {
  312. ret = btrfs_free_extent(trans, root, extent_start,
  313. extent_num_blocks, 0);
  314. BUG_ON(ret);
  315. }
  316. }
  317. ret = 0;
  318. error:
  319. btrfs_release_path(root, path);
  320. btrfs_free_path(path);
  321. return ret;
  322. }
  323. static void btrfs_delete_inode(struct inode *inode)
  324. {
  325. struct btrfs_trans_handle *trans;
  326. struct btrfs_root *root = BTRFS_I(inode)->root;
  327. int ret;
  328. truncate_inode_pages(&inode->i_data, 0);
  329. if (is_bad_inode(inode)) {
  330. goto no_delete;
  331. }
  332. inode->i_size = 0;
  333. mutex_lock(&root->fs_info->fs_mutex);
  334. trans = btrfs_start_transaction(root, 1);
  335. if (S_ISREG(inode->i_mode)) {
  336. ret = btrfs_truncate_in_trans(trans, root, inode);
  337. BUG_ON(ret);
  338. }
  339. btrfs_free_inode(trans, root, inode);
  340. btrfs_end_transaction(trans, root);
  341. mutex_unlock(&root->fs_info->fs_mutex);
  342. return;
  343. no_delete:
  344. clear_inode(inode);
  345. }
  346. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  347. struct btrfs_key *location)
  348. {
  349. const char *name = dentry->d_name.name;
  350. int namelen = dentry->d_name.len;
  351. struct btrfs_dir_item *di;
  352. struct btrfs_path *path;
  353. struct btrfs_root *root = BTRFS_I(dir)->root;
  354. int ret;
  355. path = btrfs_alloc_path();
  356. BUG_ON(!path);
  357. btrfs_init_path(path);
  358. ret = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
  359. namelen, 0);
  360. if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) {
  361. location->objectid = 0;
  362. ret = 0;
  363. goto out;
  364. }
  365. di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  366. struct btrfs_dir_item);
  367. btrfs_disk_key_to_cpu(location, &di->location);
  368. out:
  369. btrfs_release_path(root, path);
  370. btrfs_free_path(path);
  371. return ret;
  372. }
  373. int fixup_tree_root_location(struct btrfs_root *root,
  374. struct btrfs_key *location,
  375. struct btrfs_root **sub_root)
  376. {
  377. struct btrfs_path *path;
  378. struct btrfs_root_item *ri;
  379. if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
  380. return 0;
  381. if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
  382. return 0;
  383. path = btrfs_alloc_path();
  384. BUG_ON(!path);
  385. mutex_lock(&root->fs_info->fs_mutex);
  386. *sub_root = btrfs_read_fs_root(root->fs_info, location);
  387. if (IS_ERR(*sub_root))
  388. return PTR_ERR(*sub_root);
  389. ri = &(*sub_root)->root_item;
  390. location->objectid = btrfs_root_dirid(ri);
  391. location->flags = 0;
  392. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  393. location->offset = 0;
  394. btrfs_free_path(path);
  395. mutex_unlock(&root->fs_info->fs_mutex);
  396. return 0;
  397. }
  398. int btrfs_init_locked_inode(struct inode *inode, void *p)
  399. {
  400. struct btrfs_iget_args *args = p;
  401. inode->i_ino = args->ino;
  402. BTRFS_I(inode)->root = args->root;
  403. return 0;
  404. }
  405. int btrfs_find_actor(struct inode *inode, void *opaque)
  406. {
  407. struct btrfs_iget_args *args = opaque;
  408. return (args->ino == inode->i_ino &&
  409. args->root == BTRFS_I(inode)->root);
  410. }
  411. struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
  412. struct btrfs_root *root)
  413. {
  414. struct inode *inode;
  415. struct btrfs_iget_args args;
  416. args.ino = objectid;
  417. args.root = root;
  418. inode = iget5_locked(s, objectid, btrfs_find_actor,
  419. btrfs_init_locked_inode,
  420. (void *)&args);
  421. return inode;
  422. }
  423. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  424. struct nameidata *nd)
  425. {
  426. struct inode * inode;
  427. struct btrfs_inode *bi = BTRFS_I(dir);
  428. struct btrfs_root *root = bi->root;
  429. struct btrfs_root *sub_root = root;
  430. struct btrfs_key location;
  431. int ret;
  432. if (dentry->d_name.len > BTRFS_NAME_LEN)
  433. return ERR_PTR(-ENAMETOOLONG);
  434. mutex_lock(&root->fs_info->fs_mutex);
  435. ret = btrfs_inode_by_name(dir, dentry, &location);
  436. mutex_unlock(&root->fs_info->fs_mutex);
  437. if (ret < 0)
  438. return ERR_PTR(ret);
  439. inode = NULL;
  440. if (location.objectid) {
  441. ret = fixup_tree_root_location(root, &location, &sub_root);
  442. if (ret < 0)
  443. return ERR_PTR(ret);
  444. if (ret > 0)
  445. return ERR_PTR(-ENOENT);
  446. inode = btrfs_iget_locked(dir->i_sb, location.objectid,
  447. sub_root);
  448. if (!inode)
  449. return ERR_PTR(-EACCES);
  450. if (inode->i_state & I_NEW) {
  451. if (sub_root != root) {
  452. printk("adding new root for inode %lu root %p (found %p)\n", inode->i_ino, sub_root, BTRFS_I(inode)->root);
  453. igrab(inode);
  454. sub_root->inode = inode;
  455. }
  456. BTRFS_I(inode)->root = sub_root;
  457. memcpy(&BTRFS_I(inode)->location, &location,
  458. sizeof(location));
  459. btrfs_read_locked_inode(inode);
  460. unlock_new_inode(inode);
  461. }
  462. }
  463. return d_splice_alias(inode, dentry);
  464. }
  465. static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
  466. {
  467. struct inode *inode = filp->f_path.dentry->d_inode;
  468. struct btrfs_root *root = BTRFS_I(inode)->root;
  469. struct btrfs_item *item;
  470. struct btrfs_dir_item *di;
  471. struct btrfs_key key;
  472. struct btrfs_path *path;
  473. int ret;
  474. u32 nritems;
  475. struct btrfs_leaf *leaf;
  476. int slot;
  477. int advance;
  478. unsigned char d_type = DT_UNKNOWN;
  479. int over = 0;
  480. int key_type = BTRFS_DIR_ITEM_KEY;
  481. /* FIXME, use a real flag for deciding about the key type */
  482. if (root->fs_info->tree_root == root)
  483. key_type = BTRFS_DIR_ITEM_KEY;
  484. mutex_lock(&root->fs_info->fs_mutex);
  485. key.objectid = inode->i_ino;
  486. key.flags = 0;
  487. btrfs_set_key_type(&key, key_type);
  488. key.offset = filp->f_pos;
  489. path = btrfs_alloc_path();
  490. btrfs_init_path(path);
  491. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  492. if (ret < 0)
  493. goto err;
  494. advance = 0;
  495. while(1) {
  496. leaf = btrfs_buffer_leaf(path->nodes[0]);
  497. nritems = btrfs_header_nritems(&leaf->header);
  498. slot = path->slots[0];
  499. if (advance || slot >= nritems) {
  500. if (slot >= nritems -1) {
  501. ret = btrfs_next_leaf(root, path);
  502. if (ret)
  503. break;
  504. leaf = btrfs_buffer_leaf(path->nodes[0]);
  505. nritems = btrfs_header_nritems(&leaf->header);
  506. slot = path->slots[0];
  507. } else {
  508. slot++;
  509. path->slots[0]++;
  510. }
  511. }
  512. advance = 1;
  513. item = leaf->items + slot;
  514. if (btrfs_disk_key_objectid(&item->key) != key.objectid)
  515. break;
  516. if (btrfs_disk_key_type(&item->key) != key_type)
  517. break;
  518. if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
  519. continue;
  520. filp->f_pos = btrfs_disk_key_offset(&item->key);
  521. advance = 1;
  522. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  523. over = filldir(dirent, (const char *)(di + 1),
  524. btrfs_dir_name_len(di),
  525. btrfs_disk_key_offset(&item->key),
  526. btrfs_disk_key_objectid(&di->location), d_type);
  527. if (over)
  528. goto nopos;
  529. }
  530. filp->f_pos++;
  531. nopos:
  532. ret = 0;
  533. err:
  534. btrfs_release_path(root, path);
  535. btrfs_free_path(path);
  536. mutex_unlock(&root->fs_info->fs_mutex);
  537. return ret;
  538. }
  539. static void btrfs_put_super (struct super_block * sb)
  540. {
  541. struct btrfs_root *root = btrfs_sb(sb);
  542. int ret;
  543. ret = close_ctree(root);
  544. if (ret) {
  545. printk("close ctree returns %d\n", ret);
  546. }
  547. sb->s_fs_info = NULL;
  548. }
  549. static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
  550. {
  551. struct inode * inode;
  552. struct dentry * root_dentry;
  553. struct btrfs_super_block *disk_super;
  554. struct btrfs_root *tree_root;
  555. struct btrfs_inode *bi;
  556. sb->s_maxbytes = MAX_LFS_FILESIZE;
  557. sb->s_magic = BTRFS_SUPER_MAGIC;
  558. sb->s_op = &btrfs_super_ops;
  559. sb->s_time_gran = 1;
  560. tree_root = open_ctree(sb);
  561. if (!tree_root) {
  562. printk("btrfs: open_ctree failed\n");
  563. return -EIO;
  564. }
  565. sb->s_fs_info = tree_root;
  566. disk_super = tree_root->fs_info->disk_super;
  567. printk("read in super total blocks %Lu root %Lu\n",
  568. btrfs_super_total_blocks(disk_super),
  569. btrfs_super_root_dir(disk_super));
  570. inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
  571. tree_root);
  572. bi = BTRFS_I(inode);
  573. bi->location.objectid = inode->i_ino;
  574. bi->location.offset = 0;
  575. bi->location.flags = 0;
  576. bi->root = tree_root;
  577. btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
  578. if (!inode)
  579. return -ENOMEM;
  580. if (inode->i_state & I_NEW) {
  581. btrfs_read_locked_inode(inode);
  582. unlock_new_inode(inode);
  583. }
  584. root_dentry = d_alloc_root(inode);
  585. if (!root_dentry) {
  586. iput(inode);
  587. return -ENOMEM;
  588. }
  589. sb->s_root = root_dentry;
  590. return 0;
  591. }
  592. static void fill_inode_item(struct btrfs_inode_item *item,
  593. struct inode *inode)
  594. {
  595. btrfs_set_inode_uid(item, inode->i_uid);
  596. btrfs_set_inode_gid(item, inode->i_gid);
  597. btrfs_set_inode_size(item, inode->i_size);
  598. btrfs_set_inode_mode(item, inode->i_mode);
  599. btrfs_set_inode_nlink(item, inode->i_nlink);
  600. btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
  601. btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
  602. btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
  603. btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
  604. btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
  605. btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
  606. btrfs_set_inode_nblocks(item, inode->i_blocks);
  607. btrfs_set_inode_generation(item, inode->i_generation);
  608. }
  609. static int btrfs_update_inode(struct btrfs_trans_handle *trans,
  610. struct btrfs_root *root,
  611. struct inode *inode)
  612. {
  613. struct btrfs_inode_item *inode_item;
  614. struct btrfs_path *path;
  615. int ret;
  616. path = btrfs_alloc_path();
  617. BUG_ON(!path);
  618. btrfs_init_path(path);
  619. ret = btrfs_lookup_inode(trans, root, path,
  620. &BTRFS_I(inode)->location, 1);
  621. if (ret) {
  622. if (ret > 0)
  623. ret = -ENOENT;
  624. goto failed;
  625. }
  626. inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  627. path->slots[0],
  628. struct btrfs_inode_item);
  629. fill_inode_item(inode_item, inode);
  630. btrfs_mark_buffer_dirty(path->nodes[0]);
  631. ret = 0;
  632. failed:
  633. btrfs_release_path(root, path);
  634. btrfs_free_path(path);
  635. return ret;
  636. }
  637. static int btrfs_write_inode(struct inode *inode, int wait)
  638. {
  639. struct btrfs_root *root = BTRFS_I(inode)->root;
  640. struct btrfs_trans_handle *trans;
  641. int ret;
  642. mutex_lock(&root->fs_info->fs_mutex);
  643. trans = btrfs_start_transaction(root, 1);
  644. ret = btrfs_update_inode(trans, root, inode);
  645. if (wait)
  646. btrfs_commit_transaction(trans, root);
  647. else
  648. btrfs_end_transaction(trans, root);
  649. mutex_unlock(&root->fs_info->fs_mutex);
  650. return ret;
  651. }
  652. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  653. struct btrfs_root *root,
  654. u64 objectid, int mode)
  655. {
  656. struct inode *inode;
  657. struct btrfs_inode_item inode_item;
  658. struct btrfs_key *location;
  659. int ret;
  660. inode = new_inode(root->fs_info->sb);
  661. if (!inode)
  662. return ERR_PTR(-ENOMEM);
  663. BTRFS_I(inode)->root = root;
  664. inode->i_uid = current->fsuid;
  665. inode->i_gid = current->fsgid;
  666. inode->i_mode = mode;
  667. inode->i_ino = objectid;
  668. inode->i_blocks = 0;
  669. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  670. fill_inode_item(&inode_item, inode);
  671. location = &BTRFS_I(inode)->location;
  672. location->objectid = objectid;
  673. location->flags = 0;
  674. location->offset = 0;
  675. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  676. ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
  677. BUG_ON(ret);
  678. insert_inode_hash(inode);
  679. return inode;
  680. }
  681. static int btrfs_add_link(struct btrfs_trans_handle *trans,
  682. struct dentry *dentry, struct inode *inode)
  683. {
  684. int ret;
  685. struct btrfs_key key;
  686. struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
  687. key.objectid = inode->i_ino;
  688. key.flags = 0;
  689. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  690. key.offset = 0;
  691. ret = btrfs_insert_dir_item(trans, root,
  692. dentry->d_name.name, dentry->d_name.len,
  693. dentry->d_parent->d_inode->i_ino,
  694. &key, 0);
  695. if (ret == 0) {
  696. dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
  697. ret = btrfs_update_inode(trans, root,
  698. dentry->d_parent->d_inode);
  699. }
  700. return ret;
  701. }
  702. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  703. struct dentry *dentry, struct inode *inode)
  704. {
  705. int err = btrfs_add_link(trans, dentry, inode);
  706. if (!err) {
  707. d_instantiate(dentry, inode);
  708. return 0;
  709. }
  710. if (err > 0)
  711. err = -EEXIST;
  712. return err;
  713. }
  714. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  715. int mode, struct nameidata *nd)
  716. {
  717. struct btrfs_trans_handle *trans;
  718. struct btrfs_root *root = BTRFS_I(dir)->root;
  719. struct inode *inode;
  720. int err;
  721. int drop_inode = 0;
  722. u64 objectid;
  723. mutex_lock(&root->fs_info->fs_mutex);
  724. trans = btrfs_start_transaction(root, 1);
  725. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  726. if (err) {
  727. err = -ENOSPC;
  728. goto out_unlock;
  729. }
  730. inode = btrfs_new_inode(trans, root, objectid, mode);
  731. err = PTR_ERR(inode);
  732. if (IS_ERR(inode))
  733. goto out_unlock;
  734. // FIXME mark the inode dirty
  735. err = btrfs_add_nondir(trans, dentry, inode);
  736. if (err)
  737. drop_inode = 1;
  738. else {
  739. inode->i_mapping->a_ops = &btrfs_aops;
  740. inode->i_fop = &btrfs_file_operations;
  741. inode->i_op = &btrfs_file_inode_operations;
  742. }
  743. dir->i_sb->s_dirt = 1;
  744. out_unlock:
  745. btrfs_end_transaction(trans, root);
  746. mutex_unlock(&root->fs_info->fs_mutex);
  747. if (drop_inode) {
  748. inode_dec_link_count(inode);
  749. iput(inode);
  750. }
  751. return err;
  752. }
  753. static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
  754. struct btrfs_root *root,
  755. u64 objectid, u64 dirid)
  756. {
  757. int ret;
  758. char buf[2];
  759. struct btrfs_key key;
  760. buf[0] = '.';
  761. buf[1] = '.';
  762. key.objectid = objectid;
  763. key.offset = 0;
  764. key.flags = 0;
  765. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  766. ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
  767. &key, 1);
  768. if (ret)
  769. goto error;
  770. key.objectid = dirid;
  771. ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
  772. &key, 1);
  773. if (ret)
  774. goto error;
  775. error:
  776. return ret;
  777. }
  778. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  779. {
  780. struct inode *inode;
  781. struct btrfs_trans_handle *trans;
  782. struct btrfs_root *root = BTRFS_I(dir)->root;
  783. int err = 0;
  784. int drop_on_err = 0;
  785. u64 objectid;
  786. mutex_lock(&root->fs_info->fs_mutex);
  787. trans = btrfs_start_transaction(root, 1);
  788. if (IS_ERR(trans)) {
  789. err = PTR_ERR(trans);
  790. goto out_unlock;
  791. }
  792. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  793. if (err) {
  794. err = -ENOSPC;
  795. goto out_unlock;
  796. }
  797. inode = btrfs_new_inode(trans, root, objectid, S_IFDIR | mode);
  798. if (IS_ERR(inode)) {
  799. err = PTR_ERR(inode);
  800. goto out_fail;
  801. }
  802. drop_on_err = 1;
  803. inode->i_op = &btrfs_dir_inode_operations;
  804. inode->i_fop = &btrfs_dir_file_operations;
  805. err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
  806. if (err)
  807. goto out_fail;
  808. inode->i_size = 6;
  809. err = btrfs_update_inode(trans, root, inode);
  810. if (err)
  811. goto out_fail;
  812. err = btrfs_add_link(trans, dentry, inode);
  813. if (err)
  814. goto out_fail;
  815. d_instantiate(dentry, inode);
  816. drop_on_err = 0;
  817. out_fail:
  818. btrfs_end_transaction(trans, root);
  819. out_unlock:
  820. mutex_unlock(&root->fs_info->fs_mutex);
  821. if (drop_on_err)
  822. iput(inode);
  823. return err;
  824. }
  825. static int btrfs_sync_fs(struct super_block *sb, int wait)
  826. {
  827. struct btrfs_trans_handle *trans;
  828. struct btrfs_root *root;
  829. int ret;
  830. root = btrfs_sb(sb);
  831. sb->s_dirt = 0;
  832. if (!wait) {
  833. filemap_flush(root->fs_info->btree_inode->i_mapping);
  834. return 0;
  835. }
  836. filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
  837. mutex_lock(&root->fs_info->fs_mutex);
  838. trans = btrfs_start_transaction(root, 1);
  839. ret = btrfs_commit_transaction(trans, root);
  840. sb->s_dirt = 0;
  841. BUG_ON(ret);
  842. printk("btrfs sync_fs\n");
  843. mutex_unlock(&root->fs_info->fs_mutex);
  844. return 0;
  845. }
  846. #if 0
  847. static int btrfs_get_block_inline(struct inode *inode, sector_t iblock,
  848. struct buffer_head *result, int create)
  849. {
  850. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  851. struct btrfs_path *path;
  852. struct btrfs_key key;
  853. struct btrfs_leaf *leaf;
  854. int num_bytes = result->b_size;
  855. int item_size;
  856. int ret;
  857. u64 pos;
  858. char *ptr;
  859. int copy_size;
  860. int err = 0;
  861. char *safe_ptr;
  862. char *data_ptr;
  863. path = btrfs_alloc_path();
  864. BUG_ON(!path);
  865. WARN_ON(create);
  866. if (create) {
  867. return 0;
  868. }
  869. pos = iblock << inode->i_blkbits;
  870. key.objectid = inode->i_ino;
  871. key.flags = 0;
  872. btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
  873. ptr = kmap(result->b_page);
  874. safe_ptr = ptr;
  875. ptr += (pos & (PAGE_CACHE_SIZE -1));
  876. again:
  877. key.offset = pos;
  878. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  879. if (ret) {
  880. if (ret < 0)
  881. err = ret;
  882. else
  883. err = 0;
  884. goto out;
  885. }
  886. leaf = btrfs_buffer_leaf(path->nodes[0]);
  887. item_size = btrfs_item_size(leaf->items + path->slots[0]);
  888. copy_size = min(num_bytes, item_size);
  889. data_ptr = btrfs_item_ptr(leaf, path->slots[0], char);
  890. WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size);
  891. memcpy(ptr, data_ptr, copy_size);
  892. pos += copy_size;
  893. num_bytes -= copy_size;
  894. WARN_ON(num_bytes < 0);
  895. ptr += copy_size;
  896. btrfs_release_path(root, path);
  897. if (num_bytes != 0) {
  898. if (pos >= i_size_read(inode))
  899. memset(ptr, 0, num_bytes);
  900. else
  901. goto again;
  902. }
  903. set_buffer_uptodate(result);
  904. map_bh(result, inode->i_sb, 0);
  905. err = 0;
  906. out:
  907. btrfs_free_path(path);
  908. kunmap(result->b_page);
  909. return err;
  910. }
  911. #endif
  912. static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
  913. struct buffer_head *result, int create)
  914. {
  915. int ret;
  916. int err = 0;
  917. u64 blocknr;
  918. u64 extent_start = 0;
  919. u64 extent_end = 0;
  920. u64 objectid = inode->i_ino;
  921. u32 found_type;
  922. struct btrfs_path *path;
  923. struct btrfs_root *root = BTRFS_I(inode)->root;
  924. struct btrfs_file_extent_item *item;
  925. struct btrfs_leaf *leaf;
  926. struct btrfs_disk_key *found_key;
  927. path = btrfs_alloc_path();
  928. BUG_ON(!path);
  929. btrfs_init_path(path);
  930. if (create) {
  931. WARN_ON(1);
  932. }
  933. ret = btrfs_lookup_file_extent(NULL, root, path,
  934. inode->i_ino,
  935. iblock << inode->i_blkbits, 0);
  936. if (ret < 0) {
  937. err = ret;
  938. goto out;
  939. }
  940. if (ret != 0) {
  941. if (path->slots[0] == 0) {
  942. btrfs_release_path(root, path);
  943. goto out;
  944. }
  945. path->slots[0]--;
  946. }
  947. item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  948. struct btrfs_file_extent_item);
  949. leaf = btrfs_buffer_leaf(path->nodes[0]);
  950. blocknr = btrfs_file_extent_disk_blocknr(item);
  951. blocknr += btrfs_file_extent_offset(item);
  952. /* are we inside the extent that was found? */
  953. found_key = &leaf->items[path->slots[0]].key;
  954. found_type = btrfs_disk_key_type(found_key);
  955. if (btrfs_disk_key_objectid(found_key) != objectid ||
  956. found_type != BTRFS_EXTENT_DATA_KEY) {
  957. extent_end = 0;
  958. extent_start = 0;
  959. btrfs_release_path(root, path);
  960. goto out;
  961. }
  962. found_type = btrfs_file_extent_type(item);
  963. extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
  964. if (found_type == BTRFS_FILE_EXTENT_REG) {
  965. extent_start = extent_start >> inode->i_blkbits;
  966. extent_end = extent_start + btrfs_file_extent_num_blocks(item);
  967. if (iblock >= extent_start && iblock < extent_end) {
  968. err = 0;
  969. btrfs_map_bh_to_logical(root, result, blocknr +
  970. iblock - extent_start);
  971. goto out;
  972. }
  973. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  974. char *ptr;
  975. char *map;
  976. u32 size;
  977. size = btrfs_file_extent_inline_len(leaf->items +
  978. path->slots[0]);
  979. extent_end = (extent_start + size) >> inode->i_blkbits;
  980. extent_start >>= inode->i_blkbits;
  981. if (iblock < extent_start || iblock > extent_end) {
  982. goto out;
  983. }
  984. ptr = btrfs_file_extent_inline_start(item);
  985. map = kmap(result->b_page);
  986. memcpy(map, ptr, size);
  987. memset(map + size, 0, PAGE_CACHE_SIZE - size);
  988. flush_dcache_page(result->b_page);
  989. kunmap(result->b_page);
  990. set_buffer_uptodate(result);
  991. SetPageChecked(result->b_page);
  992. btrfs_map_bh_to_logical(root, result, 0);
  993. }
  994. out:
  995. btrfs_release_path(root, path);
  996. btrfs_free_path(path);
  997. return err;
  998. }
  999. static int btrfs_get_block(struct inode *inode, sector_t iblock,
  1000. struct buffer_head *result, int create)
  1001. {
  1002. int err;
  1003. struct btrfs_root *root = BTRFS_I(inode)->root;
  1004. mutex_lock(&root->fs_info->fs_mutex);
  1005. err = btrfs_get_block_lock(inode, iblock, result, create);
  1006. mutex_unlock(&root->fs_info->fs_mutex);
  1007. return err;
  1008. }
  1009. static int btrfs_prepare_write(struct file *file, struct page *page,
  1010. unsigned from, unsigned to)
  1011. {
  1012. return nobh_prepare_write(page, from, to, btrfs_get_block);
  1013. }
  1014. static void btrfs_write_super(struct super_block *sb)
  1015. {
  1016. btrfs_sync_fs(sb, 1);
  1017. }
  1018. static int btrfs_readpage(struct file *file, struct page *page)
  1019. {
  1020. return mpage_readpage(page, btrfs_get_block);
  1021. }
  1022. /*
  1023. * While block_write_full_page is writing back the dirty buffers under
  1024. * the page lock, whoever dirtied the buffers may decide to clean them
  1025. * again at any time. We handle that by only looking at the buffer
  1026. * state inside lock_buffer().
  1027. *
  1028. * If block_write_full_page() is called for regular writeback
  1029. * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
  1030. * locked buffer. This only can happen if someone has written the buffer
  1031. * directly, with submit_bh(). At the address_space level PageWriteback
  1032. * prevents this contention from occurring.
  1033. */
  1034. static int __btrfs_write_full_page(struct inode *inode, struct page *page,
  1035. struct writeback_control *wbc)
  1036. {
  1037. int err;
  1038. sector_t block;
  1039. sector_t last_block;
  1040. struct buffer_head *bh, *head;
  1041. const unsigned blocksize = 1 << inode->i_blkbits;
  1042. int nr_underway = 0;
  1043. BUG_ON(!PageLocked(page));
  1044. last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
  1045. if (!page_has_buffers(page)) {
  1046. create_empty_buffers(page, blocksize,
  1047. (1 << BH_Dirty)|(1 << BH_Uptodate));
  1048. }
  1049. /*
  1050. * Be very careful. We have no exclusion from __set_page_dirty_buffers
  1051. * here, and the (potentially unmapped) buffers may become dirty at
  1052. * any time. If a buffer becomes dirty here after we've inspected it
  1053. * then we just miss that fact, and the page stays dirty.
  1054. *
  1055. * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
  1056. * handle that here by just cleaning them.
  1057. */
  1058. block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
  1059. head = page_buffers(page);
  1060. bh = head;
  1061. /*
  1062. * Get all the dirty buffers mapped to disk addresses and
  1063. * handle any aliases from the underlying blockdev's mapping.
  1064. */
  1065. do {
  1066. if (block > last_block) {
  1067. /*
  1068. * mapped buffers outside i_size will occur, because
  1069. * this page can be outside i_size when there is a
  1070. * truncate in progress.
  1071. */
  1072. /*
  1073. * The buffer was zeroed by block_write_full_page()
  1074. */
  1075. clear_buffer_dirty(bh);
  1076. set_buffer_uptodate(bh);
  1077. } else if (!buffer_mapped(bh) && buffer_dirty(bh)) {
  1078. WARN_ON(bh->b_size != blocksize);
  1079. err = btrfs_get_block(inode, block, bh, 0);
  1080. if (err)
  1081. goto recover;
  1082. if (buffer_new(bh)) {
  1083. /* blockdev mappings never come here */
  1084. clear_buffer_new(bh);
  1085. unmap_underlying_metadata(bh->b_bdev,
  1086. bh->b_blocknr);
  1087. }
  1088. }
  1089. bh = bh->b_this_page;
  1090. block++;
  1091. } while (bh != head);
  1092. do {
  1093. if (!buffer_mapped(bh))
  1094. continue;
  1095. /*
  1096. * If it's a fully non-blocking write attempt and we cannot
  1097. * lock the buffer then redirty the page. Note that this can
  1098. * potentially cause a busy-wait loop from pdflush and kswapd
  1099. * activity, but those code paths have their own higher-level
  1100. * throttling.
  1101. */
  1102. if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) {
  1103. lock_buffer(bh);
  1104. } else if (test_set_buffer_locked(bh)) {
  1105. redirty_page_for_writepage(wbc, page);
  1106. continue;
  1107. }
  1108. if (test_clear_buffer_dirty(bh) && bh->b_blocknr != 0) {
  1109. mark_buffer_async_write(bh);
  1110. } else {
  1111. unlock_buffer(bh);
  1112. }
  1113. } while ((bh = bh->b_this_page) != head);
  1114. /*
  1115. * The page and its buffers are protected by PageWriteback(), so we can
  1116. * drop the bh refcounts early.
  1117. */
  1118. BUG_ON(PageWriteback(page));
  1119. set_page_writeback(page);
  1120. do {
  1121. struct buffer_head *next = bh->b_this_page;
  1122. if (buffer_async_write(bh)) {
  1123. submit_bh(WRITE, bh);
  1124. nr_underway++;
  1125. }
  1126. bh = next;
  1127. } while (bh != head);
  1128. unlock_page(page);
  1129. err = 0;
  1130. done:
  1131. if (nr_underway == 0) {
  1132. /*
  1133. * The page was marked dirty, but the buffers were
  1134. * clean. Someone wrote them back by hand with
  1135. * ll_rw_block/submit_bh. A rare case.
  1136. */
  1137. int uptodate = 1;
  1138. do {
  1139. if (!buffer_uptodate(bh)) {
  1140. uptodate = 0;
  1141. break;
  1142. }
  1143. bh = bh->b_this_page;
  1144. } while (bh != head);
  1145. if (uptodate)
  1146. SetPageUptodate(page);
  1147. end_page_writeback(page);
  1148. /*
  1149. * The page and buffer_heads can be released at any time from
  1150. * here on.
  1151. */
  1152. wbc->pages_skipped++; /* We didn't write this page */
  1153. }
  1154. return err;
  1155. recover:
  1156. /*
  1157. * ENOSPC, or some other error. We may already have added some
  1158. * blocks to the file, so we need to write these out to avoid
  1159. * exposing stale data.
  1160. * The page is currently locked and not marked for writeback
  1161. */
  1162. bh = head;
  1163. /* Recovery: lock and submit the mapped buffers */
  1164. do {
  1165. if (buffer_mapped(bh) && buffer_dirty(bh)) {
  1166. lock_buffer(bh);
  1167. mark_buffer_async_write(bh);
  1168. } else {
  1169. /*
  1170. * The buffer may have been set dirty during
  1171. * attachment to a dirty page.
  1172. */
  1173. clear_buffer_dirty(bh);
  1174. }
  1175. } while ((bh = bh->b_this_page) != head);
  1176. SetPageError(page);
  1177. BUG_ON(PageWriteback(page));
  1178. set_page_writeback(page);
  1179. do {
  1180. struct buffer_head *next = bh->b_this_page;
  1181. if (buffer_async_write(bh)) {
  1182. clear_buffer_dirty(bh);
  1183. submit_bh(WRITE, bh);
  1184. nr_underway++;
  1185. }
  1186. bh = next;
  1187. } while (bh != head);
  1188. unlock_page(page);
  1189. goto done;
  1190. }
  1191. /*
  1192. * The generic ->writepage function for buffer-backed address_spaces
  1193. */
  1194. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  1195. {
  1196. struct inode * const inode = page->mapping->host;
  1197. loff_t i_size = i_size_read(inode);
  1198. const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
  1199. unsigned offset;
  1200. void *kaddr;
  1201. /* Is the page fully inside i_size? */
  1202. if (page->index < end_index)
  1203. return __btrfs_write_full_page(inode, page, wbc);
  1204. /* Is the page fully outside i_size? (truncate in progress) */
  1205. offset = i_size & (PAGE_CACHE_SIZE-1);
  1206. if (page->index >= end_index+1 || !offset) {
  1207. /*
  1208. * The page may have dirty, unmapped buffers. For example,
  1209. * they may have been added in ext3_writepage(). Make them
  1210. * freeable here, so the page does not leak.
  1211. */
  1212. block_invalidatepage(page, 0);
  1213. unlock_page(page);
  1214. return 0; /* don't care */
  1215. }
  1216. /*
  1217. * The page straddles i_size. It must be zeroed out on each and every
  1218. * writepage invokation because it may be mmapped. "A file is mapped
  1219. * in multiples of the page size. For a file that is not a multiple of
  1220. * the page size, the remaining memory is zeroed when mapped, and
  1221. * writes to that region are not written out to the file."
  1222. */
  1223. kaddr = kmap_atomic(page, KM_USER0);
  1224. memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
  1225. flush_dcache_page(page);
  1226. kunmap_atomic(kaddr, KM_USER0);
  1227. return __btrfs_write_full_page(inode, page, wbc);
  1228. }
  1229. static void btrfs_truncate(struct inode *inode)
  1230. {
  1231. struct btrfs_root *root = BTRFS_I(inode)->root;
  1232. int ret;
  1233. struct btrfs_trans_handle *trans;
  1234. if (!S_ISREG(inode->i_mode))
  1235. return;
  1236. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1237. return;
  1238. nobh_truncate_page(inode->i_mapping, inode->i_size);
  1239. /* FIXME, add redo link to tree so we don't leak on crash */
  1240. mutex_lock(&root->fs_info->fs_mutex);
  1241. trans = btrfs_start_transaction(root, 1);
  1242. ret = btrfs_truncate_in_trans(trans, root, inode);
  1243. BUG_ON(ret);
  1244. ret = btrfs_end_transaction(trans, root);
  1245. BUG_ON(ret);
  1246. mutex_unlock(&root->fs_info->fs_mutex);
  1247. mark_inode_dirty(inode);
  1248. }
  1249. /*
  1250. * Make sure any changes to nobh_commit_write() are reflected in
  1251. * nobh_truncate_page(), since it doesn't call commit_write().
  1252. */
  1253. static int btrfs_commit_write(struct file *file, struct page *page,
  1254. unsigned from, unsigned to)
  1255. {
  1256. struct inode *inode = page->mapping->host;
  1257. struct buffer_head *bh;
  1258. loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
  1259. SetPageUptodate(page);
  1260. bh = page_buffers(page);
  1261. if (buffer_mapped(bh) && bh->b_blocknr != 0) {
  1262. set_page_dirty(page);
  1263. }
  1264. if (pos > inode->i_size) {
  1265. i_size_write(inode, pos);
  1266. mark_inode_dirty(inode);
  1267. }
  1268. return 0;
  1269. }
  1270. static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
  1271. struct page **prepared_pages,
  1272. const char __user * buf)
  1273. {
  1274. long page_fault = 0;
  1275. int i;
  1276. int offset = pos & (PAGE_CACHE_SIZE - 1);
  1277. for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
  1278. size_t count = min_t(size_t,
  1279. PAGE_CACHE_SIZE - offset, write_bytes);
  1280. struct page *page = prepared_pages[i];
  1281. fault_in_pages_readable(buf, count);
  1282. /* Copy data from userspace to the current page */
  1283. kmap(page);
  1284. page_fault = __copy_from_user(page_address(page) + offset,
  1285. buf, count);
  1286. /* Flush processor's dcache for this page */
  1287. flush_dcache_page(page);
  1288. kunmap(page);
  1289. buf += count;
  1290. write_bytes -= count;
  1291. if (page_fault)
  1292. break;
  1293. }
  1294. return page_fault ? -EFAULT : 0;
  1295. }
  1296. static void btrfs_drop_pages(struct page **pages, size_t num_pages)
  1297. {
  1298. size_t i;
  1299. for (i = 0; i < num_pages; i++) {
  1300. if (!pages[i])
  1301. break;
  1302. unlock_page(pages[i]);
  1303. mark_page_accessed(pages[i]);
  1304. page_cache_release(pages[i]);
  1305. }
  1306. }
  1307. static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
  1308. struct btrfs_root *root,
  1309. struct file *file,
  1310. struct page **pages,
  1311. size_t num_pages,
  1312. loff_t pos,
  1313. size_t write_bytes)
  1314. {
  1315. int i;
  1316. int offset;
  1317. int err = 0;
  1318. int ret;
  1319. int this_write;
  1320. struct inode *inode = file->f_path.dentry->d_inode;
  1321. struct buffer_head *bh;
  1322. struct btrfs_file_extent_item *ei;
  1323. for (i = 0; i < num_pages; i++) {
  1324. offset = pos & (PAGE_CACHE_SIZE -1);
  1325. this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
  1326. /* FIXME, one block at a time */
  1327. mutex_lock(&root->fs_info->fs_mutex);
  1328. trans = btrfs_start_transaction(root, 1);
  1329. bh = page_buffers(pages[i]);
  1330. if (buffer_mapped(bh) && bh->b_blocknr == 0) {
  1331. struct btrfs_key key;
  1332. struct btrfs_path *path;
  1333. char *ptr;
  1334. u32 datasize;
  1335. path = btrfs_alloc_path();
  1336. BUG_ON(!path);
  1337. key.objectid = inode->i_ino;
  1338. key.offset = pages[i]->index << PAGE_CACHE_SHIFT;
  1339. key.flags = 0;
  1340. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  1341. BUG_ON(write_bytes >= PAGE_CACHE_SIZE);
  1342. datasize = offset +
  1343. btrfs_file_extent_calc_inline_size(write_bytes);
  1344. ret = btrfs_insert_empty_item(trans, root, path, &key,
  1345. datasize);
  1346. BUG_ON(ret);
  1347. ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  1348. path->slots[0], struct btrfs_file_extent_item);
  1349. btrfs_set_file_extent_generation(ei, trans->transid);
  1350. btrfs_set_file_extent_type(ei,
  1351. BTRFS_FILE_EXTENT_INLINE);
  1352. ptr = btrfs_file_extent_inline_start(ei);
  1353. memcpy(ptr, bh->b_data, offset + write_bytes);
  1354. mark_buffer_dirty(path->nodes[0]);
  1355. btrfs_free_path(path);
  1356. } else {
  1357. btrfs_csum_file_block(trans, root, inode->i_ino,
  1358. pages[i]->index << PAGE_CACHE_SHIFT,
  1359. kmap(pages[i]), PAGE_CACHE_SIZE);
  1360. kunmap(pages[i]);
  1361. }
  1362. SetPageChecked(pages[i]);
  1363. ret = btrfs_end_transaction(trans, root);
  1364. BUG_ON(ret);
  1365. mutex_unlock(&root->fs_info->fs_mutex);
  1366. ret = btrfs_commit_write(file, pages[i], offset,
  1367. offset + this_write);
  1368. pos += this_write;
  1369. if (ret) {
  1370. err = ret;
  1371. goto failed;
  1372. }
  1373. WARN_ON(this_write > write_bytes);
  1374. write_bytes -= this_write;
  1375. }
  1376. failed:
  1377. return err;
  1378. }
  1379. static int drop_extents(struct btrfs_trans_handle *trans,
  1380. struct btrfs_root *root,
  1381. struct inode *inode,
  1382. u64 start, u64 end)
  1383. {
  1384. int ret;
  1385. struct btrfs_key key;
  1386. struct btrfs_leaf *leaf;
  1387. int slot;
  1388. struct btrfs_file_extent_item *extent;
  1389. u64 extent_end = 0;
  1390. int keep;
  1391. struct btrfs_file_extent_item old;
  1392. struct btrfs_path *path;
  1393. u64 search_start = start;
  1394. int bookend;
  1395. int found_type;
  1396. int found_extent;
  1397. int found_inline;
  1398. path = btrfs_alloc_path();
  1399. if (!path)
  1400. return -ENOMEM;
  1401. while(1) {
  1402. btrfs_release_path(root, path);
  1403. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  1404. search_start, -1);
  1405. if (ret < 0)
  1406. goto out;
  1407. if (ret > 0) {
  1408. if (path->slots[0] == 0) {
  1409. ret = 0;
  1410. goto out;
  1411. }
  1412. path->slots[0]--;
  1413. }
  1414. keep = 0;
  1415. bookend = 0;
  1416. found_extent = 0;
  1417. found_inline = 0;
  1418. extent = NULL;
  1419. leaf = btrfs_buffer_leaf(path->nodes[0]);
  1420. slot = path->slots[0];
  1421. btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
  1422. if (key.offset >= end || key.objectid != inode->i_ino) {
  1423. ret = 0;
  1424. goto out;
  1425. }
  1426. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) {
  1427. ret = 0;
  1428. goto out;
  1429. }
  1430. extent = btrfs_item_ptr(leaf, slot,
  1431. struct btrfs_file_extent_item);
  1432. found_type = btrfs_file_extent_type(extent);
  1433. if (found_type == BTRFS_FILE_EXTENT_REG) {
  1434. extent_end = key.offset +
  1435. (btrfs_file_extent_num_blocks(extent) <<
  1436. inode->i_blkbits);
  1437. found_extent = 1;
  1438. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  1439. found_inline = 1;
  1440. extent_end = key.offset +
  1441. btrfs_file_extent_inline_len(leaf->items + slot);
  1442. }
  1443. if (!found_extent && !found_inline) {
  1444. ret = 0;
  1445. goto out;
  1446. }
  1447. if (search_start >= extent_end) {
  1448. ret = 0;
  1449. goto out;
  1450. }
  1451. search_start = extent_end;
  1452. if (end < extent_end && end >= key.offset) {
  1453. if (found_extent) {
  1454. memcpy(&old, extent, sizeof(old));
  1455. ret = btrfs_inc_extent_ref(trans, root,
  1456. btrfs_file_extent_disk_blocknr(&old),
  1457. btrfs_file_extent_disk_num_blocks(&old));
  1458. BUG_ON(ret);
  1459. }
  1460. WARN_ON(found_inline);
  1461. bookend = 1;
  1462. }
  1463. if (start > key.offset) {
  1464. u64 new_num;
  1465. u64 old_num;
  1466. /* truncate existing extent */
  1467. keep = 1;
  1468. WARN_ON(start & (root->blocksize - 1));
  1469. if (found_extent) {
  1470. new_num = (start - key.offset) >>
  1471. inode->i_blkbits;
  1472. old_num = btrfs_file_extent_num_blocks(extent);
  1473. inode->i_blocks -= (old_num - new_num) << 3;
  1474. btrfs_set_file_extent_num_blocks(extent,
  1475. new_num);
  1476. mark_buffer_dirty(path->nodes[0]);
  1477. } else {
  1478. WARN_ON(1);
  1479. /*
  1480. ret = btrfs_truncate_item(trans, root, path,
  1481. start - key.offset);
  1482. BUG_ON(ret);
  1483. */
  1484. }
  1485. }
  1486. if (!keep) {
  1487. u64 disk_blocknr = 0;
  1488. u64 disk_num_blocks = 0;
  1489. u64 extent_num_blocks = 0;
  1490. if (found_extent) {
  1491. disk_blocknr =
  1492. btrfs_file_extent_disk_blocknr(extent);
  1493. disk_num_blocks =
  1494. btrfs_file_extent_disk_num_blocks(extent);
  1495. extent_num_blocks =
  1496. btrfs_file_extent_num_blocks(extent);
  1497. }
  1498. ret = btrfs_del_item(trans, root, path);
  1499. BUG_ON(ret);
  1500. btrfs_release_path(root, path);
  1501. if (found_extent) {
  1502. inode->i_blocks -=
  1503. btrfs_file_extent_num_blocks(extent) << 3;
  1504. ret = btrfs_free_extent(trans, root,
  1505. disk_blocknr,
  1506. disk_num_blocks, 0);
  1507. }
  1508. BUG_ON(ret);
  1509. if (!bookend && search_start >= end) {
  1510. ret = 0;
  1511. goto out;
  1512. }
  1513. if (!bookend)
  1514. continue;
  1515. }
  1516. if (bookend && found_extent) {
  1517. /* create bookend */
  1518. struct btrfs_key ins;
  1519. ins.objectid = inode->i_ino;
  1520. ins.offset = end;
  1521. ins.flags = 0;
  1522. btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
  1523. btrfs_release_path(root, path);
  1524. ret = btrfs_insert_empty_item(trans, root, path, &ins,
  1525. sizeof(*extent));
  1526. BUG_ON(ret);
  1527. extent = btrfs_item_ptr(
  1528. btrfs_buffer_leaf(path->nodes[0]),
  1529. path->slots[0],
  1530. struct btrfs_file_extent_item);
  1531. btrfs_set_file_extent_disk_blocknr(extent,
  1532. btrfs_file_extent_disk_blocknr(&old));
  1533. btrfs_set_file_extent_disk_num_blocks(extent,
  1534. btrfs_file_extent_disk_num_blocks(&old));
  1535. btrfs_set_file_extent_offset(extent,
  1536. btrfs_file_extent_offset(&old) +
  1537. ((end - key.offset) >> inode->i_blkbits));
  1538. WARN_ON(btrfs_file_extent_num_blocks(&old) <
  1539. (end - key.offset) >> inode->i_blkbits);
  1540. btrfs_set_file_extent_num_blocks(extent,
  1541. btrfs_file_extent_num_blocks(&old) -
  1542. ((end - key.offset) >> inode->i_blkbits));
  1543. btrfs_set_file_extent_type(extent,
  1544. BTRFS_FILE_EXTENT_REG);
  1545. btrfs_set_file_extent_generation(extent,
  1546. btrfs_file_extent_generation(&old));
  1547. btrfs_mark_buffer_dirty(path->nodes[0]);
  1548. inode->i_blocks +=
  1549. btrfs_file_extent_num_blocks(extent) << 3;
  1550. ret = 0;
  1551. goto out;
  1552. }
  1553. }
  1554. out:
  1555. btrfs_free_path(path);
  1556. return ret;
  1557. }
  1558. static int prepare_pages(struct btrfs_root *root,
  1559. struct file *file,
  1560. struct page **pages,
  1561. size_t num_pages,
  1562. loff_t pos,
  1563. unsigned long first_index,
  1564. unsigned long last_index,
  1565. size_t write_bytes,
  1566. u64 alloc_extent_start)
  1567. {
  1568. int i;
  1569. unsigned long index = pos >> PAGE_CACHE_SHIFT;
  1570. struct inode *inode = file->f_path.dentry->d_inode;
  1571. int offset;
  1572. int err = 0;
  1573. int this_write;
  1574. struct buffer_head *bh;
  1575. struct buffer_head *head;
  1576. loff_t isize = i_size_read(inode);
  1577. memset(pages, 0, num_pages * sizeof(struct page *));
  1578. for (i = 0; i < num_pages; i++) {
  1579. pages[i] = grab_cache_page(inode->i_mapping, index + i);
  1580. if (!pages[i]) {
  1581. err = -ENOMEM;
  1582. goto failed_release;
  1583. }
  1584. offset = pos & (PAGE_CACHE_SIZE -1);
  1585. this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
  1586. create_empty_buffers(pages[i], root->fs_info->sb->s_blocksize,
  1587. (1 << BH_Uptodate));
  1588. head = page_buffers(pages[i]);
  1589. bh = head;
  1590. do {
  1591. err = btrfs_map_bh_to_logical(root, bh,
  1592. alloc_extent_start);
  1593. BUG_ON(err);
  1594. if (err)
  1595. goto failed_truncate;
  1596. bh = bh->b_this_page;
  1597. if (alloc_extent_start)
  1598. alloc_extent_start++;
  1599. } while (bh != head);
  1600. pos += this_write;
  1601. WARN_ON(this_write > write_bytes);
  1602. write_bytes -= this_write;
  1603. }
  1604. return 0;
  1605. failed_release:
  1606. btrfs_drop_pages(pages, num_pages);
  1607. return err;
  1608. failed_truncate:
  1609. btrfs_drop_pages(pages, num_pages);
  1610. if (pos > isize)
  1611. vmtruncate(inode, isize);
  1612. return err;
  1613. }
  1614. static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
  1615. size_t count, loff_t *ppos)
  1616. {
  1617. loff_t pos;
  1618. size_t num_written = 0;
  1619. int err = 0;
  1620. int ret = 0;
  1621. struct inode *inode = file->f_path.dentry->d_inode;
  1622. struct btrfs_root *root = BTRFS_I(inode)->root;
  1623. struct page *pages[8];
  1624. struct page *pinned[2] = { NULL, NULL };
  1625. unsigned long first_index;
  1626. unsigned long last_index;
  1627. u64 start_pos;
  1628. u64 num_blocks;
  1629. u64 alloc_extent_start;
  1630. struct btrfs_trans_handle *trans;
  1631. struct btrfs_key ins;
  1632. if (file->f_flags & O_DIRECT)
  1633. return -EINVAL;
  1634. pos = *ppos;
  1635. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1636. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  1637. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  1638. if (err)
  1639. goto out;
  1640. if (count == 0)
  1641. goto out;
  1642. err = remove_suid(file->f_path.dentry);
  1643. if (err)
  1644. goto out;
  1645. file_update_time(file);
  1646. start_pos = pos & ~((u64)PAGE_CACHE_SIZE - 1);
  1647. num_blocks = (count + pos - start_pos + root->blocksize - 1) >>
  1648. inode->i_blkbits;
  1649. mutex_lock(&inode->i_mutex);
  1650. first_index = pos >> PAGE_CACHE_SHIFT;
  1651. last_index = (pos + count) >> PAGE_CACHE_SHIFT;
  1652. if ((first_index << PAGE_CACHE_SHIFT) < inode->i_size &&
  1653. (pos & (PAGE_CACHE_SIZE - 1))) {
  1654. pinned[0] = grab_cache_page(inode->i_mapping, first_index);
  1655. if (!PageUptodate(pinned[0])) {
  1656. ret = mpage_readpage(pinned[0], btrfs_get_block);
  1657. BUG_ON(ret);
  1658. } else {
  1659. unlock_page(pinned[0]);
  1660. }
  1661. }
  1662. if (first_index != last_index &&
  1663. (last_index << PAGE_CACHE_SHIFT) < inode->i_size &&
  1664. (count & (PAGE_CACHE_SIZE - 1))) {
  1665. pinned[1] = grab_cache_page(inode->i_mapping, last_index);
  1666. if (!PageUptodate(pinned[1])) {
  1667. ret = mpage_readpage(pinned[1], btrfs_get_block);
  1668. BUG_ON(ret);
  1669. } else {
  1670. unlock_page(pinned[1]);
  1671. }
  1672. }
  1673. mutex_lock(&root->fs_info->fs_mutex);
  1674. trans = btrfs_start_transaction(root, 1);
  1675. if (!trans) {
  1676. err = -ENOMEM;
  1677. mutex_unlock(&root->fs_info->fs_mutex);
  1678. goto out_unlock;
  1679. }
  1680. /* FIXME blocksize != 4096 */
  1681. inode->i_blocks += num_blocks << 3;
  1682. if (start_pos < inode->i_size) {
  1683. /* FIXME blocksize != pagesize */
  1684. ret = drop_extents(trans, root, inode,
  1685. start_pos,
  1686. (pos + count + root->blocksize -1) &
  1687. ~((u64)root->blocksize - 1));
  1688. BUG_ON(ret);
  1689. }
  1690. if (inode->i_size >= PAGE_CACHE_SIZE || pos + count < inode->i_size ||
  1691. pos + count - start_pos > BTRFS_MAX_INLINE_DATA_SIZE(root)) {
  1692. ret = btrfs_alloc_extent(trans, root, num_blocks, 1,
  1693. (u64)-1, &ins);
  1694. BUG_ON(ret);
  1695. ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
  1696. start_pos, ins.objectid, ins.offset);
  1697. BUG_ON(ret);
  1698. } else {
  1699. ins.offset = 0;
  1700. ins.objectid = 0;
  1701. }
  1702. BUG_ON(ret);
  1703. alloc_extent_start = ins.objectid;
  1704. ret = btrfs_end_transaction(trans, root);
  1705. mutex_unlock(&root->fs_info->fs_mutex);
  1706. while(count > 0) {
  1707. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  1708. size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
  1709. size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
  1710. PAGE_CACHE_SHIFT;
  1711. memset(pages, 0, sizeof(pages));
  1712. ret = prepare_pages(root, file, pages, num_pages,
  1713. pos, first_index, last_index,
  1714. write_bytes, alloc_extent_start);
  1715. BUG_ON(ret);
  1716. /* FIXME blocks != pagesize */
  1717. if (alloc_extent_start)
  1718. alloc_extent_start += num_pages;
  1719. ret = btrfs_copy_from_user(pos, num_pages,
  1720. write_bytes, pages, buf);
  1721. BUG_ON(ret);
  1722. ret = dirty_and_release_pages(NULL, root, file, pages,
  1723. num_pages, pos, write_bytes);
  1724. BUG_ON(ret);
  1725. btrfs_drop_pages(pages, num_pages);
  1726. buf += write_bytes;
  1727. count -= write_bytes;
  1728. pos += write_bytes;
  1729. num_written += write_bytes;
  1730. balance_dirty_pages_ratelimited(inode->i_mapping);
  1731. cond_resched();
  1732. }
  1733. out_unlock:
  1734. mutex_unlock(&inode->i_mutex);
  1735. out:
  1736. if (pinned[0])
  1737. page_cache_release(pinned[0]);
  1738. if (pinned[1])
  1739. page_cache_release(pinned[1]);
  1740. *ppos = pos;
  1741. current->backing_dev_info = NULL;
  1742. mark_inode_dirty(inode);
  1743. return num_written ? num_written : err;
  1744. }
  1745. #if 0
  1746. static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode,
  1747. struct page *page, loff_t pos,
  1748. size_t offset, size_t write_bytes)
  1749. {
  1750. struct btrfs_path *path;
  1751. struct btrfs_trans_handle *trans;
  1752. struct btrfs_key key;
  1753. struct btrfs_leaf *leaf;
  1754. struct btrfs_key found_key;
  1755. int ret;
  1756. size_t copy_size = 0;
  1757. char *dst = NULL;
  1758. int err = 0;
  1759. size_t num_written = 0;
  1760. path = btrfs_alloc_path();
  1761. BUG_ON(!path);
  1762. mutex_lock(&root->fs_info->fs_mutex);
  1763. trans = btrfs_start_transaction(root, 1);
  1764. key.objectid = inode->i_ino;
  1765. key.flags = 0;
  1766. btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
  1767. again:
  1768. key.offset = pos;
  1769. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1770. if (ret < 0) {
  1771. err = ret;
  1772. goto out;
  1773. }
  1774. if (ret == 0) {
  1775. leaf = btrfs_buffer_leaf(path->nodes[0]);
  1776. btrfs_disk_key_to_cpu(&found_key,
  1777. &leaf->items[path->slots[0]].key);
  1778. copy_size = btrfs_item_size(leaf->items + path->slots[0]);
  1779. dst = btrfs_item_ptr(leaf, path->slots[0], char);
  1780. copy_size = min(write_bytes, copy_size);
  1781. goto copyit;
  1782. } else {
  1783. int slot = path->slots[0];
  1784. if (slot > 0) {
  1785. slot--;
  1786. }
  1787. // FIXME find max key
  1788. leaf = btrfs_buffer_leaf(path->nodes[0]);
  1789. btrfs_disk_key_to_cpu(&found_key,
  1790. &leaf->items[slot].key);
  1791. if (found_key.objectid != inode->i_ino)
  1792. goto insert;
  1793. if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY)
  1794. goto insert;
  1795. copy_size = btrfs_item_size(leaf->items + slot);
  1796. if (found_key.offset + copy_size <= pos)
  1797. goto insert;
  1798. dst = btrfs_item_ptr(leaf, path->slots[0], char);
  1799. dst += pos - found_key.offset;
  1800. copy_size = copy_size - (pos - found_key.offset);
  1801. BUG_ON(copy_size < 0);
  1802. copy_size = min(write_bytes, copy_size);
  1803. WARN_ON(copy_size == 0);
  1804. goto copyit;
  1805. }
  1806. insert:
  1807. btrfs_release_path(root, path);
  1808. copy_size = min(write_bytes,
  1809. (size_t)BTRFS_LEAF_DATA_SIZE(root) -
  1810. sizeof(struct btrfs_item) * 4);
  1811. ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size);
  1812. BUG_ON(ret);
  1813. dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  1814. path->slots[0], char);
  1815. copyit:
  1816. WARN_ON(copy_size == 0);
  1817. WARN_ON(dst + copy_size >
  1818. btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  1819. path->slots[0], char) +
  1820. btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items +
  1821. path->slots[0]));
  1822. btrfs_memcpy(root, path->nodes[0]->b_data, dst,
  1823. page_address(page) + offset, copy_size);
  1824. mark_buffer_dirty(path->nodes[0]);
  1825. btrfs_release_path(root, path);
  1826. pos += copy_size;
  1827. offset += copy_size;
  1828. num_written += copy_size;
  1829. write_bytes -= copy_size;
  1830. if (write_bytes)
  1831. goto again;
  1832. out:
  1833. btrfs_free_path(path);
  1834. ret = btrfs_end_transaction(trans, root);
  1835. BUG_ON(ret);
  1836. mutex_unlock(&root->fs_info->fs_mutex);
  1837. return num_written ? num_written : err;
  1838. }
  1839. static ssize_t btrfs_file_inline_write(struct file *file,
  1840. const char __user *buf,
  1841. size_t count, loff_t *ppos)
  1842. {
  1843. loff_t pos;
  1844. size_t num_written = 0;
  1845. int err = 0;
  1846. int ret = 0;
  1847. struct inode *inode = file->f_path.dentry->d_inode;
  1848. struct btrfs_root *root = BTRFS_I(inode)->root;
  1849. unsigned long page_index;
  1850. if (file->f_flags & O_DIRECT)
  1851. return -EINVAL;
  1852. pos = *ppos;
  1853. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1854. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  1855. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  1856. if (err)
  1857. goto out;
  1858. if (count == 0)
  1859. goto out;
  1860. err = remove_suid(file->f_path.dentry);
  1861. if (err)
  1862. goto out;
  1863. file_update_time(file);
  1864. mutex_lock(&inode->i_mutex);
  1865. while(count > 0) {
  1866. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  1867. size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
  1868. struct page *page;
  1869. page_index = pos >> PAGE_CACHE_SHIFT;
  1870. page = grab_cache_page(inode->i_mapping, page_index);
  1871. if (!PageUptodate(page)) {
  1872. ret = mpage_readpage(page, btrfs_get_block);
  1873. BUG_ON(ret);
  1874. lock_page(page);
  1875. }
  1876. ret = btrfs_copy_from_user(pos, 1,
  1877. write_bytes, &page, buf);
  1878. BUG_ON(ret);
  1879. write_bytes = inline_one_page(root, inode, page, pos,
  1880. offset, write_bytes);
  1881. SetPageUptodate(page);
  1882. if (write_bytes > 0 && pos + write_bytes > inode->i_size) {
  1883. i_size_write(inode, pos + write_bytes);
  1884. mark_inode_dirty(inode);
  1885. }
  1886. page_cache_release(page);
  1887. unlock_page(page);
  1888. if (write_bytes < 0)
  1889. goto out_unlock;
  1890. buf += write_bytes;
  1891. count -= write_bytes;
  1892. pos += write_bytes;
  1893. num_written += write_bytes;
  1894. balance_dirty_pages_ratelimited(inode->i_mapping);
  1895. cond_resched();
  1896. }
  1897. out_unlock:
  1898. mutex_unlock(&inode->i_mutex);
  1899. out:
  1900. *ppos = pos;
  1901. current->backing_dev_info = NULL;
  1902. return num_written ? num_written : err;
  1903. }
  1904. #endif
  1905. static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
  1906. unsigned long offset, unsigned long size)
  1907. {
  1908. char *kaddr;
  1909. unsigned long left, count = desc->count;
  1910. struct inode *inode = page->mapping->host;
  1911. if (size > count)
  1912. size = count;
  1913. if (!PageChecked(page)) {
  1914. /* FIXME, do it per block */
  1915. struct btrfs_root *root = BTRFS_I(inode)->root;
  1916. int ret = btrfs_csum_verify_file_block(root,
  1917. page->mapping->host->i_ino,
  1918. page->index << PAGE_CACHE_SHIFT,
  1919. kmap(page), PAGE_CACHE_SIZE);
  1920. if (ret) {
  1921. printk("failed to verify ino %lu page %lu\n",
  1922. page->mapping->host->i_ino,
  1923. page->index);
  1924. memset(page_address(page), 0, PAGE_CACHE_SIZE);
  1925. }
  1926. SetPageChecked(page);
  1927. kunmap(page);
  1928. }
  1929. /*
  1930. * Faults on the destination of a read are common, so do it before
  1931. * taking the kmap.
  1932. */
  1933. if (!fault_in_pages_writeable(desc->arg.buf, size)) {
  1934. kaddr = kmap_atomic(page, KM_USER0);
  1935. left = __copy_to_user_inatomic(desc->arg.buf,
  1936. kaddr + offset, size);
  1937. kunmap_atomic(kaddr, KM_USER0);
  1938. if (left == 0)
  1939. goto success;
  1940. }
  1941. /* Do it the slow way */
  1942. kaddr = kmap(page);
  1943. left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
  1944. kunmap(page);
  1945. if (left) {
  1946. size -= left;
  1947. desc->error = -EFAULT;
  1948. }
  1949. success:
  1950. desc->count = count - size;
  1951. desc->written += size;
  1952. desc->arg.buf += size;
  1953. return size;
  1954. }
  1955. /**
  1956. * btrfs_file_aio_read - filesystem read routine
  1957. * @iocb: kernel I/O control block
  1958. * @iov: io vector request
  1959. * @nr_segs: number of segments in the iovec
  1960. * @pos: current file position
  1961. */
  1962. static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
  1963. unsigned long nr_segs, loff_t pos)
  1964. {
  1965. struct file *filp = iocb->ki_filp;
  1966. ssize_t retval;
  1967. unsigned long seg;
  1968. size_t count;
  1969. loff_t *ppos = &iocb->ki_pos;
  1970. count = 0;
  1971. for (seg = 0; seg < nr_segs; seg++) {
  1972. const struct iovec *iv = &iov[seg];
  1973. /*
  1974. * If any segment has a negative length, or the cumulative
  1975. * length ever wraps negative then return -EINVAL.
  1976. */
  1977. count += iv->iov_len;
  1978. if (unlikely((ssize_t)(count|iv->iov_len) < 0))
  1979. return -EINVAL;
  1980. if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
  1981. continue;
  1982. if (seg == 0)
  1983. return -EFAULT;
  1984. nr_segs = seg;
  1985. count -= iv->iov_len; /* This segment is no good */
  1986. break;
  1987. }
  1988. retval = 0;
  1989. if (count) {
  1990. for (seg = 0; seg < nr_segs; seg++) {
  1991. read_descriptor_t desc;
  1992. desc.written = 0;
  1993. desc.arg.buf = iov[seg].iov_base;
  1994. desc.count = iov[seg].iov_len;
  1995. if (desc.count == 0)
  1996. continue;
  1997. desc.error = 0;
  1998. do_generic_file_read(filp, ppos, &desc,
  1999. btrfs_read_actor);
  2000. retval += desc.written;
  2001. if (desc.error) {
  2002. retval = retval ?: desc.error;
  2003. break;
  2004. }
  2005. }
  2006. }
  2007. return retval;
  2008. }
  2009. static int create_subvol(struct btrfs_root *root, char *name, int namelen)
  2010. {
  2011. struct btrfs_trans_handle *trans;
  2012. struct btrfs_key key;
  2013. struct btrfs_root_item root_item;
  2014. struct btrfs_inode_item *inode_item;
  2015. struct buffer_head *subvol;
  2016. struct btrfs_leaf *leaf;
  2017. struct btrfs_root *new_root;
  2018. struct inode *inode;
  2019. int ret;
  2020. u64 objectid;
  2021. u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
  2022. mutex_lock(&root->fs_info->fs_mutex);
  2023. trans = btrfs_start_transaction(root, 1);
  2024. BUG_ON(!trans);
  2025. subvol = btrfs_alloc_free_block(trans, root);
  2026. leaf = btrfs_buffer_leaf(subvol);
  2027. btrfs_set_header_nritems(&leaf->header, 0);
  2028. btrfs_set_header_level(&leaf->header, 0);
  2029. btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol));
  2030. btrfs_set_header_generation(&leaf->header, trans->transid);
  2031. memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
  2032. sizeof(leaf->header.fsid));
  2033. inode_item = &root_item.inode;
  2034. memset(inode_item, 0, sizeof(*inode_item));
  2035. btrfs_set_inode_generation(inode_item, 1);
  2036. btrfs_set_inode_size(inode_item, 3);
  2037. btrfs_set_inode_nlink(inode_item, 1);
  2038. btrfs_set_inode_nblocks(inode_item, 1);
  2039. btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
  2040. btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
  2041. btrfs_set_root_refs(&root_item, 1);
  2042. mark_buffer_dirty(subvol);
  2043. brelse(subvol);
  2044. subvol = NULL;
  2045. ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
  2046. 0, &objectid);
  2047. BUG_ON(ret);
  2048. btrfs_set_root_dirid(&root_item, new_dirid);
  2049. key.objectid = objectid;
  2050. key.offset = 1;
  2051. key.flags = 0;
  2052. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  2053. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  2054. &root_item);
  2055. BUG_ON(ret);
  2056. /*
  2057. * insert the directory item
  2058. */
  2059. key.offset = (u64)-1;
  2060. ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
  2061. name, namelen,
  2062. root->fs_info->sb->s_root->d_inode->i_ino,
  2063. &key, 0);
  2064. BUG_ON(ret);
  2065. ret = btrfs_commit_transaction(trans, root);
  2066. BUG_ON(ret);
  2067. new_root = btrfs_read_fs_root(root->fs_info, &key);
  2068. BUG_ON(!new_root);
  2069. trans = btrfs_start_transaction(new_root, 1);
  2070. BUG_ON(!trans);
  2071. inode = btrfs_new_inode(trans, new_root, new_dirid, S_IFDIR | 0700);
  2072. inode->i_op = &btrfs_dir_inode_operations;
  2073. inode->i_fop = &btrfs_dir_file_operations;
  2074. ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
  2075. BUG_ON(ret);
  2076. inode->i_nlink = 1;
  2077. inode->i_size = 6;
  2078. ret = btrfs_update_inode(trans, new_root, inode);
  2079. BUG_ON(ret);
  2080. ret = btrfs_commit_transaction(trans, new_root);
  2081. BUG_ON(ret);
  2082. iput(inode);
  2083. mutex_unlock(&root->fs_info->fs_mutex);
  2084. return 0;
  2085. }
  2086. static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
  2087. {
  2088. struct btrfs_trans_handle *trans;
  2089. struct btrfs_key key;
  2090. struct btrfs_root_item new_root_item;
  2091. int ret;
  2092. u64 objectid;
  2093. if (!root->ref_cows)
  2094. return -EINVAL;
  2095. mutex_lock(&root->fs_info->fs_mutex);
  2096. trans = btrfs_start_transaction(root, 1);
  2097. BUG_ON(!trans);
  2098. ret = btrfs_update_inode(trans, root, root->inode);
  2099. BUG_ON(ret);
  2100. ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
  2101. 0, &objectid);
  2102. BUG_ON(ret);
  2103. memcpy(&new_root_item, &root->root_item,
  2104. sizeof(new_root_item));
  2105. key.objectid = objectid;
  2106. key.offset = 1;
  2107. key.flags = 0;
  2108. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  2109. btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node));
  2110. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  2111. &new_root_item);
  2112. BUG_ON(ret);
  2113. /*
  2114. * insert the directory item
  2115. */
  2116. key.offset = (u64)-1;
  2117. ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
  2118. name, namelen,
  2119. root->fs_info->sb->s_root->d_inode->i_ino,
  2120. &key, 0);
  2121. BUG_ON(ret);
  2122. ret = btrfs_inc_root_ref(trans, root);
  2123. BUG_ON(ret);
  2124. ret = btrfs_commit_transaction(trans, root);
  2125. BUG_ON(ret);
  2126. mutex_unlock(&root->fs_info->fs_mutex);
  2127. return 0;
  2128. }
  2129. static int add_disk(struct btrfs_root *root, char *name, int namelen)
  2130. {
  2131. struct block_device *bdev;
  2132. struct btrfs_path *path;
  2133. struct super_block *sb = root->fs_info->sb;
  2134. struct btrfs_root *dev_root = root->fs_info->dev_root;
  2135. struct btrfs_trans_handle *trans;
  2136. struct btrfs_device_item *dev_item;
  2137. struct btrfs_key key;
  2138. u16 item_size;
  2139. u64 num_blocks;
  2140. u64 new_blocks;
  2141. u64 device_id;
  2142. int ret;
  2143. printk("adding disk %s\n", name);
  2144. path = btrfs_alloc_path();
  2145. if (!path)
  2146. return -ENOMEM;
  2147. num_blocks = btrfs_super_total_blocks(root->fs_info->disk_super);
  2148. bdev = open_bdev_excl(name, O_RDWR, sb);
  2149. if (IS_ERR(bdev)) {
  2150. ret = PTR_ERR(bdev);
  2151. printk("open bdev excl failed ret %d\n", ret);
  2152. goto out_nolock;
  2153. }
  2154. set_blocksize(bdev, sb->s_blocksize);
  2155. new_blocks = bdev->bd_inode->i_size >> sb->s_blocksize_bits;
  2156. key.objectid = num_blocks;
  2157. key.offset = new_blocks;
  2158. key.flags = 0;
  2159. btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY);
  2160. mutex_lock(&dev_root->fs_info->fs_mutex);
  2161. trans = btrfs_start_transaction(dev_root, 1);
  2162. item_size = sizeof(*dev_item) + namelen;
  2163. printk("insert empty on %Lu %Lu %u size %d\n", num_blocks, new_blocks, key.flags, item_size);
  2164. ret = btrfs_insert_empty_item(trans, dev_root, path, &key, item_size);
  2165. if (ret) {
  2166. printk("insert failed %d\n", ret);
  2167. close_bdev_excl(bdev);
  2168. if (ret > 0)
  2169. ret = -EEXIST;
  2170. goto out;
  2171. }
  2172. dev_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  2173. path->slots[0], struct btrfs_device_item);
  2174. btrfs_set_device_pathlen(dev_item, namelen);
  2175. memcpy(dev_item + 1, name, namelen);
  2176. device_id = btrfs_super_last_device_id(root->fs_info->disk_super) + 1;
  2177. btrfs_set_super_last_device_id(root->fs_info->disk_super, device_id);
  2178. btrfs_set_device_id(dev_item, device_id);
  2179. mark_buffer_dirty(path->nodes[0]);
  2180. ret = btrfs_insert_dev_radix(root, bdev, device_id, num_blocks,
  2181. new_blocks);
  2182. if (!ret) {
  2183. btrfs_set_super_total_blocks(root->fs_info->disk_super,
  2184. num_blocks + new_blocks);
  2185. i_size_write(root->fs_info->btree_inode,
  2186. (num_blocks + new_blocks) <<
  2187. root->fs_info->btree_inode->i_blkbits);
  2188. }
  2189. out:
  2190. ret = btrfs_commit_transaction(trans, dev_root);
  2191. BUG_ON(ret);
  2192. mutex_unlock(&root->fs_info->fs_mutex);
  2193. out_nolock:
  2194. btrfs_free_path(path);
  2195. return ret;
  2196. }
  2197. static int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
  2198. cmd, unsigned long arg)
  2199. {
  2200. struct btrfs_root *root = BTRFS_I(inode)->root;
  2201. struct btrfs_ioctl_vol_args vol_args;
  2202. int ret = 0;
  2203. int namelen;
  2204. struct btrfs_path *path;
  2205. u64 root_dirid;
  2206. switch (cmd) {
  2207. case BTRFS_IOC_SNAP_CREATE:
  2208. if (copy_from_user(&vol_args,
  2209. (struct btrfs_ioctl_vol_args __user *)arg,
  2210. sizeof(vol_args)))
  2211. return -EFAULT;
  2212. namelen = strlen(vol_args.name);
  2213. if (namelen > BTRFS_VOL_NAME_MAX)
  2214. return -EINVAL;
  2215. path = btrfs_alloc_path();
  2216. if (!path)
  2217. return -ENOMEM;
  2218. root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
  2219. mutex_lock(&root->fs_info->fs_mutex);
  2220. ret = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
  2221. path, root_dirid,
  2222. vol_args.name, namelen, 0);
  2223. mutex_unlock(&root->fs_info->fs_mutex);
  2224. btrfs_free_path(path);
  2225. if (ret == 0)
  2226. return -EEXIST;
  2227. if (root == root->fs_info->tree_root)
  2228. ret = create_subvol(root, vol_args.name, namelen);
  2229. else
  2230. ret = create_snapshot(root, vol_args.name, namelen);
  2231. WARN_ON(ret);
  2232. break;
  2233. case BTRFS_IOC_ADD_DISK:
  2234. if (copy_from_user(&vol_args,
  2235. (struct btrfs_ioctl_vol_args __user *)arg,
  2236. sizeof(vol_args)))
  2237. return -EFAULT;
  2238. namelen = strlen(vol_args.name);
  2239. if (namelen > BTRFS_VOL_NAME_MAX)
  2240. return -EINVAL;
  2241. vol_args.name[namelen] = '\0';
  2242. ret = add_disk(root, vol_args.name, namelen);
  2243. break;
  2244. default:
  2245. return -ENOTTY;
  2246. }
  2247. return ret;
  2248. }
  2249. static struct kmem_cache *btrfs_inode_cachep;
  2250. struct kmem_cache *btrfs_trans_handle_cachep;
  2251. struct kmem_cache *btrfs_transaction_cachep;
  2252. struct kmem_cache *btrfs_bit_radix_cachep;
  2253. struct kmem_cache *btrfs_path_cachep;
  2254. /*
  2255. * Called inside transaction, so use GFP_NOFS
  2256. */
  2257. static struct inode *btrfs_alloc_inode(struct super_block *sb)
  2258. {
  2259. struct btrfs_inode *ei;
  2260. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  2261. if (!ei)
  2262. return NULL;
  2263. return &ei->vfs_inode;
  2264. }
  2265. static void btrfs_destroy_inode(struct inode *inode)
  2266. {
  2267. WARN_ON(!list_empty(&inode->i_dentry));
  2268. WARN_ON(inode->i_data.nrpages);
  2269. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  2270. }
  2271. static void init_once(void * foo, struct kmem_cache * cachep,
  2272. unsigned long flags)
  2273. {
  2274. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  2275. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  2276. SLAB_CTOR_CONSTRUCTOR) {
  2277. inode_init_once(&ei->vfs_inode);
  2278. }
  2279. }
  2280. static int init_inodecache(void)
  2281. {
  2282. btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
  2283. sizeof(struct btrfs_inode),
  2284. 0, (SLAB_RECLAIM_ACCOUNT|
  2285. SLAB_MEM_SPREAD),
  2286. init_once, NULL);
  2287. btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
  2288. sizeof(struct btrfs_trans_handle),
  2289. 0, (SLAB_RECLAIM_ACCOUNT|
  2290. SLAB_MEM_SPREAD),
  2291. NULL, NULL);
  2292. btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
  2293. sizeof(struct btrfs_transaction),
  2294. 0, (SLAB_RECLAIM_ACCOUNT|
  2295. SLAB_MEM_SPREAD),
  2296. NULL, NULL);
  2297. btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
  2298. sizeof(struct btrfs_transaction),
  2299. 0, (SLAB_RECLAIM_ACCOUNT|
  2300. SLAB_MEM_SPREAD),
  2301. NULL, NULL);
  2302. btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
  2303. 256,
  2304. 0, (SLAB_RECLAIM_ACCOUNT|
  2305. SLAB_MEM_SPREAD |
  2306. SLAB_DESTROY_BY_RCU),
  2307. NULL, NULL);
  2308. if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
  2309. btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
  2310. return -ENOMEM;
  2311. return 0;
  2312. }
  2313. static void destroy_inodecache(void)
  2314. {
  2315. kmem_cache_destroy(btrfs_inode_cachep);
  2316. kmem_cache_destroy(btrfs_trans_handle_cachep);
  2317. kmem_cache_destroy(btrfs_transaction_cachep);
  2318. kmem_cache_destroy(btrfs_bit_radix_cachep);
  2319. kmem_cache_destroy(btrfs_path_cachep);
  2320. }
  2321. static int btrfs_get_sb(struct file_system_type *fs_type,
  2322. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  2323. {
  2324. return get_sb_bdev(fs_type, flags, dev_name, data,
  2325. btrfs_fill_super, mnt);
  2326. }
  2327. static int btrfs_getattr(struct vfsmount *mnt,
  2328. struct dentry *dentry, struct kstat *stat)
  2329. {
  2330. struct inode *inode = dentry->d_inode;
  2331. generic_fillattr(inode, stat);
  2332. stat->blksize = 256 * 1024;
  2333. return 0;
  2334. }
  2335. static struct file_system_type btrfs_fs_type = {
  2336. .owner = THIS_MODULE,
  2337. .name = "btrfs",
  2338. .get_sb = btrfs_get_sb,
  2339. .kill_sb = kill_block_super,
  2340. .fs_flags = FS_REQUIRES_DEV,
  2341. };
  2342. static struct super_operations btrfs_super_ops = {
  2343. .statfs = simple_statfs,
  2344. .delete_inode = btrfs_delete_inode,
  2345. .put_super = btrfs_put_super,
  2346. .read_inode = btrfs_read_locked_inode,
  2347. .write_super = btrfs_write_super,
  2348. .sync_fs = btrfs_sync_fs,
  2349. .write_inode = btrfs_write_inode,
  2350. .alloc_inode = btrfs_alloc_inode,
  2351. .destroy_inode = btrfs_destroy_inode,
  2352. };
  2353. static struct inode_operations btrfs_dir_inode_operations = {
  2354. .lookup = btrfs_lookup,
  2355. .create = btrfs_create,
  2356. .unlink = btrfs_unlink,
  2357. .mkdir = btrfs_mkdir,
  2358. .rmdir = btrfs_rmdir,
  2359. };
  2360. static struct inode_operations btrfs_dir_ro_inode_operations = {
  2361. .lookup = btrfs_lookup,
  2362. };
  2363. static struct file_operations btrfs_dir_file_operations = {
  2364. .llseek = generic_file_llseek,
  2365. .read = generic_read_dir,
  2366. .readdir = btrfs_readdir,
  2367. .ioctl = btrfs_ioctl,
  2368. };
  2369. static struct address_space_operations btrfs_aops = {
  2370. .readpage = btrfs_readpage,
  2371. .writepage = btrfs_writepage,
  2372. .sync_page = block_sync_page,
  2373. .prepare_write = btrfs_prepare_write,
  2374. .commit_write = btrfs_commit_write,
  2375. };
  2376. static struct inode_operations btrfs_file_inode_operations = {
  2377. .truncate = btrfs_truncate,
  2378. .getattr = btrfs_getattr,
  2379. };
  2380. static struct file_operations btrfs_file_operations = {
  2381. .llseek = generic_file_llseek,
  2382. .read = do_sync_read,
  2383. .aio_read = btrfs_file_aio_read,
  2384. .write = btrfs_file_write,
  2385. .mmap = generic_file_mmap,
  2386. .open = generic_file_open,
  2387. .ioctl = btrfs_ioctl,
  2388. };
  2389. static int __init init_btrfs_fs(void)
  2390. {
  2391. int err;
  2392. printk("btrfs loaded!\n");
  2393. err = init_inodecache();
  2394. if (err)
  2395. return err;
  2396. kset_set_kset_s(&btrfs_subsys, fs_subsys);
  2397. err = subsystem_register(&btrfs_subsys);
  2398. if (err)
  2399. goto out;
  2400. return register_filesystem(&btrfs_fs_type);
  2401. out:
  2402. destroy_inodecache();
  2403. return err;
  2404. }
  2405. static void __exit exit_btrfs_fs(void)
  2406. {
  2407. destroy_inodecache();
  2408. unregister_filesystem(&btrfs_fs_type);
  2409. subsystem_unregister(&btrfs_subsys);
  2410. printk("btrfs unloaded\n");
  2411. }
  2412. module_init(init_btrfs_fs)
  2413. module_exit(exit_btrfs_fs)
  2414. MODULE_LICENSE("GPL");