super.c 64 KB

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