super.c 80 KB

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