super.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836
  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 "ctree.h"
  15. #include "disk-io.h"
  16. #include "transaction.h"
  17. #include "btrfs_inode.h"
  18. void btrfs_fsinfo_release(struct kobject *obj)
  19. {
  20. struct btrfs_fs_info *fsinfo = container_of(obj,
  21. struct btrfs_fs_info, kobj);
  22. kfree(fsinfo);
  23. }
  24. struct kobj_type btrfs_fsinfo_ktype = {
  25. .release = btrfs_fsinfo_release,
  26. };
  27. decl_subsys(btrfs, &btrfs_fsinfo_ktype, NULL);
  28. #define BTRFS_SUPER_MAGIC 0x9123682E
  29. static struct inode_operations btrfs_dir_inode_operations;
  30. static struct inode_operations btrfs_dir_ro_inode_operations;
  31. static struct super_operations btrfs_super_ops;
  32. static struct file_operations btrfs_dir_file_operations;
  33. static struct inode_operations btrfs_file_inode_operations;
  34. static struct address_space_operations btrfs_aops;
  35. static struct file_operations btrfs_file_operations;
  36. static int check_inode(struct inode *inode)
  37. {
  38. struct btrfs_inode *ei = BTRFS_I(inode);
  39. WARN_ON(ei->magic != 0xDEADBEEF);
  40. WARN_ON(ei->magic2 != 0xDEADBEAF);
  41. return 0;
  42. }
  43. static void btrfs_read_locked_inode(struct inode *inode)
  44. {
  45. struct btrfs_path *path;
  46. struct btrfs_inode_item *inode_item;
  47. struct btrfs_root *root = BTRFS_I(inode)->root;
  48. struct btrfs_key location;
  49. int ret;
  50. path = btrfs_alloc_path();
  51. BUG_ON(!path);
  52. btrfs_init_path(path);
  53. mutex_lock(&root->fs_info->fs_mutex);
  54. check_inode(inode);
  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. check_inode(inode);
  62. inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  63. path->slots[0],
  64. struct btrfs_inode_item);
  65. inode->i_mode = btrfs_inode_mode(inode_item);
  66. inode->i_nlink = btrfs_inode_nlink(inode_item);
  67. inode->i_uid = btrfs_inode_uid(inode_item);
  68. inode->i_gid = btrfs_inode_gid(inode_item);
  69. inode->i_size = btrfs_inode_size(inode_item);
  70. inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
  71. inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
  72. inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
  73. inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
  74. inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
  75. inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
  76. inode->i_blocks = btrfs_inode_nblocks(inode_item);
  77. inode->i_generation = btrfs_inode_generation(inode_item);
  78. btrfs_free_path(path);
  79. inode_item = NULL;
  80. mutex_unlock(&root->fs_info->fs_mutex);
  81. check_inode(inode);
  82. switch (inode->i_mode & S_IFMT) {
  83. #if 0
  84. default:
  85. init_special_inode(inode, inode->i_mode,
  86. btrfs_inode_rdev(inode_item));
  87. break;
  88. #endif
  89. case S_IFREG:
  90. inode->i_mapping->a_ops = &btrfs_aops;
  91. inode->i_fop = &btrfs_file_operations;
  92. inode->i_op = &btrfs_file_inode_operations;
  93. break;
  94. case S_IFDIR:
  95. inode->i_fop = &btrfs_dir_file_operations;
  96. if (root == root->fs_info->tree_root)
  97. inode->i_op = &btrfs_dir_ro_inode_operations;
  98. else
  99. inode->i_op = &btrfs_dir_inode_operations;
  100. break;
  101. case S_IFLNK:
  102. // inode->i_op = &page_symlink_inode_operations;
  103. break;
  104. }
  105. check_inode(inode);
  106. return;
  107. make_bad:
  108. btrfs_release_path(root, path);
  109. btrfs_free_path(path);
  110. mutex_unlock(&root->fs_info->fs_mutex);
  111. make_bad_inode(inode);
  112. }
  113. static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
  114. struct btrfs_root *root,
  115. struct inode *dir,
  116. struct dentry *dentry)
  117. {
  118. struct btrfs_path *path;
  119. const char *name = dentry->d_name.name;
  120. int name_len = dentry->d_name.len;
  121. int ret;
  122. u64 objectid;
  123. struct btrfs_dir_item *di;
  124. path = btrfs_alloc_path();
  125. BUG_ON(!path);
  126. btrfs_init_path(path);
  127. ret = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  128. name, name_len, -1);
  129. if (ret < 0)
  130. goto err;
  131. if (ret > 0) {
  132. ret = -ENOENT;
  133. goto err;
  134. }
  135. di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  136. struct btrfs_dir_item);
  137. objectid = btrfs_disk_key_objectid(&di->location);
  138. ret = btrfs_del_item(trans, root, path);
  139. BUG_ON(ret);
  140. btrfs_release_path(root, path);
  141. ret = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
  142. objectid, -1);
  143. BUG_ON(ret);
  144. ret = btrfs_del_item(trans, root, path);
  145. BUG_ON(ret);
  146. dentry->d_inode->i_ctime = dir->i_ctime;
  147. err:
  148. btrfs_release_path(root, path);
  149. btrfs_free_path(path);
  150. if (ret == 0) {
  151. inode_dec_link_count(dentry->d_inode);
  152. dir->i_size -= name_len * 2;
  153. mark_inode_dirty(dir);
  154. }
  155. return ret;
  156. }
  157. static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
  158. {
  159. struct btrfs_root *root;
  160. struct btrfs_trans_handle *trans;
  161. int ret;
  162. root = BTRFS_I(dir)->root;
  163. mutex_lock(&root->fs_info->fs_mutex);
  164. trans = btrfs_start_transaction(root, 1);
  165. ret = btrfs_unlink_trans(trans, root, dir, dentry);
  166. btrfs_end_transaction(trans, root);
  167. mutex_unlock(&root->fs_info->fs_mutex);
  168. return ret;
  169. }
  170. static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
  171. {
  172. struct inode *inode = dentry->d_inode;
  173. int err;
  174. int ret;
  175. struct btrfs_root *root = BTRFS_I(dir)->root;
  176. struct btrfs_path *path;
  177. struct btrfs_key key;
  178. struct btrfs_trans_handle *trans;
  179. struct btrfs_key found_key;
  180. int found_type;
  181. struct btrfs_leaf *leaf;
  182. char *goodnames = "..";
  183. path = btrfs_alloc_path();
  184. BUG_ON(!path);
  185. btrfs_init_path(path);
  186. mutex_lock(&root->fs_info->fs_mutex);
  187. trans = btrfs_start_transaction(root, 1);
  188. key.objectid = inode->i_ino;
  189. key.offset = (u64)-1;
  190. key.flags = (u32)-1;
  191. while(1) {
  192. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  193. if (ret < 0) {
  194. err = ret;
  195. goto out;
  196. }
  197. BUG_ON(ret == 0);
  198. if (path->slots[0] == 0) {
  199. err = -ENOENT;
  200. goto out;
  201. }
  202. path->slots[0]--;
  203. leaf = btrfs_buffer_leaf(path->nodes[0]);
  204. btrfs_disk_key_to_cpu(&found_key,
  205. &leaf->items[path->slots[0]].key);
  206. found_type = btrfs_key_type(&found_key);
  207. if (found_key.objectid != inode->i_ino) {
  208. err = -ENOENT;
  209. goto out;
  210. }
  211. if ((found_type != BTRFS_DIR_ITEM_KEY &&
  212. found_type != BTRFS_DIR_INDEX_KEY) ||
  213. (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
  214. !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
  215. err = -ENOTEMPTY;
  216. goto out;
  217. }
  218. ret = btrfs_del_item(trans, root, path);
  219. BUG_ON(ret);
  220. if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
  221. break;
  222. btrfs_release_path(root, path);
  223. }
  224. ret = 0;
  225. btrfs_release_path(root, path);
  226. /* now the directory is empty */
  227. err = btrfs_unlink_trans(trans, root, dir, dentry);
  228. if (!err) {
  229. inode->i_size = 0;
  230. }
  231. out:
  232. btrfs_release_path(root, path);
  233. btrfs_free_path(path);
  234. mutex_unlock(&root->fs_info->fs_mutex);
  235. ret = btrfs_end_transaction(trans, root);
  236. if (ret && !err)
  237. err = ret;
  238. return err;
  239. }
  240. static int btrfs_free_inode(struct btrfs_trans_handle *trans,
  241. struct btrfs_root *root,
  242. struct inode *inode)
  243. {
  244. u64 objectid = inode->i_ino;
  245. struct btrfs_path *path;
  246. struct btrfs_inode_map_item *map;
  247. struct btrfs_key stat_data_key;
  248. int ret;
  249. clear_inode(inode);
  250. path = btrfs_alloc_path();
  251. BUG_ON(!path);
  252. btrfs_init_path(path);
  253. ret = btrfs_lookup_inode_map(trans, root, path, objectid, -1);
  254. if (ret) {
  255. if (ret > 0)
  256. ret = -ENOENT;
  257. goto error;
  258. }
  259. map = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  260. struct btrfs_inode_map_item);
  261. btrfs_disk_key_to_cpu(&stat_data_key, &map->key);
  262. ret = btrfs_del_item(trans, root->fs_info->inode_root, path);
  263. BUG_ON(ret);
  264. btrfs_release_path(root, path);
  265. ret = btrfs_lookup_inode(trans, root, path,
  266. &BTRFS_I(inode)->location, -1);
  267. BUG_ON(ret);
  268. ret = btrfs_del_item(trans, root, path);
  269. BUG_ON(ret);
  270. error:
  271. btrfs_release_path(root, path);
  272. btrfs_free_path(path);
  273. return ret;
  274. }
  275. static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
  276. struct btrfs_root *root,
  277. struct inode *inode)
  278. {
  279. int ret;
  280. struct btrfs_path *path;
  281. struct btrfs_key key;
  282. struct btrfs_disk_key *found_key;
  283. struct btrfs_leaf *leaf;
  284. struct btrfs_file_extent_item *fi = NULL;
  285. u64 extent_start = 0;
  286. u64 extent_num_blocks = 0;
  287. int found_extent;
  288. path = btrfs_alloc_path();
  289. BUG_ON(!path);
  290. /* FIXME, add redo link to tree so we don't leak on crash */
  291. key.objectid = inode->i_ino;
  292. key.offset = (u64)-1;
  293. key.flags = 0;
  294. /*
  295. * use BTRFS_CSUM_ITEM_KEY because it is larger than inline keys
  296. * or extent data
  297. */
  298. btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY);
  299. while(1) {
  300. btrfs_init_path(path);
  301. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  302. if (ret < 0) {
  303. goto error;
  304. }
  305. if (ret > 0) {
  306. BUG_ON(path->slots[0] == 0);
  307. path->slots[0]--;
  308. }
  309. leaf = btrfs_buffer_leaf(path->nodes[0]);
  310. found_key = &leaf->items[path->slots[0]].key;
  311. if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
  312. break;
  313. if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY &&
  314. btrfs_disk_key_type(found_key) != BTRFS_INLINE_DATA_KEY &&
  315. btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY)
  316. break;
  317. if (btrfs_disk_key_offset(found_key) < inode->i_size)
  318. break;
  319. if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) {
  320. fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  321. path->slots[0],
  322. struct btrfs_file_extent_item);
  323. extent_start = btrfs_file_extent_disk_blocknr(fi);
  324. extent_num_blocks =
  325. btrfs_file_extent_disk_num_blocks(fi);
  326. inode->i_blocks -=
  327. btrfs_file_extent_num_blocks(fi) >> 9;
  328. found_extent = 1;
  329. } else {
  330. found_extent = 0;
  331. }
  332. ret = btrfs_del_item(trans, root, path);
  333. BUG_ON(ret);
  334. btrfs_release_path(root, path);
  335. if (found_extent) {
  336. ret = btrfs_free_extent(trans, root, extent_start,
  337. extent_num_blocks, 0);
  338. BUG_ON(ret);
  339. }
  340. }
  341. ret = 0;
  342. error:
  343. btrfs_release_path(root, path);
  344. btrfs_free_path(path);
  345. return ret;
  346. }
  347. static void btrfs_delete_inode(struct inode *inode)
  348. {
  349. struct btrfs_trans_handle *trans;
  350. struct btrfs_root *root = BTRFS_I(inode)->root;
  351. int ret;
  352. truncate_inode_pages(&inode->i_data, 0);
  353. if (is_bad_inode(inode)) {
  354. goto no_delete;
  355. }
  356. inode->i_size = 0;
  357. mutex_lock(&root->fs_info->fs_mutex);
  358. trans = btrfs_start_transaction(root, 1);
  359. if (S_ISREG(inode->i_mode)) {
  360. ret = btrfs_truncate_in_trans(trans, root, inode);
  361. BUG_ON(ret);
  362. }
  363. btrfs_free_inode(trans, root, inode);
  364. btrfs_end_transaction(trans, root);
  365. mutex_unlock(&root->fs_info->fs_mutex);
  366. return;
  367. no_delete:
  368. clear_inode(inode);
  369. }
  370. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  371. struct btrfs_key *location)
  372. {
  373. const char *name = dentry->d_name.name;
  374. int namelen = dentry->d_name.len;
  375. struct btrfs_dir_item *di;
  376. struct btrfs_path *path;
  377. struct btrfs_root *root = BTRFS_I(dir)->root;
  378. int ret;
  379. path = btrfs_alloc_path();
  380. BUG_ON(!path);
  381. btrfs_init_path(path);
  382. ret = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
  383. namelen, 0);
  384. if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) {
  385. location->objectid = 0;
  386. ret = 0;
  387. goto out;
  388. }
  389. di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  390. struct btrfs_dir_item);
  391. btrfs_disk_key_to_cpu(location, &di->location);
  392. out:
  393. btrfs_release_path(root, path);
  394. btrfs_free_path(path);
  395. check_inode(dir);
  396. return ret;
  397. }
  398. int fixup_tree_root_location(struct btrfs_root *root,
  399. struct btrfs_key *location,
  400. struct btrfs_root **sub_root)
  401. {
  402. struct btrfs_path *path;
  403. struct btrfs_root_item *ri;
  404. if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
  405. return 0;
  406. if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
  407. return 0;
  408. path = btrfs_alloc_path();
  409. BUG_ON(!path);
  410. mutex_lock(&root->fs_info->fs_mutex);
  411. *sub_root = btrfs_read_fs_root(root->fs_info, location);
  412. if (IS_ERR(*sub_root))
  413. return PTR_ERR(*sub_root);
  414. ri = &(*sub_root)->root_item;
  415. location->objectid = btrfs_root_dirid(ri);
  416. location->flags = 0;
  417. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  418. location->offset = 0;
  419. btrfs_free_path(path);
  420. mutex_unlock(&root->fs_info->fs_mutex);
  421. return 0;
  422. }
  423. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  424. struct nameidata *nd)
  425. {
  426. struct inode * inode;
  427. struct btrfs_inode *bi = BTRFS_I(dir);
  428. struct btrfs_root *root = bi->root;
  429. struct btrfs_root *sub_root = root;
  430. struct btrfs_key location;
  431. int ret;
  432. if (dentry->d_name.len > BTRFS_NAME_LEN)
  433. return ERR_PTR(-ENAMETOOLONG);
  434. mutex_lock(&root->fs_info->fs_mutex);
  435. ret = btrfs_inode_by_name(dir, dentry, &location);
  436. mutex_unlock(&root->fs_info->fs_mutex);
  437. if (ret < 0)
  438. return ERR_PTR(ret);
  439. inode = NULL;
  440. if (location.objectid) {
  441. ret = fixup_tree_root_location(root, &location, &sub_root);
  442. if (ret < 0)
  443. return ERR_PTR(ret);
  444. if (ret > 0)
  445. return ERR_PTR(-ENOENT);
  446. inode = iget_locked(dir->i_sb, location.objectid);
  447. if (!inode)
  448. return ERR_PTR(-EACCES);
  449. if (inode->i_state & I_NEW) {
  450. if (sub_root != root) {
  451. ret = radix_tree_insert(
  452. &root->fs_info->fs_roots_radix,
  453. (unsigned long)sub_root,
  454. sub_root);
  455. printk("adding new root for inode %lu\n", inode->i_ino);
  456. igrab(inode);
  457. sub_root->inode = inode;
  458. }
  459. BTRFS_I(inode)->root = sub_root;
  460. memcpy(&BTRFS_I(inode)->location, &location,
  461. sizeof(location));
  462. btrfs_read_locked_inode(inode);
  463. unlock_new_inode(inode);
  464. }
  465. check_inode(inode);
  466. }
  467. check_inode(dir);
  468. return d_splice_alias(inode, dentry);
  469. }
  470. static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
  471. {
  472. struct inode *inode = filp->f_path.dentry->d_inode;
  473. struct btrfs_root *root = BTRFS_I(inode)->root;
  474. struct btrfs_item *item;
  475. struct btrfs_dir_item *di;
  476. struct btrfs_key key;
  477. struct btrfs_path *path;
  478. int ret;
  479. u32 nritems;
  480. struct btrfs_leaf *leaf;
  481. int slot;
  482. int advance;
  483. unsigned char d_type = DT_UNKNOWN;
  484. int over = 0;
  485. int key_type = BTRFS_DIR_INDEX_KEY;
  486. /* FIXME, use a real flag for deciding about the key type */
  487. if (root->fs_info->tree_root == root)
  488. key_type = BTRFS_DIR_ITEM_KEY;
  489. mutex_lock(&root->fs_info->fs_mutex);
  490. key.objectid = inode->i_ino;
  491. key.flags = 0;
  492. btrfs_set_key_type(&key, key_type);
  493. key.offset = filp->f_pos;
  494. path = btrfs_alloc_path();
  495. btrfs_init_path(path);
  496. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  497. if (ret < 0) {
  498. goto err;
  499. }
  500. advance = 0;
  501. while(1) {
  502. leaf = btrfs_buffer_leaf(path->nodes[0]);
  503. nritems = btrfs_header_nritems(&leaf->header);
  504. slot = path->slots[0];
  505. if (advance || slot >= nritems) {
  506. if (slot >= nritems -1) {
  507. ret = btrfs_next_leaf(root, path);
  508. if (ret)
  509. break;
  510. leaf = btrfs_buffer_leaf(path->nodes[0]);
  511. nritems = btrfs_header_nritems(&leaf->header);
  512. slot = path->slots[0];
  513. } else {
  514. slot++;
  515. path->slots[0]++;
  516. }
  517. }
  518. advance = 1;
  519. item = leaf->items + slot;
  520. if (btrfs_disk_key_objectid(&item->key) != key.objectid)
  521. break;
  522. if (key_type == BTRFS_DIR_INDEX_KEY &&
  523. btrfs_disk_key_offset(&item->key) >
  524. root->fs_info->highest_inode)
  525. break;
  526. if (btrfs_disk_key_type(&item->key) != key_type)
  527. continue;
  528. if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
  529. continue;
  530. filp->f_pos = btrfs_disk_key_offset(&item->key);
  531. advance = 1;
  532. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  533. over = filldir(dirent, (const char *)(di + 1),
  534. btrfs_dir_name_len(di),
  535. btrfs_disk_key_offset(&item->key),
  536. btrfs_disk_key_objectid(&di->location), d_type);
  537. if (over)
  538. goto nopos;
  539. }
  540. filp->f_pos++;
  541. nopos:
  542. ret = 0;
  543. err:
  544. btrfs_release_path(root, path);
  545. btrfs_free_path(path);
  546. mutex_unlock(&root->fs_info->fs_mutex);
  547. return ret;
  548. }
  549. static void btrfs_put_super (struct super_block * sb)
  550. {
  551. struct btrfs_root *root = btrfs_sb(sb);
  552. int ret;
  553. ret = close_ctree(root);
  554. if (ret) {
  555. printk("close ctree returns %d\n", ret);
  556. }
  557. sb->s_fs_info = NULL;
  558. }
  559. static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
  560. {
  561. struct inode * inode;
  562. struct dentry * root_dentry;
  563. struct btrfs_super_block *disk_super;
  564. struct btrfs_root *tree_root;
  565. struct btrfs_inode *bi;
  566. sb->s_maxbytes = MAX_LFS_FILESIZE;
  567. sb->s_magic = BTRFS_SUPER_MAGIC;
  568. sb->s_op = &btrfs_super_ops;
  569. sb->s_time_gran = 1;
  570. tree_root = open_ctree(sb);
  571. if (!tree_root) {
  572. printk("btrfs: open_ctree failed\n");
  573. return -EIO;
  574. }
  575. sb->s_fs_info = tree_root;
  576. disk_super = tree_root->fs_info->disk_super;
  577. printk("read in super total blocks %Lu root %Lu\n",
  578. btrfs_super_total_blocks(disk_super),
  579. btrfs_super_root_dir(disk_super));
  580. inode = iget_locked(sb, btrfs_super_root_dir(disk_super));
  581. bi = BTRFS_I(inode);
  582. bi->location.objectid = inode->i_ino;
  583. bi->location.offset = 0;
  584. bi->location.flags = 0;
  585. bi->root = tree_root;
  586. btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
  587. if (!inode)
  588. return -ENOMEM;
  589. if (inode->i_state & I_NEW) {
  590. btrfs_read_locked_inode(inode);
  591. unlock_new_inode(inode);
  592. }
  593. root_dentry = d_alloc_root(inode);
  594. if (!root_dentry) {
  595. iput(inode);
  596. return -ENOMEM;
  597. }
  598. sb->s_root = root_dentry;
  599. return 0;
  600. }
  601. static void fill_inode_item(struct btrfs_inode_item *item,
  602. struct inode *inode)
  603. {
  604. btrfs_set_inode_uid(item, inode->i_uid);
  605. btrfs_set_inode_gid(item, inode->i_gid);
  606. btrfs_set_inode_size(item, inode->i_size);
  607. btrfs_set_inode_mode(item, inode->i_mode);
  608. btrfs_set_inode_nlink(item, inode->i_nlink);
  609. btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
  610. btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
  611. btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
  612. btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
  613. btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
  614. btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
  615. btrfs_set_inode_nblocks(item, inode->i_blocks);
  616. btrfs_set_inode_generation(item, inode->i_generation);
  617. check_inode(inode);
  618. }
  619. static int btrfs_update_inode(struct btrfs_trans_handle *trans,
  620. struct btrfs_root *root,
  621. struct inode *inode)
  622. {
  623. struct btrfs_inode_item *inode_item;
  624. struct btrfs_path *path;
  625. int ret;
  626. path = btrfs_alloc_path();
  627. BUG_ON(!path);
  628. btrfs_init_path(path);
  629. ret = btrfs_lookup_inode(trans, root, path,
  630. &BTRFS_I(inode)->location, 1);
  631. if (ret) {
  632. if (ret > 0)
  633. ret = -ENOENT;
  634. goto failed;
  635. }
  636. inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  637. path->slots[0],
  638. struct btrfs_inode_item);
  639. fill_inode_item(inode_item, inode);
  640. btrfs_mark_buffer_dirty(path->nodes[0]);
  641. failed:
  642. btrfs_release_path(root, path);
  643. btrfs_free_path(path);
  644. check_inode(inode);
  645. return 0;
  646. }
  647. static int btrfs_write_inode(struct inode *inode, int wait)
  648. {
  649. struct btrfs_root *root = BTRFS_I(inode)->root;
  650. struct btrfs_trans_handle *trans;
  651. int ret;
  652. mutex_lock(&root->fs_info->fs_mutex);
  653. trans = btrfs_start_transaction(root, 1);
  654. ret = btrfs_update_inode(trans, root, inode);
  655. if (wait)
  656. btrfs_commit_transaction(trans, root);
  657. else
  658. btrfs_end_transaction(trans, root);
  659. mutex_unlock(&root->fs_info->fs_mutex);
  660. check_inode(inode);
  661. return ret;
  662. }
  663. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  664. struct inode *dir, int mode)
  665. {
  666. struct inode *inode;
  667. struct btrfs_inode_item inode_item;
  668. struct btrfs_root *root = BTRFS_I(dir)->root;
  669. struct btrfs_key *key;
  670. int ret;
  671. u64 objectid;
  672. inode = new_inode(dir->i_sb);
  673. if (!inode)
  674. return ERR_PTR(-ENOMEM);
  675. BTRFS_I(inode)->root = BTRFS_I(dir)->root;
  676. key = &BTRFS_I(inode)->location;
  677. check_inode(inode);
  678. ret = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  679. BUG_ON(ret);
  680. inode->i_uid = current->fsuid;
  681. inode->i_gid = current->fsgid;
  682. inode->i_mode = mode;
  683. inode->i_ino = objectid;
  684. inode->i_blocks = 0;
  685. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
  686. fill_inode_item(&inode_item, inode);
  687. key->objectid = objectid;
  688. key->flags = 0;
  689. key->offset = 0;
  690. btrfs_set_key_type(key, BTRFS_INODE_ITEM_KEY);
  691. ret = btrfs_insert_inode_map(trans, root, objectid, key);
  692. BUG_ON(ret);
  693. ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
  694. BUG_ON(ret);
  695. insert_inode_hash(inode);
  696. check_inode(inode);
  697. check_inode(dir);
  698. return inode;
  699. }
  700. static int btrfs_add_link(struct btrfs_trans_handle *trans,
  701. struct dentry *dentry, struct inode *inode)
  702. {
  703. int ret;
  704. struct btrfs_key key;
  705. struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
  706. key.objectid = inode->i_ino;
  707. key.flags = 0;
  708. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  709. key.offset = 0;
  710. ret = btrfs_insert_dir_item(trans, root,
  711. dentry->d_name.name, dentry->d_name.len,
  712. dentry->d_parent->d_inode->i_ino,
  713. &key, 0);
  714. if (ret == 0) {
  715. dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
  716. ret = btrfs_update_inode(trans, root,
  717. dentry->d_parent->d_inode);
  718. }
  719. check_inode(inode);
  720. check_inode(dentry->d_parent->d_inode);
  721. return ret;
  722. }
  723. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  724. struct dentry *dentry, struct inode *inode)
  725. {
  726. int err = btrfs_add_link(trans, dentry, inode);
  727. if (!err) {
  728. d_instantiate(dentry, inode);
  729. return 0;
  730. }
  731. if (err > 0)
  732. err = -EEXIST;
  733. check_inode(inode);
  734. return err;
  735. }
  736. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  737. int mode, struct nameidata *nd)
  738. {
  739. struct btrfs_trans_handle *trans;
  740. struct btrfs_root *root = BTRFS_I(dir)->root;
  741. struct inode *inode;
  742. int err;
  743. int drop_inode = 0;
  744. mutex_lock(&root->fs_info->fs_mutex);
  745. trans = btrfs_start_transaction(root, 1);
  746. inode = btrfs_new_inode(trans, dir, mode);
  747. err = PTR_ERR(inode);
  748. if (IS_ERR(inode))
  749. goto out_unlock;
  750. // FIXME mark the inode dirty
  751. err = btrfs_add_nondir(trans, dentry, inode);
  752. if (err)
  753. drop_inode = 1;
  754. else {
  755. inode->i_mapping->a_ops = &btrfs_aops;
  756. inode->i_fop = &btrfs_file_operations;
  757. inode->i_op = &btrfs_file_inode_operations;
  758. }
  759. dir->i_sb->s_dirt = 1;
  760. out_unlock:
  761. btrfs_end_transaction(trans, root);
  762. mutex_unlock(&root->fs_info->fs_mutex);
  763. check_inode(inode);
  764. check_inode(dir);
  765. if (drop_inode) {
  766. inode_dec_link_count(inode);
  767. iput(inode);
  768. }
  769. return err;
  770. }
  771. static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
  772. struct inode *inode, struct inode *dir)
  773. {
  774. struct btrfs_root *root = BTRFS_I(dir)->root;
  775. int ret;
  776. char buf[2];
  777. struct btrfs_key key;
  778. buf[0] = '.';
  779. buf[1] = '.';
  780. key.objectid = inode->i_ino;
  781. key.offset = 0;
  782. key.flags = 0;
  783. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  784. ret = btrfs_insert_dir_item(trans, root, buf, 1, inode->i_ino,
  785. &key, 1);
  786. if (ret)
  787. goto error;
  788. key.objectid = dir->i_ino;
  789. ret = btrfs_insert_dir_item(trans, root, buf, 2, inode->i_ino,
  790. &key, 1);
  791. if (ret)
  792. goto error;
  793. inode->i_size = 6;
  794. ret = btrfs_update_inode(trans, root, inode);
  795. error:
  796. return ret;
  797. }
  798. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  799. {
  800. struct inode *inode;
  801. struct btrfs_trans_handle *trans;
  802. struct btrfs_root *root = BTRFS_I(dir)->root;
  803. int err = 0;
  804. int drop_on_err = 0;
  805. mutex_lock(&root->fs_info->fs_mutex);
  806. trans = btrfs_start_transaction(root, 1);
  807. if (IS_ERR(trans)) {
  808. err = PTR_ERR(trans);
  809. goto out_unlock;
  810. }
  811. inode = btrfs_new_inode(trans, dir, S_IFDIR | mode);
  812. if (IS_ERR(inode)) {
  813. err = PTR_ERR(inode);
  814. goto out_fail;
  815. }
  816. drop_on_err = 1;
  817. inode->i_op = &btrfs_dir_inode_operations;
  818. inode->i_fop = &btrfs_dir_file_operations;
  819. err = btrfs_make_empty_dir(trans, inode, dir);
  820. if (err)
  821. goto out_fail;
  822. err = btrfs_add_link(trans, dentry, inode);
  823. if (err)
  824. goto out_fail;
  825. d_instantiate(dentry, inode);
  826. drop_on_err = 0;
  827. out_fail:
  828. btrfs_end_transaction(trans, root);
  829. out_unlock:
  830. mutex_unlock(&root->fs_info->fs_mutex);
  831. if (drop_on_err)
  832. iput(inode);
  833. return err;
  834. }
  835. static int btrfs_sync_fs(struct super_block *sb, int wait)
  836. {
  837. struct btrfs_trans_handle *trans;
  838. struct btrfs_root *root;
  839. int ret;
  840. root = btrfs_sb(sb);
  841. sb->s_dirt = 0;
  842. if (!wait) {
  843. filemap_flush(root->fs_info->btree_inode->i_mapping);
  844. return 0;
  845. }
  846. filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
  847. mutex_lock(&root->fs_info->fs_mutex);
  848. trans = btrfs_start_transaction(root, 1);
  849. ret = btrfs_commit_transaction(trans, root);
  850. sb->s_dirt = 0;
  851. BUG_ON(ret);
  852. printk("btrfs sync_fs\n");
  853. mutex_unlock(&root->fs_info->fs_mutex);
  854. return 0;
  855. }
  856. #if 0
  857. static int btrfs_get_block_inline(struct inode *inode, sector_t iblock,
  858. struct buffer_head *result, int create)
  859. {
  860. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  861. struct btrfs_path *path;
  862. struct btrfs_key key;
  863. struct btrfs_leaf *leaf;
  864. int num_bytes = result->b_size;
  865. int item_size;
  866. int ret;
  867. u64 pos;
  868. char *ptr;
  869. int copy_size;
  870. int err = 0;
  871. char *safe_ptr;
  872. char *data_ptr;
  873. path = btrfs_alloc_path();
  874. BUG_ON(!path);
  875. WARN_ON(create);
  876. if (create) {
  877. return 0;
  878. }
  879. pos = iblock << inode->i_blkbits;
  880. key.objectid = inode->i_ino;
  881. key.flags = 0;
  882. btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
  883. ptr = kmap(result->b_page);
  884. safe_ptr = ptr;
  885. ptr += (pos & (PAGE_CACHE_SIZE -1));
  886. again:
  887. key.offset = pos;
  888. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  889. if (ret) {
  890. if (ret < 0)
  891. err = ret;
  892. else
  893. err = 0;
  894. goto out;
  895. }
  896. leaf = btrfs_buffer_leaf(path->nodes[0]);
  897. item_size = btrfs_item_size(leaf->items + path->slots[0]);
  898. copy_size = min(num_bytes, item_size);
  899. data_ptr = btrfs_item_ptr(leaf, path->slots[0], char);
  900. WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size);
  901. memcpy(ptr, data_ptr, copy_size);
  902. pos += copy_size;
  903. num_bytes -= copy_size;
  904. WARN_ON(num_bytes < 0);
  905. ptr += copy_size;
  906. btrfs_release_path(root, path);
  907. if (num_bytes != 0) {
  908. if (pos >= i_size_read(inode))
  909. memset(ptr, 0, num_bytes);
  910. else
  911. goto again;
  912. }
  913. set_buffer_uptodate(result);
  914. map_bh(result, inode->i_sb, 0);
  915. err = 0;
  916. out:
  917. btrfs_free_path(path);
  918. kunmap(result->b_page);
  919. return err;
  920. }
  921. #endif
  922. static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
  923. struct buffer_head *result, int create)
  924. {
  925. int ret;
  926. int err = 0;
  927. u64 blocknr;
  928. u64 extent_start = 0;
  929. u64 extent_end = 0;
  930. u64 objectid = inode->i_ino;
  931. struct btrfs_path *path;
  932. struct btrfs_root *root = BTRFS_I(inode)->root;
  933. struct btrfs_trans_handle *trans = NULL;
  934. struct btrfs_file_extent_item *item;
  935. struct btrfs_leaf *leaf;
  936. struct btrfs_disk_key *found_key;
  937. path = btrfs_alloc_path();
  938. BUG_ON(!path);
  939. btrfs_init_path(path);
  940. if (create)
  941. trans = btrfs_start_transaction(root, 1);
  942. ret = btrfs_lookup_file_extent(trans, root, path,
  943. inode->i_ino,
  944. iblock << inode->i_blkbits, 0);
  945. if (ret < 0) {
  946. err = ret;
  947. goto out;
  948. }
  949. if (ret != 0) {
  950. if (path->slots[0] == 0) {
  951. btrfs_release_path(root, path);
  952. goto allocate;
  953. }
  954. path->slots[0]--;
  955. }
  956. item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  957. struct btrfs_file_extent_item);
  958. leaf = btrfs_buffer_leaf(path->nodes[0]);
  959. blocknr = btrfs_file_extent_disk_blocknr(item);
  960. blocknr += btrfs_file_extent_offset(item);
  961. /* exact match found, use it */
  962. if (ret == 0) {
  963. err = 0;
  964. map_bh(result, inode->i_sb, blocknr);
  965. goto out;
  966. }
  967. /* are we inside the extent that was found? */
  968. found_key = &leaf->items[path->slots[0]].key;
  969. if (btrfs_disk_key_objectid(found_key) != objectid ||
  970. btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
  971. extent_end = 0;
  972. extent_start = 0;
  973. btrfs_release_path(root, path);
  974. goto allocate;
  975. }
  976. extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
  977. extent_start = extent_start >> inode->i_blkbits;
  978. extent_start += btrfs_file_extent_offset(item);
  979. extent_end = extent_start + btrfs_file_extent_num_blocks(item);
  980. if (iblock >= extent_start && iblock < extent_end) {
  981. err = 0;
  982. map_bh(result, inode->i_sb, blocknr + iblock - extent_start);
  983. goto out;
  984. }
  985. allocate:
  986. /* ok, create a new extent */
  987. if (!create) {
  988. err = 0;
  989. goto out;
  990. }
  991. ret = btrfs_alloc_file_extent(trans, root, objectid,
  992. iblock << inode->i_blkbits,
  993. 1, extent_end, &blocknr);
  994. if (ret) {
  995. err = ret;
  996. goto out;
  997. }
  998. inode->i_blocks += inode->i_sb->s_blocksize >> 9;
  999. set_buffer_new(result);
  1000. map_bh(result, inode->i_sb, blocknr);
  1001. out:
  1002. btrfs_release_path(root, path);
  1003. btrfs_free_path(path);
  1004. if (trans)
  1005. btrfs_end_transaction(trans, root);
  1006. return err;
  1007. }
  1008. static int btrfs_get_block(struct inode *inode, sector_t iblock,
  1009. struct buffer_head *result, int create)
  1010. {
  1011. int err;
  1012. struct btrfs_root *root = BTRFS_I(inode)->root;
  1013. mutex_lock(&root->fs_info->fs_mutex);
  1014. err = btrfs_get_block_lock(inode, iblock, result, create);
  1015. // err = btrfs_get_block_inline(inode, iblock, result, create);
  1016. mutex_unlock(&root->fs_info->fs_mutex);
  1017. return err;
  1018. }
  1019. static int btrfs_prepare_write(struct file *file, struct page *page,
  1020. unsigned from, unsigned to)
  1021. {
  1022. return nobh_prepare_write(page, from, to, btrfs_get_block);
  1023. }
  1024. static int btrfs_commit_write(struct file *file, struct page *page,
  1025. unsigned from, unsigned to)
  1026. {
  1027. return nobh_commit_write(file, page, from, to);
  1028. }
  1029. static void btrfs_write_super(struct super_block *sb)
  1030. {
  1031. btrfs_sync_fs(sb, 1);
  1032. }
  1033. static int btrfs_readpage(struct file *file, struct page *page)
  1034. {
  1035. return mpage_readpage(page, btrfs_get_block);
  1036. }
  1037. static int btrfs_readpages(struct file *file, struct address_space *mapping,
  1038. struct list_head *pages, unsigned nr_pages)
  1039. {
  1040. return mpage_readpages(mapping, pages, nr_pages, btrfs_get_block);
  1041. }
  1042. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  1043. {
  1044. return nobh_writepage(page, btrfs_get_block, wbc);
  1045. }
  1046. static void btrfs_truncate(struct inode *inode)
  1047. {
  1048. struct btrfs_root *root = BTRFS_I(inode)->root;
  1049. int ret;
  1050. struct btrfs_trans_handle *trans;
  1051. if (!S_ISREG(inode->i_mode))
  1052. return;
  1053. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1054. return;
  1055. nobh_truncate_page(inode->i_mapping, inode->i_size);
  1056. /* FIXME, add redo link to tree so we don't leak on crash */
  1057. mutex_lock(&root->fs_info->fs_mutex);
  1058. trans = btrfs_start_transaction(root, 1);
  1059. ret = btrfs_truncate_in_trans(trans, root, inode);
  1060. BUG_ON(ret);
  1061. ret = btrfs_end_transaction(trans, root);
  1062. BUG_ON(ret);
  1063. mutex_unlock(&root->fs_info->fs_mutex);
  1064. mark_inode_dirty(inode);
  1065. }
  1066. static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
  1067. struct page **prepared_pages,
  1068. const char __user * buf)
  1069. {
  1070. long page_fault = 0;
  1071. int i;
  1072. int offset = pos & (PAGE_CACHE_SIZE - 1);
  1073. for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
  1074. size_t count = min_t(size_t,
  1075. PAGE_CACHE_SIZE - offset, write_bytes);
  1076. struct page *page = prepared_pages[i];
  1077. fault_in_pages_readable(buf, count);
  1078. /* Copy data from userspace to the current page */
  1079. kmap(page);
  1080. page_fault = __copy_from_user(page_address(page) + offset,
  1081. buf, count);
  1082. /* Flush processor's dcache for this page */
  1083. flush_dcache_page(page);
  1084. kunmap(page);
  1085. buf += count;
  1086. write_bytes -= count;
  1087. if (page_fault)
  1088. break;
  1089. }
  1090. return page_fault ? -EFAULT : 0;
  1091. }
  1092. static void btrfs_drop_pages(struct page **pages, size_t num_pages)
  1093. {
  1094. size_t i;
  1095. for (i = 0; i < num_pages; i++) {
  1096. if (!pages[i])
  1097. break;
  1098. unlock_page(pages[i]);
  1099. mark_page_accessed(pages[i]);
  1100. page_cache_release(pages[i]);
  1101. }
  1102. }
  1103. static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
  1104. struct btrfs_root *root,
  1105. struct file *file,
  1106. struct page **pages,
  1107. size_t num_pages,
  1108. loff_t pos,
  1109. size_t write_bytes)
  1110. {
  1111. int i;
  1112. int offset;
  1113. int err = 0;
  1114. int ret;
  1115. int this_write;
  1116. struct inode *inode = file->f_path.dentry->d_inode;
  1117. for (i = 0; i < num_pages; i++) {
  1118. offset = pos & (PAGE_CACHE_SIZE -1);
  1119. this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
  1120. /* FIXME, one block at a time */
  1121. mutex_lock(&root->fs_info->fs_mutex);
  1122. trans = btrfs_start_transaction(root, 1);
  1123. btrfs_csum_file_block(trans, root, inode->i_ino,
  1124. pages[i]->index << PAGE_CACHE_SHIFT,
  1125. kmap(pages[i]), PAGE_CACHE_SIZE);
  1126. kunmap(pages[i]);
  1127. SetPageChecked(pages[i]);
  1128. ret = btrfs_end_transaction(trans, root);
  1129. BUG_ON(ret);
  1130. mutex_unlock(&root->fs_info->fs_mutex);
  1131. ret = nobh_commit_write(file, pages[i], offset,
  1132. offset + this_write);
  1133. pos += this_write;
  1134. if (ret) {
  1135. err = ret;
  1136. goto failed;
  1137. }
  1138. WARN_ON(this_write > write_bytes);
  1139. write_bytes -= this_write;
  1140. }
  1141. failed:
  1142. return err;
  1143. }
  1144. static int prepare_pages(struct btrfs_trans_handle *trans,
  1145. struct btrfs_root *root,
  1146. struct file *file,
  1147. struct page **pages,
  1148. size_t num_pages,
  1149. loff_t pos,
  1150. size_t write_bytes)
  1151. {
  1152. int i;
  1153. unsigned long index = pos >> PAGE_CACHE_SHIFT;
  1154. struct inode *inode = file->f_path.dentry->d_inode;
  1155. int offset;
  1156. int err = 0;
  1157. int ret;
  1158. int this_write;
  1159. loff_t isize = i_size_read(inode);
  1160. memset(pages, 0, num_pages * sizeof(struct page *));
  1161. for (i = 0; i < num_pages; i++) {
  1162. pages[i] = grab_cache_page(inode->i_mapping, index + i);
  1163. if (!pages[i]) {
  1164. err = -ENOMEM;
  1165. goto failed_release;
  1166. }
  1167. offset = pos & (PAGE_CACHE_SIZE -1);
  1168. this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
  1169. ret = nobh_prepare_write(pages[i], offset,
  1170. offset + this_write,
  1171. btrfs_get_block);
  1172. pos += this_write;
  1173. if (ret) {
  1174. err = ret;
  1175. goto failed_truncate;
  1176. }
  1177. WARN_ON(this_write > write_bytes);
  1178. write_bytes -= this_write;
  1179. }
  1180. return 0;
  1181. failed_release:
  1182. btrfs_drop_pages(pages, num_pages);
  1183. return err;
  1184. failed_truncate:
  1185. btrfs_drop_pages(pages, num_pages);
  1186. if (pos > isize)
  1187. vmtruncate(inode, isize);
  1188. return err;
  1189. }
  1190. static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
  1191. size_t count, loff_t *ppos)
  1192. {
  1193. loff_t pos;
  1194. size_t num_written = 0;
  1195. int err = 0;
  1196. int ret = 0;
  1197. struct inode *inode = file->f_path.dentry->d_inode;
  1198. struct btrfs_root *root = BTRFS_I(inode)->root;
  1199. struct page *pages[1];
  1200. if (file->f_flags & O_DIRECT)
  1201. return -EINVAL;
  1202. pos = *ppos;
  1203. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1204. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  1205. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  1206. if (err)
  1207. goto out;
  1208. if (count == 0)
  1209. goto out;
  1210. err = remove_suid(file->f_path.dentry);
  1211. if (err)
  1212. goto out;
  1213. file_update_time(file);
  1214. mutex_lock(&inode->i_mutex);
  1215. while(count > 0) {
  1216. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  1217. size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
  1218. size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
  1219. PAGE_CACHE_SHIFT;
  1220. ret = prepare_pages(NULL, root, file, pages, num_pages,
  1221. pos, write_bytes);
  1222. BUG_ON(ret);
  1223. ret = btrfs_copy_from_user(pos, num_pages,
  1224. write_bytes, pages, buf);
  1225. BUG_ON(ret);
  1226. ret = dirty_and_release_pages(NULL, root, file, pages,
  1227. num_pages, pos, write_bytes);
  1228. BUG_ON(ret);
  1229. btrfs_drop_pages(pages, num_pages);
  1230. buf += write_bytes;
  1231. count -= write_bytes;
  1232. pos += write_bytes;
  1233. num_written += write_bytes;
  1234. balance_dirty_pages_ratelimited(inode->i_mapping);
  1235. cond_resched();
  1236. }
  1237. mutex_unlock(&inode->i_mutex);
  1238. out:
  1239. *ppos = pos;
  1240. current->backing_dev_info = NULL;
  1241. return num_written ? num_written : err;
  1242. }
  1243. #if 0
  1244. static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode,
  1245. struct page *page, loff_t pos,
  1246. size_t offset, size_t write_bytes)
  1247. {
  1248. struct btrfs_path *path;
  1249. struct btrfs_trans_handle *trans;
  1250. struct btrfs_key key;
  1251. struct btrfs_leaf *leaf;
  1252. struct btrfs_key found_key;
  1253. int ret;
  1254. size_t copy_size = 0;
  1255. char *dst = NULL;
  1256. int err = 0;
  1257. size_t num_written = 0;
  1258. path = btrfs_alloc_path();
  1259. BUG_ON(!path);
  1260. mutex_lock(&root->fs_info->fs_mutex);
  1261. trans = btrfs_start_transaction(root, 1);
  1262. key.objectid = inode->i_ino;
  1263. key.flags = 0;
  1264. btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
  1265. again:
  1266. key.offset = pos;
  1267. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1268. if (ret < 0) {
  1269. err = ret;
  1270. goto out;
  1271. }
  1272. if (ret == 0) {
  1273. leaf = btrfs_buffer_leaf(path->nodes[0]);
  1274. btrfs_disk_key_to_cpu(&found_key,
  1275. &leaf->items[path->slots[0]].key);
  1276. copy_size = btrfs_item_size(leaf->items + path->slots[0]);
  1277. dst = btrfs_item_ptr(leaf, path->slots[0], char);
  1278. copy_size = min(write_bytes, copy_size);
  1279. goto copyit;
  1280. } else {
  1281. int slot = path->slots[0];
  1282. if (slot > 0) {
  1283. slot--;
  1284. }
  1285. // FIXME find max key
  1286. leaf = btrfs_buffer_leaf(path->nodes[0]);
  1287. btrfs_disk_key_to_cpu(&found_key,
  1288. &leaf->items[slot].key);
  1289. if (found_key.objectid != inode->i_ino)
  1290. goto insert;
  1291. if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY)
  1292. goto insert;
  1293. copy_size = btrfs_item_size(leaf->items + slot);
  1294. if (found_key.offset + copy_size <= pos)
  1295. goto insert;
  1296. dst = btrfs_item_ptr(leaf, path->slots[0], char);
  1297. dst += pos - found_key.offset;
  1298. copy_size = copy_size - (pos - found_key.offset);
  1299. BUG_ON(copy_size < 0);
  1300. copy_size = min(write_bytes, copy_size);
  1301. WARN_ON(copy_size == 0);
  1302. goto copyit;
  1303. }
  1304. insert:
  1305. btrfs_release_path(root, path);
  1306. copy_size = min(write_bytes,
  1307. (size_t)BTRFS_LEAF_DATA_SIZE(root) -
  1308. sizeof(struct btrfs_item) * 4);
  1309. ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size);
  1310. BUG_ON(ret);
  1311. dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  1312. path->slots[0], char);
  1313. copyit:
  1314. WARN_ON(copy_size == 0);
  1315. WARN_ON(dst + copy_size >
  1316. btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  1317. path->slots[0], char) +
  1318. btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items +
  1319. path->slots[0]));
  1320. btrfs_memcpy(root, path->nodes[0]->b_data, dst,
  1321. page_address(page) + offset, copy_size);
  1322. mark_buffer_dirty(path->nodes[0]);
  1323. btrfs_release_path(root, path);
  1324. pos += copy_size;
  1325. offset += copy_size;
  1326. num_written += copy_size;
  1327. write_bytes -= copy_size;
  1328. if (write_bytes)
  1329. goto again;
  1330. out:
  1331. btrfs_free_path(path);
  1332. ret = btrfs_end_transaction(trans, root);
  1333. BUG_ON(ret);
  1334. mutex_unlock(&root->fs_info->fs_mutex);
  1335. return num_written ? num_written : err;
  1336. }
  1337. static ssize_t btrfs_file_inline_write(struct file *file,
  1338. const char __user *buf,
  1339. size_t count, loff_t *ppos)
  1340. {
  1341. loff_t pos;
  1342. size_t num_written = 0;
  1343. int err = 0;
  1344. int ret = 0;
  1345. struct inode *inode = file->f_path.dentry->d_inode;
  1346. struct btrfs_root *root = BTRFS_I(inode)->root;
  1347. unsigned long page_index;
  1348. if (file->f_flags & O_DIRECT)
  1349. return -EINVAL;
  1350. pos = *ppos;
  1351. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1352. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  1353. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  1354. if (err)
  1355. goto out;
  1356. if (count == 0)
  1357. goto out;
  1358. err = remove_suid(file->f_path.dentry);
  1359. if (err)
  1360. goto out;
  1361. file_update_time(file);
  1362. mutex_lock(&inode->i_mutex);
  1363. while(count > 0) {
  1364. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  1365. size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
  1366. struct page *page;
  1367. page_index = pos >> PAGE_CACHE_SHIFT;
  1368. page = grab_cache_page(inode->i_mapping, page_index);
  1369. if (!PageUptodate(page)) {
  1370. ret = mpage_readpage(page, btrfs_get_block);
  1371. BUG_ON(ret);
  1372. lock_page(page);
  1373. }
  1374. ret = btrfs_copy_from_user(pos, 1,
  1375. write_bytes, &page, buf);
  1376. BUG_ON(ret);
  1377. write_bytes = inline_one_page(root, inode, page, pos,
  1378. offset, write_bytes);
  1379. SetPageUptodate(page);
  1380. if (write_bytes > 0 && pos + write_bytes > inode->i_size) {
  1381. i_size_write(inode, pos + write_bytes);
  1382. mark_inode_dirty(inode);
  1383. }
  1384. page_cache_release(page);
  1385. unlock_page(page);
  1386. if (write_bytes < 0)
  1387. goto out_unlock;
  1388. buf += write_bytes;
  1389. count -= write_bytes;
  1390. pos += write_bytes;
  1391. num_written += write_bytes;
  1392. balance_dirty_pages_ratelimited(inode->i_mapping);
  1393. cond_resched();
  1394. }
  1395. out_unlock:
  1396. mutex_unlock(&inode->i_mutex);
  1397. out:
  1398. *ppos = pos;
  1399. current->backing_dev_info = NULL;
  1400. return num_written ? num_written : err;
  1401. }
  1402. #endif
  1403. static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
  1404. unsigned long offset, unsigned long size)
  1405. {
  1406. char *kaddr;
  1407. unsigned long left, count = desc->count;
  1408. struct inode *inode = page->mapping->host;
  1409. if (size > count)
  1410. size = count;
  1411. if (!PageChecked(page)) {
  1412. /* FIXME, do it per block */
  1413. struct btrfs_root *root = BTRFS_I(inode)->root;
  1414. int ret = btrfs_csum_verify_file_block(root,
  1415. page->mapping->host->i_ino,
  1416. page->index << PAGE_CACHE_SHIFT,
  1417. kmap(page), PAGE_CACHE_SIZE);
  1418. if (ret) {
  1419. printk("failed to verify ino %lu page %lu\n",
  1420. page->mapping->host->i_ino,
  1421. page->index);
  1422. memset(page_address(page), 0, PAGE_CACHE_SIZE);
  1423. }
  1424. SetPageChecked(page);
  1425. kunmap(page);
  1426. }
  1427. /*
  1428. * Faults on the destination of a read are common, so do it before
  1429. * taking the kmap.
  1430. */
  1431. if (!fault_in_pages_writeable(desc->arg.buf, size)) {
  1432. kaddr = kmap_atomic(page, KM_USER0);
  1433. left = __copy_to_user_inatomic(desc->arg.buf,
  1434. kaddr + offset, size);
  1435. kunmap_atomic(kaddr, KM_USER0);
  1436. if (left == 0)
  1437. goto success;
  1438. }
  1439. /* Do it the slow way */
  1440. kaddr = kmap(page);
  1441. left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
  1442. kunmap(page);
  1443. if (left) {
  1444. size -= left;
  1445. desc->error = -EFAULT;
  1446. }
  1447. success:
  1448. desc->count = count - size;
  1449. desc->written += size;
  1450. desc->arg.buf += size;
  1451. return size;
  1452. }
  1453. /**
  1454. * btrfs_file_aio_read - filesystem read routine
  1455. * @iocb: kernel I/O control block
  1456. * @iov: io vector request
  1457. * @nr_segs: number of segments in the iovec
  1458. * @pos: current file position
  1459. */
  1460. static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
  1461. unsigned long nr_segs, loff_t pos)
  1462. {
  1463. struct file *filp = iocb->ki_filp;
  1464. ssize_t retval;
  1465. unsigned long seg;
  1466. size_t count;
  1467. loff_t *ppos = &iocb->ki_pos;
  1468. count = 0;
  1469. for (seg = 0; seg < nr_segs; seg++) {
  1470. const struct iovec *iv = &iov[seg];
  1471. /*
  1472. * If any segment has a negative length, or the cumulative
  1473. * length ever wraps negative then return -EINVAL.
  1474. */
  1475. count += iv->iov_len;
  1476. if (unlikely((ssize_t)(count|iv->iov_len) < 0))
  1477. return -EINVAL;
  1478. if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
  1479. continue;
  1480. if (seg == 0)
  1481. return -EFAULT;
  1482. nr_segs = seg;
  1483. count -= iv->iov_len; /* This segment is no good */
  1484. break;
  1485. }
  1486. retval = 0;
  1487. if (count) {
  1488. for (seg = 0; seg < nr_segs; seg++) {
  1489. read_descriptor_t desc;
  1490. desc.written = 0;
  1491. desc.arg.buf = iov[seg].iov_base;
  1492. desc.count = iov[seg].iov_len;
  1493. if (desc.count == 0)
  1494. continue;
  1495. desc.error = 0;
  1496. do_generic_file_read(filp, ppos, &desc,
  1497. btrfs_read_actor);
  1498. retval += desc.written;
  1499. if (desc.error) {
  1500. retval = retval ?: desc.error;
  1501. break;
  1502. }
  1503. }
  1504. }
  1505. return retval;
  1506. }
  1507. static struct kmem_cache *btrfs_inode_cachep;
  1508. struct kmem_cache *btrfs_trans_handle_cachep;
  1509. struct kmem_cache *btrfs_transaction_cachep;
  1510. struct kmem_cache *btrfs_bit_radix_cachep;
  1511. struct kmem_cache *btrfs_path_cachep;
  1512. /*
  1513. * Called inside transaction, so use GFP_NOFS
  1514. */
  1515. static struct inode *btrfs_alloc_inode(struct super_block *sb)
  1516. {
  1517. struct btrfs_inode *ei;
  1518. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  1519. if (!ei)
  1520. return NULL;
  1521. ei->magic = 0xDEADBEEF;
  1522. ei->magic2 = 0xDEADBEAF;
  1523. return &ei->vfs_inode;
  1524. }
  1525. static void btrfs_destroy_inode(struct inode *inode)
  1526. {
  1527. struct btrfs_inode *ei = BTRFS_I(inode);
  1528. WARN_ON(ei->magic != 0xDEADBEEF);
  1529. WARN_ON(ei->magic2 != 0xDEADBEAF);
  1530. WARN_ON(!list_empty(&inode->i_dentry));
  1531. WARN_ON(inode->i_data.nrpages);
  1532. ei->magic = 0;
  1533. ei->magic2 = 0;
  1534. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  1535. }
  1536. static void init_once(void * foo, struct kmem_cache * cachep,
  1537. unsigned long flags)
  1538. {
  1539. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  1540. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  1541. SLAB_CTOR_CONSTRUCTOR) {
  1542. inode_init_once(&ei->vfs_inode);
  1543. }
  1544. }
  1545. static int init_inodecache(void)
  1546. {
  1547. btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
  1548. sizeof(struct btrfs_inode),
  1549. 0, (SLAB_RECLAIM_ACCOUNT|
  1550. SLAB_MEM_SPREAD),
  1551. init_once, NULL);
  1552. btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
  1553. sizeof(struct btrfs_trans_handle),
  1554. 0, (SLAB_RECLAIM_ACCOUNT|
  1555. SLAB_MEM_SPREAD),
  1556. NULL, NULL);
  1557. btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
  1558. sizeof(struct btrfs_transaction),
  1559. 0, (SLAB_RECLAIM_ACCOUNT|
  1560. SLAB_MEM_SPREAD),
  1561. NULL, NULL);
  1562. btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
  1563. sizeof(struct btrfs_transaction),
  1564. 0, (SLAB_RECLAIM_ACCOUNT|
  1565. SLAB_MEM_SPREAD),
  1566. NULL, NULL);
  1567. btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
  1568. 256,
  1569. 0, (SLAB_RECLAIM_ACCOUNT|
  1570. SLAB_MEM_SPREAD |
  1571. SLAB_DESTROY_BY_RCU),
  1572. NULL, NULL);
  1573. if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
  1574. btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
  1575. return -ENOMEM;
  1576. return 0;
  1577. }
  1578. static void destroy_inodecache(void)
  1579. {
  1580. kmem_cache_destroy(btrfs_inode_cachep);
  1581. kmem_cache_destroy(btrfs_trans_handle_cachep);
  1582. kmem_cache_destroy(btrfs_transaction_cachep);
  1583. kmem_cache_destroy(btrfs_bit_radix_cachep);
  1584. kmem_cache_destroy(btrfs_path_cachep);
  1585. }
  1586. static int btrfs_get_sb(struct file_system_type *fs_type,
  1587. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  1588. {
  1589. return get_sb_bdev(fs_type, flags, dev_name, data,
  1590. btrfs_fill_super, mnt);
  1591. }
  1592. static struct file_system_type btrfs_fs_type = {
  1593. .owner = THIS_MODULE,
  1594. .name = "btrfs",
  1595. .get_sb = btrfs_get_sb,
  1596. .kill_sb = kill_block_super,
  1597. .fs_flags = FS_REQUIRES_DEV,
  1598. };
  1599. static struct super_operations btrfs_super_ops = {
  1600. .statfs = simple_statfs,
  1601. .delete_inode = btrfs_delete_inode,
  1602. .put_super = btrfs_put_super,
  1603. .read_inode = btrfs_read_locked_inode,
  1604. .write_super = btrfs_write_super,
  1605. .sync_fs = btrfs_sync_fs,
  1606. .write_inode = btrfs_write_inode,
  1607. .alloc_inode = btrfs_alloc_inode,
  1608. .destroy_inode = btrfs_destroy_inode,
  1609. };
  1610. static struct inode_operations btrfs_dir_inode_operations = {
  1611. .lookup = btrfs_lookup,
  1612. .create = btrfs_create,
  1613. .unlink = btrfs_unlink,
  1614. .mkdir = btrfs_mkdir,
  1615. .rmdir = btrfs_rmdir,
  1616. };
  1617. static struct inode_operations btrfs_dir_ro_inode_operations = {
  1618. .lookup = btrfs_lookup,
  1619. };
  1620. static struct file_operations btrfs_dir_file_operations = {
  1621. .llseek = generic_file_llseek,
  1622. .read = generic_read_dir,
  1623. .readdir = btrfs_readdir,
  1624. };
  1625. static struct address_space_operations btrfs_aops = {
  1626. .readpage = btrfs_readpage,
  1627. .readpages = btrfs_readpages,
  1628. .writepage = btrfs_writepage,
  1629. .sync_page = block_sync_page,
  1630. .prepare_write = btrfs_prepare_write,
  1631. .commit_write = btrfs_commit_write,
  1632. };
  1633. static struct inode_operations btrfs_file_inode_operations = {
  1634. .truncate = btrfs_truncate,
  1635. };
  1636. static struct file_operations btrfs_file_operations = {
  1637. .llseek = generic_file_llseek,
  1638. .read = do_sync_read,
  1639. .aio_read = btrfs_file_aio_read,
  1640. .write = btrfs_file_write,
  1641. .mmap = generic_file_mmap,
  1642. .open = generic_file_open,
  1643. };
  1644. static int __init init_btrfs_fs(void)
  1645. {
  1646. int err;
  1647. printk("btrfs loaded!\n");
  1648. err = init_inodecache();
  1649. if (err)
  1650. return err;
  1651. kset_set_kset_s(&btrfs_subsys, fs_subsys);
  1652. err = subsystem_register(&btrfs_subsys);
  1653. if (err)
  1654. goto out;
  1655. return register_filesystem(&btrfs_fs_type);
  1656. out:
  1657. destroy_inodecache();
  1658. return err;
  1659. }
  1660. static void __exit exit_btrfs_fs(void)
  1661. {
  1662. destroy_inodecache();
  1663. unregister_filesystem(&btrfs_fs_type);
  1664. subsystem_unregister(&btrfs_subsys);
  1665. printk("btrfs unloaded\n");
  1666. }
  1667. module_init(init_btrfs_fs)
  1668. module_exit(exit_btrfs_fs)
  1669. MODULE_LICENSE("GPL");