super.c 66 KB

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