super.c 73 KB

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