super.c 79 KB

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