fs.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392
  1. // SPDX-License-Identifier: GPL-2.0
  2. #ifndef NO_BCACHEFS_FS
  3. #include "bcachefs.h"
  4. #include "acl.h"
  5. #include "bkey_buf.h"
  6. #include "btree_update.h"
  7. #include "buckets.h"
  8. #include "chardev.h"
  9. #include "dirent.h"
  10. #include "errcode.h"
  11. #include "extents.h"
  12. #include "fs.h"
  13. #include "fs-common.h"
  14. #include "fs-io.h"
  15. #include "fs-ioctl.h"
  16. #include "fs-io-buffered.h"
  17. #include "fs-io-direct.h"
  18. #include "fs-io-pagecache.h"
  19. #include "fsck.h"
  20. #include "inode.h"
  21. #include "io_read.h"
  22. #include "journal.h"
  23. #include "keylist.h"
  24. #include "quota.h"
  25. #include "snapshot.h"
  26. #include "super.h"
  27. #include "xattr.h"
  28. #include "trace.h"
  29. #include <linux/aio.h>
  30. #include <linux/backing-dev.h>
  31. #include <linux/exportfs.h>
  32. #include <linux/fiemap.h>
  33. #include <linux/fs_context.h>
  34. #include <linux/module.h>
  35. #include <linux/pagemap.h>
  36. #include <linux/posix_acl.h>
  37. #include <linux/random.h>
  38. #include <linux/seq_file.h>
  39. #include <linux/statfs.h>
  40. #include <linux/string.h>
  41. #include <linux/xattr.h>
  42. static struct kmem_cache *bch2_inode_cache;
  43. static void bch2_vfs_inode_init(struct btree_trans *, subvol_inum,
  44. struct bch_inode_info *,
  45. struct bch_inode_unpacked *,
  46. struct bch_subvolume *);
  47. void bch2_inode_update_after_write(struct btree_trans *trans,
  48. struct bch_inode_info *inode,
  49. struct bch_inode_unpacked *bi,
  50. unsigned fields)
  51. {
  52. struct bch_fs *c = trans->c;
  53. BUG_ON(bi->bi_inum != inode->v.i_ino);
  54. bch2_assert_pos_locked(trans, BTREE_ID_inodes, POS(0, bi->bi_inum));
  55. set_nlink(&inode->v, bch2_inode_nlink_get(bi));
  56. i_uid_write(&inode->v, bi->bi_uid);
  57. i_gid_write(&inode->v, bi->bi_gid);
  58. inode->v.i_mode = bi->bi_mode;
  59. if (fields & ATTR_ATIME)
  60. inode_set_atime_to_ts(&inode->v, bch2_time_to_timespec(c, bi->bi_atime));
  61. if (fields & ATTR_MTIME)
  62. inode_set_mtime_to_ts(&inode->v, bch2_time_to_timespec(c, bi->bi_mtime));
  63. if (fields & ATTR_CTIME)
  64. inode_set_ctime_to_ts(&inode->v, bch2_time_to_timespec(c, bi->bi_ctime));
  65. inode->ei_inode = *bi;
  66. bch2_inode_flags_to_vfs(inode);
  67. }
  68. int __must_check bch2_write_inode(struct bch_fs *c,
  69. struct bch_inode_info *inode,
  70. inode_set_fn set,
  71. void *p, unsigned fields)
  72. {
  73. struct btree_trans *trans = bch2_trans_get(c);
  74. struct btree_iter iter = { NULL };
  75. struct bch_inode_unpacked inode_u;
  76. int ret;
  77. retry:
  78. bch2_trans_begin(trans);
  79. ret = bch2_inode_peek(trans, &iter, &inode_u, inode_inum(inode),
  80. BTREE_ITER_intent) ?:
  81. (set ? set(trans, inode, &inode_u, p) : 0) ?:
  82. bch2_inode_write(trans, &iter, &inode_u) ?:
  83. bch2_trans_commit(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc);
  84. /*
  85. * the btree node lock protects inode->ei_inode, not ei_update_lock;
  86. * this is important for inode updates via bchfs_write_index_update
  87. */
  88. if (!ret)
  89. bch2_inode_update_after_write(trans, inode, &inode_u, fields);
  90. bch2_trans_iter_exit(trans, &iter);
  91. if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
  92. goto retry;
  93. bch2_fs_fatal_err_on(bch2_err_matches(ret, ENOENT), c,
  94. "%s: inode %llu:%llu not found when updating",
  95. bch2_err_str(ret),
  96. inode_inum(inode).subvol,
  97. inode_inum(inode).inum);
  98. bch2_trans_put(trans);
  99. return ret < 0 ? ret : 0;
  100. }
  101. int bch2_fs_quota_transfer(struct bch_fs *c,
  102. struct bch_inode_info *inode,
  103. struct bch_qid new_qid,
  104. unsigned qtypes,
  105. enum quota_acct_mode mode)
  106. {
  107. unsigned i;
  108. int ret;
  109. qtypes &= enabled_qtypes(c);
  110. for (i = 0; i < QTYP_NR; i++)
  111. if (new_qid.q[i] == inode->ei_qid.q[i])
  112. qtypes &= ~(1U << i);
  113. if (!qtypes)
  114. return 0;
  115. mutex_lock(&inode->ei_quota_lock);
  116. ret = bch2_quota_transfer(c, qtypes, new_qid,
  117. inode->ei_qid,
  118. inode->v.i_blocks +
  119. inode->ei_quota_reserved,
  120. mode);
  121. if (!ret)
  122. for (i = 0; i < QTYP_NR; i++)
  123. if (qtypes & (1 << i))
  124. inode->ei_qid.q[i] = new_qid.q[i];
  125. mutex_unlock(&inode->ei_quota_lock);
  126. return ret;
  127. }
  128. static bool subvol_inum_eq(subvol_inum a, subvol_inum b)
  129. {
  130. return a.subvol == b.subvol && a.inum == b.inum;
  131. }
  132. static u32 bch2_vfs_inode_hash_fn(const void *data, u32 len, u32 seed)
  133. {
  134. const subvol_inum *inum = data;
  135. return jhash(&inum->inum, sizeof(inum->inum), seed);
  136. }
  137. static u32 bch2_vfs_inode_obj_hash_fn(const void *data, u32 len, u32 seed)
  138. {
  139. const struct bch_inode_info *inode = data;
  140. return bch2_vfs_inode_hash_fn(&inode->ei_inum, sizeof(inode->ei_inum), seed);
  141. }
  142. static int bch2_vfs_inode_cmp_fn(struct rhashtable_compare_arg *arg,
  143. const void *obj)
  144. {
  145. const struct bch_inode_info *inode = obj;
  146. const subvol_inum *v = arg->key;
  147. return !subvol_inum_eq(inode->ei_inum, *v);
  148. }
  149. static const struct rhashtable_params bch2_vfs_inodes_params = {
  150. .head_offset = offsetof(struct bch_inode_info, hash),
  151. .key_offset = offsetof(struct bch_inode_info, ei_inum),
  152. .key_len = sizeof(subvol_inum),
  153. .hashfn = bch2_vfs_inode_hash_fn,
  154. .obj_hashfn = bch2_vfs_inode_obj_hash_fn,
  155. .obj_cmpfn = bch2_vfs_inode_cmp_fn,
  156. .automatic_shrinking = true,
  157. };
  158. int bch2_inode_or_descendents_is_open(struct btree_trans *trans, struct bpos p)
  159. {
  160. struct bch_fs *c = trans->c;
  161. struct rhashtable *ht = &c->vfs_inodes_table;
  162. subvol_inum inum = (subvol_inum) { .inum = p.offset };
  163. DARRAY(u32) subvols;
  164. int ret = 0;
  165. if (!test_bit(BCH_FS_started, &c->flags))
  166. return false;
  167. darray_init(&subvols);
  168. restart_from_top:
  169. /*
  170. * Tweaked version of __rhashtable_lookup(); we need to get a list of
  171. * subvolumes in which the given inode number is open.
  172. *
  173. * For this to work, we don't include the subvolume ID in the key that
  174. * we hash - all inodes with the same inode number regardless of
  175. * subvolume will hash to the same slot.
  176. *
  177. * This will be less than ideal if the same file is ever open
  178. * simultaneously in many different snapshots:
  179. */
  180. rcu_read_lock();
  181. struct rhash_lock_head __rcu *const *bkt;
  182. struct rhash_head *he;
  183. unsigned int hash;
  184. struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
  185. restart:
  186. hash = rht_key_hashfn(ht, tbl, &inum, bch2_vfs_inodes_params);
  187. bkt = rht_bucket(tbl, hash);
  188. do {
  189. struct bch_inode_info *inode;
  190. rht_for_each_entry_rcu_from(inode, he, rht_ptr_rcu(bkt), tbl, hash, hash) {
  191. if (inode->ei_inum.inum == inum.inum) {
  192. ret = darray_push_gfp(&subvols, inode->ei_inum.subvol,
  193. GFP_NOWAIT|__GFP_NOWARN);
  194. if (ret) {
  195. rcu_read_unlock();
  196. ret = darray_make_room(&subvols, 1);
  197. if (ret)
  198. goto err;
  199. subvols.nr = 0;
  200. goto restart_from_top;
  201. }
  202. }
  203. }
  204. /* An object might have been moved to a different hash chain,
  205. * while we walk along it - better check and retry.
  206. */
  207. } while (he != RHT_NULLS_MARKER(bkt));
  208. /* Ensure we see any new tables. */
  209. smp_rmb();
  210. tbl = rht_dereference_rcu(tbl->future_tbl, ht);
  211. if (unlikely(tbl))
  212. goto restart;
  213. rcu_read_unlock();
  214. darray_for_each(subvols, i) {
  215. u32 snap;
  216. ret = bch2_subvolume_get_snapshot(trans, *i, &snap);
  217. if (ret)
  218. goto err;
  219. ret = bch2_snapshot_is_ancestor(c, snap, p.snapshot);
  220. if (ret)
  221. break;
  222. }
  223. err:
  224. darray_exit(&subvols);
  225. return ret;
  226. }
  227. static struct bch_inode_info *__bch2_inode_hash_find(struct bch_fs *c, subvol_inum inum)
  228. {
  229. return rhashtable_lookup_fast(&c->vfs_inodes_table, &inum, bch2_vfs_inodes_params);
  230. }
  231. static void __wait_on_freeing_inode(struct bch_fs *c,
  232. struct bch_inode_info *inode,
  233. subvol_inum inum)
  234. {
  235. wait_queue_head_t *wq;
  236. struct wait_bit_queue_entry wait;
  237. wq = inode_bit_waitqueue(&wait, &inode->v, __I_NEW);
  238. prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
  239. spin_unlock(&inode->v.i_lock);
  240. if (__bch2_inode_hash_find(c, inum) == inode)
  241. schedule_timeout(HZ * 10);
  242. finish_wait(wq, &wait.wq_entry);
  243. }
  244. static struct bch_inode_info *bch2_inode_hash_find(struct bch_fs *c, struct btree_trans *trans,
  245. subvol_inum inum)
  246. {
  247. struct bch_inode_info *inode;
  248. repeat:
  249. inode = __bch2_inode_hash_find(c, inum);
  250. if (inode) {
  251. spin_lock(&inode->v.i_lock);
  252. if (!test_bit(EI_INODE_HASHED, &inode->ei_flags)) {
  253. spin_unlock(&inode->v.i_lock);
  254. return NULL;
  255. }
  256. if ((inode->v.i_state & (I_FREEING|I_WILL_FREE))) {
  257. if (!trans) {
  258. __wait_on_freeing_inode(c, inode, inum);
  259. } else {
  260. bch2_trans_unlock(trans);
  261. __wait_on_freeing_inode(c, inode, inum);
  262. int ret = bch2_trans_relock(trans);
  263. if (ret)
  264. return ERR_PTR(ret);
  265. }
  266. goto repeat;
  267. }
  268. __iget(&inode->v);
  269. spin_unlock(&inode->v.i_lock);
  270. }
  271. return inode;
  272. }
  273. static void bch2_inode_hash_remove(struct bch_fs *c, struct bch_inode_info *inode)
  274. {
  275. spin_lock(&inode->v.i_lock);
  276. bool remove = test_and_clear_bit(EI_INODE_HASHED, &inode->ei_flags);
  277. spin_unlock(&inode->v.i_lock);
  278. if (remove) {
  279. int ret = rhashtable_remove_fast(&c->vfs_inodes_table,
  280. &inode->hash, bch2_vfs_inodes_params);
  281. BUG_ON(ret);
  282. inode->v.i_hash.pprev = NULL;
  283. /*
  284. * This pairs with the bch2_inode_hash_find() ->
  285. * __wait_on_freeing_inode() path
  286. */
  287. inode_wake_up_bit(&inode->v, __I_NEW);
  288. }
  289. }
  290. static struct bch_inode_info *bch2_inode_hash_insert(struct bch_fs *c,
  291. struct btree_trans *trans,
  292. struct bch_inode_info *inode)
  293. {
  294. struct bch_inode_info *old = inode;
  295. set_bit(EI_INODE_HASHED, &inode->ei_flags);
  296. retry:
  297. if (unlikely(rhashtable_lookup_insert_key(&c->vfs_inodes_table,
  298. &inode->ei_inum,
  299. &inode->hash,
  300. bch2_vfs_inodes_params))) {
  301. old = bch2_inode_hash_find(c, trans, inode->ei_inum);
  302. if (!old)
  303. goto retry;
  304. clear_bit(EI_INODE_HASHED, &inode->ei_flags);
  305. /*
  306. * bcachefs doesn't use I_NEW; we have no use for it since we
  307. * only insert fully created inodes in the inode hash table. But
  308. * discard_new_inode() expects it to be set...
  309. */
  310. inode->v.i_state |= I_NEW;
  311. /*
  312. * We don't want bch2_evict_inode() to delete the inode on disk,
  313. * we just raced and had another inode in cache. Normally new
  314. * inodes don't have nlink == 0 - except tmpfiles do...
  315. */
  316. set_nlink(&inode->v, 1);
  317. discard_new_inode(&inode->v);
  318. return old;
  319. } else {
  320. inode_fake_hash(&inode->v);
  321. inode_sb_list_add(&inode->v);
  322. mutex_lock(&c->vfs_inodes_lock);
  323. list_add(&inode->ei_vfs_inode_list, &c->vfs_inodes_list);
  324. mutex_unlock(&c->vfs_inodes_lock);
  325. return inode;
  326. }
  327. }
  328. #define memalloc_flags_do(_flags, _do) \
  329. ({ \
  330. unsigned _saved_flags = memalloc_flags_save(_flags); \
  331. typeof(_do) _ret = _do; \
  332. memalloc_noreclaim_restore(_saved_flags); \
  333. _ret; \
  334. })
  335. static struct inode *bch2_alloc_inode(struct super_block *sb)
  336. {
  337. BUG();
  338. }
  339. static struct bch_inode_info *__bch2_new_inode(struct bch_fs *c, gfp_t gfp)
  340. {
  341. struct bch_inode_info *inode = alloc_inode_sb(c->vfs_sb,
  342. bch2_inode_cache, gfp);
  343. if (!inode)
  344. return NULL;
  345. inode_init_once(&inode->v);
  346. mutex_init(&inode->ei_update_lock);
  347. two_state_lock_init(&inode->ei_pagecache_lock);
  348. INIT_LIST_HEAD(&inode->ei_vfs_inode_list);
  349. inode->ei_flags = 0;
  350. mutex_init(&inode->ei_quota_lock);
  351. memset(&inode->ei_devs_need_flush, 0, sizeof(inode->ei_devs_need_flush));
  352. if (unlikely(inode_init_always_gfp(c->vfs_sb, &inode->v, gfp))) {
  353. kmem_cache_free(bch2_inode_cache, inode);
  354. return NULL;
  355. }
  356. return inode;
  357. }
  358. /*
  359. * Allocate a new inode, dropping/retaking btree locks if necessary:
  360. */
  361. static struct bch_inode_info *bch2_new_inode(struct btree_trans *trans)
  362. {
  363. struct bch_inode_info *inode = __bch2_new_inode(trans->c, GFP_NOWAIT);
  364. if (unlikely(!inode)) {
  365. int ret = drop_locks_do(trans, (inode = __bch2_new_inode(trans->c, GFP_NOFS)) ? 0 : -ENOMEM);
  366. if (ret && inode) {
  367. __destroy_inode(&inode->v);
  368. kmem_cache_free(bch2_inode_cache, inode);
  369. }
  370. if (ret)
  371. return ERR_PTR(ret);
  372. }
  373. return inode;
  374. }
  375. static struct bch_inode_info *bch2_inode_hash_init_insert(struct btree_trans *trans,
  376. subvol_inum inum,
  377. struct bch_inode_unpacked *bi,
  378. struct bch_subvolume *subvol)
  379. {
  380. struct bch_inode_info *inode = bch2_new_inode(trans);
  381. if (IS_ERR(inode))
  382. return inode;
  383. bch2_vfs_inode_init(trans, inum, inode, bi, subvol);
  384. return bch2_inode_hash_insert(trans->c, trans, inode);
  385. }
  386. struct inode *bch2_vfs_inode_get(struct bch_fs *c, subvol_inum inum)
  387. {
  388. struct bch_inode_info *inode = bch2_inode_hash_find(c, NULL, inum);
  389. if (inode)
  390. return &inode->v;
  391. struct btree_trans *trans = bch2_trans_get(c);
  392. struct bch_inode_unpacked inode_u;
  393. struct bch_subvolume subvol;
  394. int ret = lockrestart_do(trans,
  395. bch2_subvolume_get(trans, inum.subvol, true, 0, &subvol) ?:
  396. bch2_inode_find_by_inum_trans(trans, inum, &inode_u)) ?:
  397. PTR_ERR_OR_ZERO(inode = bch2_inode_hash_init_insert(trans, inum, &inode_u, &subvol));
  398. bch2_trans_put(trans);
  399. return ret ? ERR_PTR(ret) : &inode->v;
  400. }
  401. struct bch_inode_info *
  402. __bch2_create(struct mnt_idmap *idmap,
  403. struct bch_inode_info *dir, struct dentry *dentry,
  404. umode_t mode, dev_t rdev, subvol_inum snapshot_src,
  405. unsigned flags)
  406. {
  407. struct bch_fs *c = dir->v.i_sb->s_fs_info;
  408. struct btree_trans *trans;
  409. struct bch_inode_unpacked dir_u;
  410. struct bch_inode_info *inode;
  411. struct bch_inode_unpacked inode_u;
  412. struct posix_acl *default_acl = NULL, *acl = NULL;
  413. subvol_inum inum;
  414. struct bch_subvolume subvol;
  415. u64 journal_seq = 0;
  416. kuid_t kuid;
  417. kgid_t kgid;
  418. int ret;
  419. /*
  420. * preallocate acls + vfs inode before btree transaction, so that
  421. * nothing can fail after the transaction succeeds:
  422. */
  423. #ifdef CONFIG_BCACHEFS_POSIX_ACL
  424. ret = posix_acl_create(&dir->v, &mode, &default_acl, &acl);
  425. if (ret)
  426. return ERR_PTR(ret);
  427. #endif
  428. inode = __bch2_new_inode(c, GFP_NOFS);
  429. if (unlikely(!inode)) {
  430. inode = ERR_PTR(-ENOMEM);
  431. goto err;
  432. }
  433. bch2_inode_init_early(c, &inode_u);
  434. if (!(flags & BCH_CREATE_TMPFILE))
  435. mutex_lock(&dir->ei_update_lock);
  436. trans = bch2_trans_get(c);
  437. retry:
  438. bch2_trans_begin(trans);
  439. kuid = mapped_fsuid(idmap, i_user_ns(&dir->v));
  440. kgid = mapped_fsgid(idmap, i_user_ns(&dir->v));
  441. ret = bch2_subvol_is_ro_trans(trans, dir->ei_inum.subvol) ?:
  442. bch2_create_trans(trans,
  443. inode_inum(dir), &dir_u, &inode_u,
  444. !(flags & BCH_CREATE_TMPFILE)
  445. ? &dentry->d_name : NULL,
  446. from_kuid(i_user_ns(&dir->v), kuid),
  447. from_kgid(i_user_ns(&dir->v), kgid),
  448. mode, rdev,
  449. default_acl, acl, snapshot_src, flags) ?:
  450. bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, 1,
  451. KEY_TYPE_QUOTA_PREALLOC);
  452. if (unlikely(ret))
  453. goto err_before_quota;
  454. inum.subvol = inode_u.bi_subvol ?: dir->ei_inum.subvol;
  455. inum.inum = inode_u.bi_inum;
  456. ret = bch2_subvolume_get(trans, inum.subvol, true,
  457. BTREE_ITER_with_updates, &subvol) ?:
  458. bch2_trans_commit(trans, NULL, &journal_seq, 0);
  459. if (unlikely(ret)) {
  460. bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, -1,
  461. KEY_TYPE_QUOTA_WARN);
  462. err_before_quota:
  463. if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
  464. goto retry;
  465. goto err_trans;
  466. }
  467. if (!(flags & BCH_CREATE_TMPFILE)) {
  468. bch2_inode_update_after_write(trans, dir, &dir_u,
  469. ATTR_MTIME|ATTR_CTIME);
  470. mutex_unlock(&dir->ei_update_lock);
  471. }
  472. bch2_vfs_inode_init(trans, inum, inode, &inode_u, &subvol);
  473. set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
  474. set_cached_acl(&inode->v, ACL_TYPE_DEFAULT, default_acl);
  475. /*
  476. * we must insert the new inode into the inode cache before calling
  477. * bch2_trans_exit() and dropping locks, else we could race with another
  478. * thread pulling the inode in and modifying it:
  479. *
  480. * also, calling bch2_inode_hash_insert() without passing in the
  481. * transaction object is sketchy - if we could ever end up in
  482. * __wait_on_freeing_inode(), we'd risk deadlock.
  483. *
  484. * But that shouldn't be possible, since we still have the inode locked
  485. * that we just created, and we _really_ can't take a transaction
  486. * restart here.
  487. */
  488. inode = bch2_inode_hash_insert(c, NULL, inode);
  489. bch2_trans_put(trans);
  490. err:
  491. posix_acl_release(default_acl);
  492. posix_acl_release(acl);
  493. return inode;
  494. err_trans:
  495. if (!(flags & BCH_CREATE_TMPFILE))
  496. mutex_unlock(&dir->ei_update_lock);
  497. bch2_trans_put(trans);
  498. make_bad_inode(&inode->v);
  499. iput(&inode->v);
  500. inode = ERR_PTR(ret);
  501. goto err;
  502. }
  503. /* methods */
  504. static struct bch_inode_info *bch2_lookup_trans(struct btree_trans *trans,
  505. subvol_inum dir, struct bch_hash_info *dir_hash_info,
  506. const struct qstr *name)
  507. {
  508. struct bch_fs *c = trans->c;
  509. struct btree_iter dirent_iter = {};
  510. subvol_inum inum = {};
  511. struct printbuf buf = PRINTBUF;
  512. struct bkey_s_c k = bch2_hash_lookup(trans, &dirent_iter, bch2_dirent_hash_desc,
  513. dir_hash_info, dir, name, 0);
  514. int ret = bkey_err(k);
  515. if (ret)
  516. return ERR_PTR(ret);
  517. ret = bch2_dirent_read_target(trans, dir, bkey_s_c_to_dirent(k), &inum);
  518. if (ret > 0)
  519. ret = -ENOENT;
  520. if (ret)
  521. goto err;
  522. struct bch_inode_info *inode = bch2_inode_hash_find(c, trans, inum);
  523. if (inode)
  524. goto out;
  525. struct bch_subvolume subvol;
  526. struct bch_inode_unpacked inode_u;
  527. ret = bch2_subvolume_get(trans, inum.subvol, true, 0, &subvol) ?:
  528. bch2_inode_find_by_inum_nowarn_trans(trans, inum, &inode_u) ?:
  529. PTR_ERR_OR_ZERO(inode = bch2_inode_hash_init_insert(trans, inum, &inode_u, &subvol));
  530. bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT),
  531. c, "dirent to missing inode:\n %s",
  532. (bch2_bkey_val_to_text(&buf, c, k), buf.buf));
  533. if (ret)
  534. goto err;
  535. /* regular files may have hardlinks: */
  536. if (bch2_fs_inconsistent_on(bch2_inode_should_have_bp(&inode_u) &&
  537. !bkey_eq(k.k->p, POS(inode_u.bi_dir, inode_u.bi_dir_offset)),
  538. c,
  539. "dirent points to inode that does not point back:\n %s",
  540. (bch2_bkey_val_to_text(&buf, c, k),
  541. prt_printf(&buf, "\n "),
  542. bch2_inode_unpacked_to_text(&buf, &inode_u),
  543. buf.buf))) {
  544. ret = -ENOENT;
  545. goto err;
  546. }
  547. out:
  548. bch2_trans_iter_exit(trans, &dirent_iter);
  549. printbuf_exit(&buf);
  550. return inode;
  551. err:
  552. inode = ERR_PTR(ret);
  553. goto out;
  554. }
  555. static struct dentry *bch2_lookup(struct inode *vdir, struct dentry *dentry,
  556. unsigned int flags)
  557. {
  558. struct bch_fs *c = vdir->i_sb->s_fs_info;
  559. struct bch_inode_info *dir = to_bch_ei(vdir);
  560. struct bch_hash_info hash = bch2_hash_info_init(c, &dir->ei_inode);
  561. struct bch_inode_info *inode;
  562. bch2_trans_do(c,
  563. PTR_ERR_OR_ZERO(inode = bch2_lookup_trans(trans, inode_inum(dir),
  564. &hash, &dentry->d_name)));
  565. if (IS_ERR(inode))
  566. inode = NULL;
  567. return d_splice_alias(&inode->v, dentry);
  568. }
  569. static int bch2_mknod(struct mnt_idmap *idmap,
  570. struct inode *vdir, struct dentry *dentry,
  571. umode_t mode, dev_t rdev)
  572. {
  573. struct bch_inode_info *inode =
  574. __bch2_create(idmap, to_bch_ei(vdir), dentry, mode, rdev,
  575. (subvol_inum) { 0 }, 0);
  576. if (IS_ERR(inode))
  577. return bch2_err_class(PTR_ERR(inode));
  578. d_instantiate(dentry, &inode->v);
  579. return 0;
  580. }
  581. static int bch2_create(struct mnt_idmap *idmap,
  582. struct inode *vdir, struct dentry *dentry,
  583. umode_t mode, bool excl)
  584. {
  585. return bch2_mknod(idmap, vdir, dentry, mode|S_IFREG, 0);
  586. }
  587. static int __bch2_link(struct bch_fs *c,
  588. struct bch_inode_info *inode,
  589. struct bch_inode_info *dir,
  590. struct dentry *dentry)
  591. {
  592. struct bch_inode_unpacked dir_u, inode_u;
  593. int ret;
  594. mutex_lock(&inode->ei_update_lock);
  595. struct btree_trans *trans = bch2_trans_get(c);
  596. ret = commit_do(trans, NULL, NULL, 0,
  597. bch2_link_trans(trans,
  598. inode_inum(dir), &dir_u,
  599. inode_inum(inode), &inode_u,
  600. &dentry->d_name));
  601. if (likely(!ret)) {
  602. bch2_inode_update_after_write(trans, dir, &dir_u,
  603. ATTR_MTIME|ATTR_CTIME);
  604. bch2_inode_update_after_write(trans, inode, &inode_u, ATTR_CTIME);
  605. }
  606. bch2_trans_put(trans);
  607. mutex_unlock(&inode->ei_update_lock);
  608. return ret;
  609. }
  610. static int bch2_link(struct dentry *old_dentry, struct inode *vdir,
  611. struct dentry *dentry)
  612. {
  613. struct bch_fs *c = vdir->i_sb->s_fs_info;
  614. struct bch_inode_info *dir = to_bch_ei(vdir);
  615. struct bch_inode_info *inode = to_bch_ei(old_dentry->d_inode);
  616. int ret;
  617. lockdep_assert_held(&inode->v.i_rwsem);
  618. ret = bch2_subvol_is_ro(c, dir->ei_inum.subvol) ?:
  619. bch2_subvol_is_ro(c, inode->ei_inum.subvol) ?:
  620. __bch2_link(c, inode, dir, dentry);
  621. if (unlikely(ret))
  622. return bch2_err_class(ret);
  623. ihold(&inode->v);
  624. d_instantiate(dentry, &inode->v);
  625. return 0;
  626. }
  627. int __bch2_unlink(struct inode *vdir, struct dentry *dentry,
  628. bool deleting_snapshot)
  629. {
  630. struct bch_fs *c = vdir->i_sb->s_fs_info;
  631. struct bch_inode_info *dir = to_bch_ei(vdir);
  632. struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
  633. struct bch_inode_unpacked dir_u, inode_u;
  634. int ret;
  635. bch2_lock_inodes(INODE_UPDATE_LOCK, dir, inode);
  636. struct btree_trans *trans = bch2_trans_get(c);
  637. ret = commit_do(trans, NULL, NULL,
  638. BCH_TRANS_COMMIT_no_enospc,
  639. bch2_unlink_trans(trans,
  640. inode_inum(dir), &dir_u,
  641. &inode_u, &dentry->d_name,
  642. deleting_snapshot));
  643. if (unlikely(ret))
  644. goto err;
  645. bch2_inode_update_after_write(trans, dir, &dir_u,
  646. ATTR_MTIME|ATTR_CTIME);
  647. bch2_inode_update_after_write(trans, inode, &inode_u,
  648. ATTR_MTIME);
  649. if (inode_u.bi_subvol) {
  650. /*
  651. * Subvolume deletion is asynchronous, but we still want to tell
  652. * the VFS that it's been deleted here:
  653. */
  654. set_nlink(&inode->v, 0);
  655. }
  656. err:
  657. bch2_trans_put(trans);
  658. bch2_unlock_inodes(INODE_UPDATE_LOCK, dir, inode);
  659. return ret;
  660. }
  661. static int bch2_unlink(struct inode *vdir, struct dentry *dentry)
  662. {
  663. struct bch_inode_info *dir= to_bch_ei(vdir);
  664. struct bch_fs *c = dir->v.i_sb->s_fs_info;
  665. int ret = bch2_subvol_is_ro(c, dir->ei_inum.subvol) ?:
  666. __bch2_unlink(vdir, dentry, false);
  667. return bch2_err_class(ret);
  668. }
  669. static int bch2_symlink(struct mnt_idmap *idmap,
  670. struct inode *vdir, struct dentry *dentry,
  671. const char *symname)
  672. {
  673. struct bch_fs *c = vdir->i_sb->s_fs_info;
  674. struct bch_inode_info *dir = to_bch_ei(vdir), *inode;
  675. int ret;
  676. inode = __bch2_create(idmap, dir, dentry, S_IFLNK|S_IRWXUGO, 0,
  677. (subvol_inum) { 0 }, BCH_CREATE_TMPFILE);
  678. if (IS_ERR(inode))
  679. return bch2_err_class(PTR_ERR(inode));
  680. inode_lock(&inode->v);
  681. ret = page_symlink(&inode->v, symname, strlen(symname) + 1);
  682. inode_unlock(&inode->v);
  683. if (unlikely(ret))
  684. goto err;
  685. ret = filemap_write_and_wait_range(inode->v.i_mapping, 0, LLONG_MAX);
  686. if (unlikely(ret))
  687. goto err;
  688. ret = __bch2_link(c, inode, dir, dentry);
  689. if (unlikely(ret))
  690. goto err;
  691. d_instantiate(dentry, &inode->v);
  692. return 0;
  693. err:
  694. iput(&inode->v);
  695. return bch2_err_class(ret);
  696. }
  697. static int bch2_mkdir(struct mnt_idmap *idmap,
  698. struct inode *vdir, struct dentry *dentry, umode_t mode)
  699. {
  700. return bch2_mknod(idmap, vdir, dentry, mode|S_IFDIR, 0);
  701. }
  702. static int bch2_rename2(struct mnt_idmap *idmap,
  703. struct inode *src_vdir, struct dentry *src_dentry,
  704. struct inode *dst_vdir, struct dentry *dst_dentry,
  705. unsigned flags)
  706. {
  707. struct bch_fs *c = src_vdir->i_sb->s_fs_info;
  708. struct bch_inode_info *src_dir = to_bch_ei(src_vdir);
  709. struct bch_inode_info *dst_dir = to_bch_ei(dst_vdir);
  710. struct bch_inode_info *src_inode = to_bch_ei(src_dentry->d_inode);
  711. struct bch_inode_info *dst_inode = to_bch_ei(dst_dentry->d_inode);
  712. struct bch_inode_unpacked dst_dir_u, src_dir_u;
  713. struct bch_inode_unpacked src_inode_u, dst_inode_u, *whiteout_inode_u;
  714. struct btree_trans *trans;
  715. enum bch_rename_mode mode = flags & RENAME_EXCHANGE
  716. ? BCH_RENAME_EXCHANGE
  717. : dst_dentry->d_inode
  718. ? BCH_RENAME_OVERWRITE : BCH_RENAME;
  719. bool whiteout = !!(flags & RENAME_WHITEOUT);
  720. int ret;
  721. if (flags & ~(RENAME_NOREPLACE|RENAME_EXCHANGE|RENAME_WHITEOUT))
  722. return -EINVAL;
  723. if (mode == BCH_RENAME_OVERWRITE) {
  724. ret = filemap_write_and_wait_range(src_inode->v.i_mapping,
  725. 0, LLONG_MAX);
  726. if (ret)
  727. return ret;
  728. }
  729. bch2_lock_inodes(INODE_UPDATE_LOCK,
  730. src_dir,
  731. dst_dir,
  732. src_inode,
  733. dst_inode);
  734. trans = bch2_trans_get(c);
  735. ret = bch2_subvol_is_ro_trans(trans, src_dir->ei_inum.subvol) ?:
  736. bch2_subvol_is_ro_trans(trans, dst_dir->ei_inum.subvol);
  737. if (ret)
  738. goto err_tx_restart;
  739. if (inode_attr_changing(dst_dir, src_inode, Inode_opt_project)) {
  740. ret = bch2_fs_quota_transfer(c, src_inode,
  741. dst_dir->ei_qid,
  742. 1 << QTYP_PRJ,
  743. KEY_TYPE_QUOTA_PREALLOC);
  744. if (ret)
  745. goto err;
  746. }
  747. if (mode == BCH_RENAME_EXCHANGE &&
  748. inode_attr_changing(src_dir, dst_inode, Inode_opt_project)) {
  749. ret = bch2_fs_quota_transfer(c, dst_inode,
  750. src_dir->ei_qid,
  751. 1 << QTYP_PRJ,
  752. KEY_TYPE_QUOTA_PREALLOC);
  753. if (ret)
  754. goto err;
  755. }
  756. retry:
  757. bch2_trans_begin(trans);
  758. ret = bch2_rename_trans(trans,
  759. inode_inum(src_dir), &src_dir_u,
  760. inode_inum(dst_dir), &dst_dir_u,
  761. &src_inode_u,
  762. &dst_inode_u,
  763. &src_dentry->d_name,
  764. &dst_dentry->d_name,
  765. mode);
  766. if (unlikely(ret))
  767. goto err_tx_restart;
  768. if (whiteout) {
  769. whiteout_inode_u = bch2_trans_kmalloc_nomemzero(trans, sizeof(*whiteout_inode_u));
  770. ret = PTR_ERR_OR_ZERO(whiteout_inode_u);
  771. if (unlikely(ret))
  772. goto err_tx_restart;
  773. bch2_inode_init_early(c, whiteout_inode_u);
  774. ret = bch2_create_trans(trans,
  775. inode_inum(src_dir), &src_dir_u,
  776. whiteout_inode_u,
  777. &src_dentry->d_name,
  778. from_kuid(i_user_ns(&src_dir->v), current_fsuid()),
  779. from_kgid(i_user_ns(&src_dir->v), current_fsgid()),
  780. S_IFCHR|WHITEOUT_MODE, 0,
  781. NULL, NULL, (subvol_inum) { 0 }, 0) ?:
  782. bch2_quota_acct(c, bch_qid(whiteout_inode_u), Q_INO, 1,
  783. KEY_TYPE_QUOTA_PREALLOC);
  784. if (unlikely(ret))
  785. goto err_tx_restart;
  786. }
  787. ret = bch2_trans_commit(trans, NULL, NULL, 0);
  788. if (unlikely(ret)) {
  789. err_tx_restart:
  790. if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
  791. goto retry;
  792. goto err;
  793. }
  794. BUG_ON(src_inode->v.i_ino != src_inode_u.bi_inum);
  795. BUG_ON(dst_inode &&
  796. dst_inode->v.i_ino != dst_inode_u.bi_inum);
  797. bch2_inode_update_after_write(trans, src_dir, &src_dir_u,
  798. ATTR_MTIME|ATTR_CTIME);
  799. if (src_dir != dst_dir)
  800. bch2_inode_update_after_write(trans, dst_dir, &dst_dir_u,
  801. ATTR_MTIME|ATTR_CTIME);
  802. bch2_inode_update_after_write(trans, src_inode, &src_inode_u,
  803. ATTR_CTIME);
  804. if (dst_inode)
  805. bch2_inode_update_after_write(trans, dst_inode, &dst_inode_u,
  806. ATTR_CTIME);
  807. err:
  808. bch2_trans_put(trans);
  809. bch2_fs_quota_transfer(c, src_inode,
  810. bch_qid(&src_inode->ei_inode),
  811. 1 << QTYP_PRJ,
  812. KEY_TYPE_QUOTA_NOCHECK);
  813. if (dst_inode)
  814. bch2_fs_quota_transfer(c, dst_inode,
  815. bch_qid(&dst_inode->ei_inode),
  816. 1 << QTYP_PRJ,
  817. KEY_TYPE_QUOTA_NOCHECK);
  818. bch2_unlock_inodes(INODE_UPDATE_LOCK,
  819. src_dir,
  820. dst_dir,
  821. src_inode,
  822. dst_inode);
  823. return bch2_err_class(ret);
  824. }
  825. static void bch2_setattr_copy(struct mnt_idmap *idmap,
  826. struct bch_inode_info *inode,
  827. struct bch_inode_unpacked *bi,
  828. struct iattr *attr)
  829. {
  830. struct bch_fs *c = inode->v.i_sb->s_fs_info;
  831. unsigned int ia_valid = attr->ia_valid;
  832. kuid_t kuid;
  833. kgid_t kgid;
  834. if (ia_valid & ATTR_UID) {
  835. kuid = from_vfsuid(idmap, i_user_ns(&inode->v), attr->ia_vfsuid);
  836. bi->bi_uid = from_kuid(i_user_ns(&inode->v), kuid);
  837. }
  838. if (ia_valid & ATTR_GID) {
  839. kgid = from_vfsgid(idmap, i_user_ns(&inode->v), attr->ia_vfsgid);
  840. bi->bi_gid = from_kgid(i_user_ns(&inode->v), kgid);
  841. }
  842. if (ia_valid & ATTR_SIZE)
  843. bi->bi_size = attr->ia_size;
  844. if (ia_valid & ATTR_ATIME)
  845. bi->bi_atime = timespec_to_bch2_time(c, attr->ia_atime);
  846. if (ia_valid & ATTR_MTIME)
  847. bi->bi_mtime = timespec_to_bch2_time(c, attr->ia_mtime);
  848. if (ia_valid & ATTR_CTIME)
  849. bi->bi_ctime = timespec_to_bch2_time(c, attr->ia_ctime);
  850. if (ia_valid & ATTR_MODE) {
  851. umode_t mode = attr->ia_mode;
  852. kgid_t gid = ia_valid & ATTR_GID
  853. ? kgid
  854. : inode->v.i_gid;
  855. if (!in_group_or_capable(idmap, &inode->v,
  856. make_vfsgid(idmap, i_user_ns(&inode->v), gid)))
  857. mode &= ~S_ISGID;
  858. bi->bi_mode = mode;
  859. }
  860. }
  861. int bch2_setattr_nonsize(struct mnt_idmap *idmap,
  862. struct bch_inode_info *inode,
  863. struct iattr *attr)
  864. {
  865. struct bch_fs *c = inode->v.i_sb->s_fs_info;
  866. struct bch_qid qid;
  867. struct btree_trans *trans;
  868. struct btree_iter inode_iter = { NULL };
  869. struct bch_inode_unpacked inode_u;
  870. struct posix_acl *acl = NULL;
  871. kuid_t kuid;
  872. kgid_t kgid;
  873. int ret;
  874. mutex_lock(&inode->ei_update_lock);
  875. qid = inode->ei_qid;
  876. if (attr->ia_valid & ATTR_UID) {
  877. kuid = from_vfsuid(idmap, i_user_ns(&inode->v), attr->ia_vfsuid);
  878. qid.q[QTYP_USR] = from_kuid(i_user_ns(&inode->v), kuid);
  879. }
  880. if (attr->ia_valid & ATTR_GID) {
  881. kgid = from_vfsgid(idmap, i_user_ns(&inode->v), attr->ia_vfsgid);
  882. qid.q[QTYP_GRP] = from_kgid(i_user_ns(&inode->v), kgid);
  883. }
  884. ret = bch2_fs_quota_transfer(c, inode, qid, ~0,
  885. KEY_TYPE_QUOTA_PREALLOC);
  886. if (ret)
  887. goto err;
  888. trans = bch2_trans_get(c);
  889. retry:
  890. bch2_trans_begin(trans);
  891. kfree(acl);
  892. acl = NULL;
  893. ret = bch2_inode_peek(trans, &inode_iter, &inode_u, inode_inum(inode),
  894. BTREE_ITER_intent);
  895. if (ret)
  896. goto btree_err;
  897. bch2_setattr_copy(idmap, inode, &inode_u, attr);
  898. if (attr->ia_valid & ATTR_MODE) {
  899. ret = bch2_acl_chmod(trans, inode_inum(inode), &inode_u,
  900. inode_u.bi_mode, &acl);
  901. if (ret)
  902. goto btree_err;
  903. }
  904. ret = bch2_inode_write(trans, &inode_iter, &inode_u) ?:
  905. bch2_trans_commit(trans, NULL, NULL,
  906. BCH_TRANS_COMMIT_no_enospc);
  907. btree_err:
  908. bch2_trans_iter_exit(trans, &inode_iter);
  909. if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
  910. goto retry;
  911. if (unlikely(ret))
  912. goto err_trans;
  913. bch2_inode_update_after_write(trans, inode, &inode_u, attr->ia_valid);
  914. if (acl)
  915. set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
  916. err_trans:
  917. bch2_trans_put(trans);
  918. err:
  919. mutex_unlock(&inode->ei_update_lock);
  920. return bch2_err_class(ret);
  921. }
  922. static int bch2_getattr(struct mnt_idmap *idmap,
  923. const struct path *path, struct kstat *stat,
  924. u32 request_mask, unsigned query_flags)
  925. {
  926. struct bch_inode_info *inode = to_bch_ei(d_inode(path->dentry));
  927. struct bch_fs *c = inode->v.i_sb->s_fs_info;
  928. vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, &inode->v);
  929. vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, &inode->v);
  930. stat->dev = inode->v.i_sb->s_dev;
  931. stat->ino = inode->v.i_ino;
  932. stat->mode = inode->v.i_mode;
  933. stat->nlink = inode->v.i_nlink;
  934. stat->uid = vfsuid_into_kuid(vfsuid);
  935. stat->gid = vfsgid_into_kgid(vfsgid);
  936. stat->rdev = inode->v.i_rdev;
  937. stat->size = i_size_read(&inode->v);
  938. stat->atime = inode_get_atime(&inode->v);
  939. stat->mtime = inode_get_mtime(&inode->v);
  940. stat->ctime = inode_get_ctime(&inode->v);
  941. stat->blksize = block_bytes(c);
  942. stat->blocks = inode->v.i_blocks;
  943. stat->subvol = inode->ei_inum.subvol;
  944. stat->result_mask |= STATX_SUBVOL;
  945. if ((request_mask & STATX_DIOALIGN) && S_ISREG(inode->v.i_mode)) {
  946. stat->result_mask |= STATX_DIOALIGN;
  947. /*
  948. * this is incorrect; we should be tracking this in superblock,
  949. * and checking the alignment of open devices
  950. */
  951. stat->dio_mem_align = SECTOR_SIZE;
  952. stat->dio_offset_align = block_bytes(c);
  953. }
  954. if (request_mask & STATX_BTIME) {
  955. stat->result_mask |= STATX_BTIME;
  956. stat->btime = bch2_time_to_timespec(c, inode->ei_inode.bi_otime);
  957. }
  958. if (inode->ei_inode.bi_flags & BCH_INODE_immutable)
  959. stat->attributes |= STATX_ATTR_IMMUTABLE;
  960. stat->attributes_mask |= STATX_ATTR_IMMUTABLE;
  961. if (inode->ei_inode.bi_flags & BCH_INODE_append)
  962. stat->attributes |= STATX_ATTR_APPEND;
  963. stat->attributes_mask |= STATX_ATTR_APPEND;
  964. if (inode->ei_inode.bi_flags & BCH_INODE_nodump)
  965. stat->attributes |= STATX_ATTR_NODUMP;
  966. stat->attributes_mask |= STATX_ATTR_NODUMP;
  967. return 0;
  968. }
  969. static int bch2_setattr(struct mnt_idmap *idmap,
  970. struct dentry *dentry, struct iattr *iattr)
  971. {
  972. struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
  973. struct bch_fs *c = inode->v.i_sb->s_fs_info;
  974. int ret;
  975. lockdep_assert_held(&inode->v.i_rwsem);
  976. ret = bch2_subvol_is_ro(c, inode->ei_inum.subvol) ?:
  977. setattr_prepare(idmap, dentry, iattr);
  978. if (ret)
  979. return ret;
  980. return iattr->ia_valid & ATTR_SIZE
  981. ? bchfs_truncate(idmap, inode, iattr)
  982. : bch2_setattr_nonsize(idmap, inode, iattr);
  983. }
  984. static int bch2_tmpfile(struct mnt_idmap *idmap,
  985. struct inode *vdir, struct file *file, umode_t mode)
  986. {
  987. struct bch_inode_info *inode =
  988. __bch2_create(idmap, to_bch_ei(vdir),
  989. file->f_path.dentry, mode, 0,
  990. (subvol_inum) { 0 }, BCH_CREATE_TMPFILE);
  991. if (IS_ERR(inode))
  992. return bch2_err_class(PTR_ERR(inode));
  993. d_mark_tmpfile(file, &inode->v);
  994. d_instantiate(file->f_path.dentry, &inode->v);
  995. return finish_open_simple(file, 0);
  996. }
  997. static int bch2_fill_extent(struct bch_fs *c,
  998. struct fiemap_extent_info *info,
  999. struct bkey_s_c k, unsigned flags)
  1000. {
  1001. if (bkey_extent_is_direct_data(k.k)) {
  1002. struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
  1003. const union bch_extent_entry *entry;
  1004. struct extent_ptr_decoded p;
  1005. int ret;
  1006. if (k.k->type == KEY_TYPE_reflink_v)
  1007. flags |= FIEMAP_EXTENT_SHARED;
  1008. bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
  1009. int flags2 = 0;
  1010. u64 offset = p.ptr.offset;
  1011. if (p.ptr.unwritten)
  1012. flags2 |= FIEMAP_EXTENT_UNWRITTEN;
  1013. if (p.crc.compression_type)
  1014. flags2 |= FIEMAP_EXTENT_ENCODED;
  1015. else
  1016. offset += p.crc.offset;
  1017. if ((offset & (block_sectors(c) - 1)) ||
  1018. (k.k->size & (block_sectors(c) - 1)))
  1019. flags2 |= FIEMAP_EXTENT_NOT_ALIGNED;
  1020. ret = fiemap_fill_next_extent(info,
  1021. bkey_start_offset(k.k) << 9,
  1022. offset << 9,
  1023. k.k->size << 9, flags|flags2);
  1024. if (ret)
  1025. return ret;
  1026. }
  1027. return 0;
  1028. } else if (bkey_extent_is_inline_data(k.k)) {
  1029. return fiemap_fill_next_extent(info,
  1030. bkey_start_offset(k.k) << 9,
  1031. 0, k.k->size << 9,
  1032. flags|
  1033. FIEMAP_EXTENT_DATA_INLINE);
  1034. } else if (k.k->type == KEY_TYPE_reservation) {
  1035. return fiemap_fill_next_extent(info,
  1036. bkey_start_offset(k.k) << 9,
  1037. 0, k.k->size << 9,
  1038. flags|
  1039. FIEMAP_EXTENT_DELALLOC|
  1040. FIEMAP_EXTENT_UNWRITTEN);
  1041. } else {
  1042. BUG();
  1043. }
  1044. }
  1045. static int bch2_fiemap(struct inode *vinode, struct fiemap_extent_info *info,
  1046. u64 start, u64 len)
  1047. {
  1048. struct bch_fs *c = vinode->i_sb->s_fs_info;
  1049. struct bch_inode_info *ei = to_bch_ei(vinode);
  1050. struct btree_trans *trans;
  1051. struct btree_iter iter;
  1052. struct bkey_s_c k;
  1053. struct bkey_buf cur, prev;
  1054. unsigned offset_into_extent, sectors;
  1055. bool have_extent = false;
  1056. int ret = 0;
  1057. ret = fiemap_prep(&ei->v, info, start, &len, FIEMAP_FLAG_SYNC);
  1058. if (ret)
  1059. return ret;
  1060. struct bpos end = POS(ei->v.i_ino, (start + len) >> 9);
  1061. if (start + len < start)
  1062. return -EINVAL;
  1063. start >>= 9;
  1064. bch2_bkey_buf_init(&cur);
  1065. bch2_bkey_buf_init(&prev);
  1066. trans = bch2_trans_get(c);
  1067. bch2_trans_iter_init(trans, &iter, BTREE_ID_extents,
  1068. POS(ei->v.i_ino, start), 0);
  1069. while (!ret || bch2_err_matches(ret, BCH_ERR_transaction_restart)) {
  1070. enum btree_id data_btree = BTREE_ID_extents;
  1071. bch2_trans_begin(trans);
  1072. u32 snapshot;
  1073. ret = bch2_subvolume_get_snapshot(trans, ei->ei_inum.subvol, &snapshot);
  1074. if (ret)
  1075. continue;
  1076. bch2_btree_iter_set_snapshot(&iter, snapshot);
  1077. k = bch2_btree_iter_peek_upto(&iter, end);
  1078. ret = bkey_err(k);
  1079. if (ret)
  1080. continue;
  1081. if (!k.k)
  1082. break;
  1083. if (!bkey_extent_is_data(k.k) &&
  1084. k.k->type != KEY_TYPE_reservation) {
  1085. bch2_btree_iter_advance(&iter);
  1086. continue;
  1087. }
  1088. offset_into_extent = iter.pos.offset -
  1089. bkey_start_offset(k.k);
  1090. sectors = k.k->size - offset_into_extent;
  1091. bch2_bkey_buf_reassemble(&cur, c, k);
  1092. ret = bch2_read_indirect_extent(trans, &data_btree,
  1093. &offset_into_extent, &cur);
  1094. if (ret)
  1095. continue;
  1096. k = bkey_i_to_s_c(cur.k);
  1097. bch2_bkey_buf_realloc(&prev, c, k.k->u64s);
  1098. sectors = min(sectors, k.k->size - offset_into_extent);
  1099. bch2_cut_front(POS(k.k->p.inode,
  1100. bkey_start_offset(k.k) +
  1101. offset_into_extent),
  1102. cur.k);
  1103. bch2_key_resize(&cur.k->k, sectors);
  1104. cur.k->k.p = iter.pos;
  1105. cur.k->k.p.offset += cur.k->k.size;
  1106. if (have_extent) {
  1107. bch2_trans_unlock(trans);
  1108. ret = bch2_fill_extent(c, info,
  1109. bkey_i_to_s_c(prev.k), 0);
  1110. if (ret)
  1111. break;
  1112. }
  1113. bkey_copy(prev.k, cur.k);
  1114. have_extent = true;
  1115. bch2_btree_iter_set_pos(&iter,
  1116. POS(iter.pos.inode, iter.pos.offset + sectors));
  1117. }
  1118. bch2_trans_iter_exit(trans, &iter);
  1119. if (!ret && have_extent) {
  1120. bch2_trans_unlock(trans);
  1121. ret = bch2_fill_extent(c, info, bkey_i_to_s_c(prev.k),
  1122. FIEMAP_EXTENT_LAST);
  1123. }
  1124. bch2_trans_put(trans);
  1125. bch2_bkey_buf_exit(&cur, c);
  1126. bch2_bkey_buf_exit(&prev, c);
  1127. return ret < 0 ? ret : 0;
  1128. }
  1129. static const struct vm_operations_struct bch_vm_ops = {
  1130. .fault = bch2_page_fault,
  1131. .map_pages = filemap_map_pages,
  1132. .page_mkwrite = bch2_page_mkwrite,
  1133. };
  1134. static int bch2_mmap(struct file *file, struct vm_area_struct *vma)
  1135. {
  1136. file_accessed(file);
  1137. vma->vm_ops = &bch_vm_ops;
  1138. return 0;
  1139. }
  1140. /* Directories: */
  1141. static loff_t bch2_dir_llseek(struct file *file, loff_t offset, int whence)
  1142. {
  1143. return generic_file_llseek_size(file, offset, whence,
  1144. S64_MAX, S64_MAX);
  1145. }
  1146. static int bch2_vfs_readdir(struct file *file, struct dir_context *ctx)
  1147. {
  1148. struct bch_inode_info *inode = file_bch_inode(file);
  1149. struct bch_fs *c = inode->v.i_sb->s_fs_info;
  1150. if (!dir_emit_dots(file, ctx))
  1151. return 0;
  1152. int ret = bch2_readdir(c, inode_inum(inode), ctx);
  1153. bch_err_fn(c, ret);
  1154. return bch2_err_class(ret);
  1155. }
  1156. static int bch2_open(struct inode *vinode, struct file *file)
  1157. {
  1158. if (file->f_flags & (O_WRONLY|O_RDWR)) {
  1159. struct bch_inode_info *inode = to_bch_ei(vinode);
  1160. struct bch_fs *c = inode->v.i_sb->s_fs_info;
  1161. int ret = bch2_subvol_is_ro(c, inode->ei_inum.subvol);
  1162. if (ret)
  1163. return ret;
  1164. }
  1165. file->f_mode |= FMODE_CAN_ODIRECT;
  1166. return generic_file_open(vinode, file);
  1167. }
  1168. static const struct file_operations bch_file_operations = {
  1169. .open = bch2_open,
  1170. .llseek = bch2_llseek,
  1171. .read_iter = bch2_read_iter,
  1172. .write_iter = bch2_write_iter,
  1173. .mmap = bch2_mmap,
  1174. .get_unmapped_area = thp_get_unmapped_area,
  1175. .fsync = bch2_fsync,
  1176. .splice_read = filemap_splice_read,
  1177. .splice_write = iter_file_splice_write,
  1178. .fallocate = bch2_fallocate_dispatch,
  1179. .unlocked_ioctl = bch2_fs_file_ioctl,
  1180. #ifdef CONFIG_COMPAT
  1181. .compat_ioctl = bch2_compat_fs_ioctl,
  1182. #endif
  1183. .remap_file_range = bch2_remap_file_range,
  1184. };
  1185. static const struct inode_operations bch_file_inode_operations = {
  1186. .getattr = bch2_getattr,
  1187. .setattr = bch2_setattr,
  1188. .fiemap = bch2_fiemap,
  1189. .listxattr = bch2_xattr_list,
  1190. #ifdef CONFIG_BCACHEFS_POSIX_ACL
  1191. .get_inode_acl = bch2_get_acl,
  1192. .set_acl = bch2_set_acl,
  1193. #endif
  1194. };
  1195. static const struct inode_operations bch_dir_inode_operations = {
  1196. .lookup = bch2_lookup,
  1197. .create = bch2_create,
  1198. .link = bch2_link,
  1199. .unlink = bch2_unlink,
  1200. .symlink = bch2_symlink,
  1201. .mkdir = bch2_mkdir,
  1202. .rmdir = bch2_unlink,
  1203. .mknod = bch2_mknod,
  1204. .rename = bch2_rename2,
  1205. .getattr = bch2_getattr,
  1206. .setattr = bch2_setattr,
  1207. .tmpfile = bch2_tmpfile,
  1208. .listxattr = bch2_xattr_list,
  1209. #ifdef CONFIG_BCACHEFS_POSIX_ACL
  1210. .get_inode_acl = bch2_get_acl,
  1211. .set_acl = bch2_set_acl,
  1212. #endif
  1213. };
  1214. static const struct file_operations bch_dir_file_operations = {
  1215. .llseek = bch2_dir_llseek,
  1216. .read = generic_read_dir,
  1217. .iterate_shared = bch2_vfs_readdir,
  1218. .fsync = bch2_fsync,
  1219. .unlocked_ioctl = bch2_fs_file_ioctl,
  1220. #ifdef CONFIG_COMPAT
  1221. .compat_ioctl = bch2_compat_fs_ioctl,
  1222. #endif
  1223. };
  1224. static const struct inode_operations bch_symlink_inode_operations = {
  1225. .get_link = page_get_link,
  1226. .getattr = bch2_getattr,
  1227. .setattr = bch2_setattr,
  1228. .listxattr = bch2_xattr_list,
  1229. #ifdef CONFIG_BCACHEFS_POSIX_ACL
  1230. .get_inode_acl = bch2_get_acl,
  1231. .set_acl = bch2_set_acl,
  1232. #endif
  1233. };
  1234. static const struct inode_operations bch_special_inode_operations = {
  1235. .getattr = bch2_getattr,
  1236. .setattr = bch2_setattr,
  1237. .listxattr = bch2_xattr_list,
  1238. #ifdef CONFIG_BCACHEFS_POSIX_ACL
  1239. .get_inode_acl = bch2_get_acl,
  1240. .set_acl = bch2_set_acl,
  1241. #endif
  1242. };
  1243. static const struct address_space_operations bch_address_space_operations = {
  1244. .read_folio = bch2_read_folio,
  1245. .writepages = bch2_writepages,
  1246. .readahead = bch2_readahead,
  1247. .dirty_folio = filemap_dirty_folio,
  1248. .write_begin = bch2_write_begin,
  1249. .write_end = bch2_write_end,
  1250. .invalidate_folio = bch2_invalidate_folio,
  1251. .release_folio = bch2_release_folio,
  1252. #ifdef CONFIG_MIGRATION
  1253. .migrate_folio = filemap_migrate_folio,
  1254. #endif
  1255. .error_remove_folio = generic_error_remove_folio,
  1256. };
  1257. struct bcachefs_fid {
  1258. u64 inum;
  1259. u32 subvol;
  1260. u32 gen;
  1261. } __packed;
  1262. struct bcachefs_fid_with_parent {
  1263. struct bcachefs_fid fid;
  1264. struct bcachefs_fid dir;
  1265. } __packed;
  1266. static int bcachefs_fid_valid(int fh_len, int fh_type)
  1267. {
  1268. switch (fh_type) {
  1269. case FILEID_BCACHEFS_WITHOUT_PARENT:
  1270. return fh_len == sizeof(struct bcachefs_fid) / sizeof(u32);
  1271. case FILEID_BCACHEFS_WITH_PARENT:
  1272. return fh_len == sizeof(struct bcachefs_fid_with_parent) / sizeof(u32);
  1273. default:
  1274. return false;
  1275. }
  1276. }
  1277. static struct bcachefs_fid bch2_inode_to_fid(struct bch_inode_info *inode)
  1278. {
  1279. return (struct bcachefs_fid) {
  1280. .inum = inode->ei_inum.inum,
  1281. .subvol = inode->ei_inum.subvol,
  1282. .gen = inode->ei_inode.bi_generation,
  1283. };
  1284. }
  1285. static int bch2_encode_fh(struct inode *vinode, u32 *fh, int *len,
  1286. struct inode *vdir)
  1287. {
  1288. struct bch_inode_info *inode = to_bch_ei(vinode);
  1289. struct bch_inode_info *dir = to_bch_ei(vdir);
  1290. int min_len;
  1291. if (!S_ISDIR(inode->v.i_mode) && dir) {
  1292. struct bcachefs_fid_with_parent *fid = (void *) fh;
  1293. min_len = sizeof(*fid) / sizeof(u32);
  1294. if (*len < min_len) {
  1295. *len = min_len;
  1296. return FILEID_INVALID;
  1297. }
  1298. fid->fid = bch2_inode_to_fid(inode);
  1299. fid->dir = bch2_inode_to_fid(dir);
  1300. *len = min_len;
  1301. return FILEID_BCACHEFS_WITH_PARENT;
  1302. } else {
  1303. struct bcachefs_fid *fid = (void *) fh;
  1304. min_len = sizeof(*fid) / sizeof(u32);
  1305. if (*len < min_len) {
  1306. *len = min_len;
  1307. return FILEID_INVALID;
  1308. }
  1309. *fid = bch2_inode_to_fid(inode);
  1310. *len = min_len;
  1311. return FILEID_BCACHEFS_WITHOUT_PARENT;
  1312. }
  1313. }
  1314. static struct inode *bch2_nfs_get_inode(struct super_block *sb,
  1315. struct bcachefs_fid fid)
  1316. {
  1317. struct bch_fs *c = sb->s_fs_info;
  1318. struct inode *vinode = bch2_vfs_inode_get(c, (subvol_inum) {
  1319. .subvol = fid.subvol,
  1320. .inum = fid.inum,
  1321. });
  1322. if (!IS_ERR(vinode) && vinode->i_generation != fid.gen) {
  1323. iput(vinode);
  1324. vinode = ERR_PTR(-ESTALE);
  1325. }
  1326. return vinode;
  1327. }
  1328. static struct dentry *bch2_fh_to_dentry(struct super_block *sb, struct fid *_fid,
  1329. int fh_len, int fh_type)
  1330. {
  1331. struct bcachefs_fid *fid = (void *) _fid;
  1332. if (!bcachefs_fid_valid(fh_len, fh_type))
  1333. return NULL;
  1334. return d_obtain_alias(bch2_nfs_get_inode(sb, *fid));
  1335. }
  1336. static struct dentry *bch2_fh_to_parent(struct super_block *sb, struct fid *_fid,
  1337. int fh_len, int fh_type)
  1338. {
  1339. struct bcachefs_fid_with_parent *fid = (void *) _fid;
  1340. if (!bcachefs_fid_valid(fh_len, fh_type) ||
  1341. fh_type != FILEID_BCACHEFS_WITH_PARENT)
  1342. return NULL;
  1343. return d_obtain_alias(bch2_nfs_get_inode(sb, fid->dir));
  1344. }
  1345. static struct dentry *bch2_get_parent(struct dentry *child)
  1346. {
  1347. struct bch_inode_info *inode = to_bch_ei(child->d_inode);
  1348. struct bch_fs *c = inode->v.i_sb->s_fs_info;
  1349. subvol_inum parent_inum = {
  1350. .subvol = inode->ei_inode.bi_parent_subvol ?:
  1351. inode->ei_inum.subvol,
  1352. .inum = inode->ei_inode.bi_dir,
  1353. };
  1354. return d_obtain_alias(bch2_vfs_inode_get(c, parent_inum));
  1355. }
  1356. static int bch2_get_name(struct dentry *parent, char *name, struct dentry *child)
  1357. {
  1358. struct bch_inode_info *inode = to_bch_ei(child->d_inode);
  1359. struct bch_inode_info *dir = to_bch_ei(parent->d_inode);
  1360. struct bch_fs *c = inode->v.i_sb->s_fs_info;
  1361. struct btree_trans *trans;
  1362. struct btree_iter iter1;
  1363. struct btree_iter iter2;
  1364. struct bkey_s_c k;
  1365. struct bkey_s_c_dirent d;
  1366. struct bch_inode_unpacked inode_u;
  1367. subvol_inum target;
  1368. u32 snapshot;
  1369. struct qstr dirent_name;
  1370. unsigned name_len = 0;
  1371. int ret;
  1372. if (!S_ISDIR(dir->v.i_mode))
  1373. return -EINVAL;
  1374. trans = bch2_trans_get(c);
  1375. bch2_trans_iter_init(trans, &iter1, BTREE_ID_dirents,
  1376. POS(dir->ei_inode.bi_inum, 0), 0);
  1377. bch2_trans_iter_init(trans, &iter2, BTREE_ID_dirents,
  1378. POS(dir->ei_inode.bi_inum, 0), 0);
  1379. retry:
  1380. bch2_trans_begin(trans);
  1381. ret = bch2_subvolume_get_snapshot(trans, dir->ei_inum.subvol, &snapshot);
  1382. if (ret)
  1383. goto err;
  1384. bch2_btree_iter_set_snapshot(&iter1, snapshot);
  1385. bch2_btree_iter_set_snapshot(&iter2, snapshot);
  1386. ret = bch2_inode_find_by_inum_trans(trans, inode_inum(inode), &inode_u);
  1387. if (ret)
  1388. goto err;
  1389. if (inode_u.bi_dir == dir->ei_inode.bi_inum) {
  1390. bch2_btree_iter_set_pos(&iter1, POS(inode_u.bi_dir, inode_u.bi_dir_offset));
  1391. k = bch2_btree_iter_peek_slot(&iter1);
  1392. ret = bkey_err(k);
  1393. if (ret)
  1394. goto err;
  1395. if (k.k->type != KEY_TYPE_dirent) {
  1396. ret = -BCH_ERR_ENOENT_dirent_doesnt_match_inode;
  1397. goto err;
  1398. }
  1399. d = bkey_s_c_to_dirent(k);
  1400. ret = bch2_dirent_read_target(trans, inode_inum(dir), d, &target);
  1401. if (ret > 0)
  1402. ret = -BCH_ERR_ENOENT_dirent_doesnt_match_inode;
  1403. if (ret)
  1404. goto err;
  1405. if (subvol_inum_eq(target, inode->ei_inum))
  1406. goto found;
  1407. } else {
  1408. /*
  1409. * File with multiple hardlinks and our backref is to the wrong
  1410. * directory - linear search:
  1411. */
  1412. for_each_btree_key_continue_norestart(iter2, 0, k, ret) {
  1413. if (k.k->p.inode > dir->ei_inode.bi_inum)
  1414. break;
  1415. if (k.k->type != KEY_TYPE_dirent)
  1416. continue;
  1417. d = bkey_s_c_to_dirent(k);
  1418. ret = bch2_dirent_read_target(trans, inode_inum(dir), d, &target);
  1419. if (ret < 0)
  1420. break;
  1421. if (ret)
  1422. continue;
  1423. if (subvol_inum_eq(target, inode->ei_inum))
  1424. goto found;
  1425. }
  1426. }
  1427. ret = -ENOENT;
  1428. goto err;
  1429. found:
  1430. dirent_name = bch2_dirent_get_name(d);
  1431. name_len = min_t(unsigned, dirent_name.len, NAME_MAX);
  1432. memcpy(name, dirent_name.name, name_len);
  1433. name[name_len] = '\0';
  1434. err:
  1435. if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
  1436. goto retry;
  1437. bch2_trans_iter_exit(trans, &iter1);
  1438. bch2_trans_iter_exit(trans, &iter2);
  1439. bch2_trans_put(trans);
  1440. return ret;
  1441. }
  1442. static const struct export_operations bch_export_ops = {
  1443. .encode_fh = bch2_encode_fh,
  1444. .fh_to_dentry = bch2_fh_to_dentry,
  1445. .fh_to_parent = bch2_fh_to_parent,
  1446. .get_parent = bch2_get_parent,
  1447. .get_name = bch2_get_name,
  1448. };
  1449. static void bch2_vfs_inode_init(struct btree_trans *trans,
  1450. subvol_inum inum,
  1451. struct bch_inode_info *inode,
  1452. struct bch_inode_unpacked *bi,
  1453. struct bch_subvolume *subvol)
  1454. {
  1455. inode->v.i_ino = inum.inum;
  1456. inode->ei_inum = inum;
  1457. inode->ei_inode.bi_inum = inum.inum;
  1458. bch2_inode_update_after_write(trans, inode, bi, ~0);
  1459. inode->v.i_blocks = bi->bi_sectors;
  1460. inode->v.i_ino = bi->bi_inum;
  1461. inode->v.i_rdev = bi->bi_dev;
  1462. inode->v.i_generation = bi->bi_generation;
  1463. inode->v.i_size = bi->bi_size;
  1464. inode->ei_flags = 0;
  1465. inode->ei_quota_reserved = 0;
  1466. inode->ei_qid = bch_qid(bi);
  1467. if (BCH_SUBVOLUME_SNAP(subvol))
  1468. set_bit(EI_INODE_SNAPSHOT, &inode->ei_flags);
  1469. inode->v.i_mapping->a_ops = &bch_address_space_operations;
  1470. switch (inode->v.i_mode & S_IFMT) {
  1471. case S_IFREG:
  1472. inode->v.i_op = &bch_file_inode_operations;
  1473. inode->v.i_fop = &bch_file_operations;
  1474. break;
  1475. case S_IFDIR:
  1476. inode->v.i_op = &bch_dir_inode_operations;
  1477. inode->v.i_fop = &bch_dir_file_operations;
  1478. break;
  1479. case S_IFLNK:
  1480. inode_nohighmem(&inode->v);
  1481. inode->v.i_op = &bch_symlink_inode_operations;
  1482. break;
  1483. default:
  1484. init_special_inode(&inode->v, inode->v.i_mode, inode->v.i_rdev);
  1485. inode->v.i_op = &bch_special_inode_operations;
  1486. break;
  1487. }
  1488. mapping_set_large_folios(inode->v.i_mapping);
  1489. }
  1490. static void bch2_free_inode(struct inode *vinode)
  1491. {
  1492. kmem_cache_free(bch2_inode_cache, to_bch_ei(vinode));
  1493. }
  1494. static int inode_update_times_fn(struct btree_trans *trans,
  1495. struct bch_inode_info *inode,
  1496. struct bch_inode_unpacked *bi,
  1497. void *p)
  1498. {
  1499. struct bch_fs *c = inode->v.i_sb->s_fs_info;
  1500. bi->bi_atime = timespec_to_bch2_time(c, inode_get_atime(&inode->v));
  1501. bi->bi_mtime = timespec_to_bch2_time(c, inode_get_mtime(&inode->v));
  1502. bi->bi_ctime = timespec_to_bch2_time(c, inode_get_ctime(&inode->v));
  1503. return 0;
  1504. }
  1505. static int bch2_vfs_write_inode(struct inode *vinode,
  1506. struct writeback_control *wbc)
  1507. {
  1508. struct bch_fs *c = vinode->i_sb->s_fs_info;
  1509. struct bch_inode_info *inode = to_bch_ei(vinode);
  1510. int ret;
  1511. mutex_lock(&inode->ei_update_lock);
  1512. ret = bch2_write_inode(c, inode, inode_update_times_fn, NULL,
  1513. ATTR_ATIME|ATTR_MTIME|ATTR_CTIME);
  1514. mutex_unlock(&inode->ei_update_lock);
  1515. return bch2_err_class(ret);
  1516. }
  1517. static void bch2_evict_inode(struct inode *vinode)
  1518. {
  1519. struct bch_fs *c = vinode->i_sb->s_fs_info;
  1520. struct bch_inode_info *inode = to_bch_ei(vinode);
  1521. bool delete = !inode->v.i_nlink && !is_bad_inode(&inode->v);
  1522. /*
  1523. * evict() has waited for outstanding writeback, we'll do no more IO
  1524. * through this inode: it's safe to remove from VFS inode hashtable here
  1525. *
  1526. * Do that now so that other threads aren't blocked from pulling it back
  1527. * in, there's no reason for them to be:
  1528. */
  1529. if (!delete)
  1530. bch2_inode_hash_remove(c, inode);
  1531. truncate_inode_pages_final(&inode->v.i_data);
  1532. clear_inode(&inode->v);
  1533. BUG_ON(!is_bad_inode(&inode->v) && inode->ei_quota_reserved);
  1534. if (delete) {
  1535. bch2_quota_acct(c, inode->ei_qid, Q_SPC, -((s64) inode->v.i_blocks),
  1536. KEY_TYPE_QUOTA_WARN);
  1537. bch2_quota_acct(c, inode->ei_qid, Q_INO, -1,
  1538. KEY_TYPE_QUOTA_WARN);
  1539. bch2_inode_rm(c, inode_inum(inode));
  1540. /*
  1541. * If we are deleting, we need it present in the vfs hash table
  1542. * so that fsck can check if unlinked inodes are still open:
  1543. */
  1544. bch2_inode_hash_remove(c, inode);
  1545. }
  1546. mutex_lock(&c->vfs_inodes_lock);
  1547. list_del_init(&inode->ei_vfs_inode_list);
  1548. mutex_unlock(&c->vfs_inodes_lock);
  1549. }
  1550. void bch2_evict_subvolume_inodes(struct bch_fs *c, snapshot_id_list *s)
  1551. {
  1552. struct bch_inode_info *inode;
  1553. DARRAY(struct bch_inode_info *) grabbed;
  1554. bool clean_pass = false, this_pass_clean;
  1555. /*
  1556. * Initially, we scan for inodes without I_DONTCACHE, then mark them to
  1557. * be pruned with d_mark_dontcache().
  1558. *
  1559. * Once we've had a clean pass where we didn't find any inodes without
  1560. * I_DONTCACHE, we wait for them to be freed:
  1561. */
  1562. darray_init(&grabbed);
  1563. darray_make_room(&grabbed, 1024);
  1564. again:
  1565. cond_resched();
  1566. this_pass_clean = true;
  1567. mutex_lock(&c->vfs_inodes_lock);
  1568. list_for_each_entry(inode, &c->vfs_inodes_list, ei_vfs_inode_list) {
  1569. if (!snapshot_list_has_id(s, inode->ei_inum.subvol))
  1570. continue;
  1571. if (!(inode->v.i_state & I_DONTCACHE) &&
  1572. !(inode->v.i_state & I_FREEING) &&
  1573. igrab(&inode->v)) {
  1574. this_pass_clean = false;
  1575. if (darray_push_gfp(&grabbed, inode, GFP_ATOMIC|__GFP_NOWARN)) {
  1576. iput(&inode->v);
  1577. break;
  1578. }
  1579. } else if (clean_pass && this_pass_clean) {
  1580. struct wait_bit_queue_entry wqe;
  1581. struct wait_queue_head *wq_head;
  1582. wq_head = inode_bit_waitqueue(&wqe, &inode->v, __I_NEW);
  1583. prepare_to_wait_event(wq_head, &wqe.wq_entry,
  1584. TASK_UNINTERRUPTIBLE);
  1585. mutex_unlock(&c->vfs_inodes_lock);
  1586. schedule();
  1587. finish_wait(wq_head, &wqe.wq_entry);
  1588. goto again;
  1589. }
  1590. }
  1591. mutex_unlock(&c->vfs_inodes_lock);
  1592. darray_for_each(grabbed, i) {
  1593. inode = *i;
  1594. d_mark_dontcache(&inode->v);
  1595. d_prune_aliases(&inode->v);
  1596. iput(&inode->v);
  1597. }
  1598. grabbed.nr = 0;
  1599. if (!clean_pass || !this_pass_clean) {
  1600. clean_pass = this_pass_clean;
  1601. goto again;
  1602. }
  1603. darray_exit(&grabbed);
  1604. }
  1605. static int bch2_statfs(struct dentry *dentry, struct kstatfs *buf)
  1606. {
  1607. struct super_block *sb = dentry->d_sb;
  1608. struct bch_fs *c = sb->s_fs_info;
  1609. struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c);
  1610. unsigned shift = sb->s_blocksize_bits - 9;
  1611. /*
  1612. * this assumes inodes take up 64 bytes, which is a decent average
  1613. * number:
  1614. */
  1615. u64 avail_inodes = ((usage.capacity - usage.used) << 3);
  1616. buf->f_type = BCACHEFS_STATFS_MAGIC;
  1617. buf->f_bsize = sb->s_blocksize;
  1618. buf->f_blocks = usage.capacity >> shift;
  1619. buf->f_bfree = usage.free >> shift;
  1620. buf->f_bavail = avail_factor(usage.free) >> shift;
  1621. buf->f_files = usage.nr_inodes + avail_inodes;
  1622. buf->f_ffree = avail_inodes;
  1623. buf->f_fsid = uuid_to_fsid(c->sb.user_uuid.b);
  1624. buf->f_namelen = BCH_NAME_MAX;
  1625. return 0;
  1626. }
  1627. static int bch2_sync_fs(struct super_block *sb, int wait)
  1628. {
  1629. struct bch_fs *c = sb->s_fs_info;
  1630. int ret;
  1631. trace_bch2_sync_fs(sb, wait);
  1632. if (c->opts.journal_flush_disabled)
  1633. return 0;
  1634. if (!wait) {
  1635. bch2_journal_flush_async(&c->journal, NULL);
  1636. return 0;
  1637. }
  1638. ret = bch2_journal_flush(&c->journal);
  1639. return bch2_err_class(ret);
  1640. }
  1641. static struct bch_fs *bch2_path_to_fs(const char *path)
  1642. {
  1643. struct bch_fs *c;
  1644. dev_t dev;
  1645. int ret;
  1646. ret = lookup_bdev(path, &dev);
  1647. if (ret)
  1648. return ERR_PTR(ret);
  1649. c = bch2_dev_to_fs(dev);
  1650. if (c)
  1651. closure_put(&c->cl);
  1652. return c ?: ERR_PTR(-ENOENT);
  1653. }
  1654. static int bch2_remount(struct super_block *sb, int *flags,
  1655. struct bch_opts opts)
  1656. {
  1657. struct bch_fs *c = sb->s_fs_info;
  1658. int ret = 0;
  1659. opt_set(opts, read_only, (*flags & SB_RDONLY) != 0);
  1660. if (opts.read_only != c->opts.read_only) {
  1661. down_write(&c->state_lock);
  1662. if (opts.read_only) {
  1663. bch2_fs_read_only(c);
  1664. sb->s_flags |= SB_RDONLY;
  1665. } else {
  1666. ret = bch2_fs_read_write(c);
  1667. if (ret) {
  1668. bch_err(c, "error going rw: %i", ret);
  1669. up_write(&c->state_lock);
  1670. ret = -EINVAL;
  1671. goto err;
  1672. }
  1673. sb->s_flags &= ~SB_RDONLY;
  1674. }
  1675. c->opts.read_only = opts.read_only;
  1676. up_write(&c->state_lock);
  1677. }
  1678. if (opt_defined(opts, errors))
  1679. c->opts.errors = opts.errors;
  1680. err:
  1681. return bch2_err_class(ret);
  1682. }
  1683. static int bch2_show_devname(struct seq_file *seq, struct dentry *root)
  1684. {
  1685. struct bch_fs *c = root->d_sb->s_fs_info;
  1686. bool first = true;
  1687. for_each_online_member(c, ca) {
  1688. if (!first)
  1689. seq_putc(seq, ':');
  1690. first = false;
  1691. seq_puts(seq, ca->disk_sb.sb_name);
  1692. }
  1693. return 0;
  1694. }
  1695. static int bch2_show_options(struct seq_file *seq, struct dentry *root)
  1696. {
  1697. struct bch_fs *c = root->d_sb->s_fs_info;
  1698. struct printbuf buf = PRINTBUF;
  1699. bch2_opts_to_text(&buf, c->opts, c, c->disk_sb.sb,
  1700. OPT_MOUNT, OPT_HIDDEN, OPT_SHOW_MOUNT_STYLE);
  1701. printbuf_nul_terminate(&buf);
  1702. seq_printf(seq, ",%s", buf.buf);
  1703. int ret = buf.allocation_failure ? -ENOMEM : 0;
  1704. printbuf_exit(&buf);
  1705. return ret;
  1706. }
  1707. static void bch2_put_super(struct super_block *sb)
  1708. {
  1709. struct bch_fs *c = sb->s_fs_info;
  1710. __bch2_fs_stop(c);
  1711. }
  1712. /*
  1713. * bcachefs doesn't currently integrate intwrite freeze protection but the
  1714. * internal write references serve the same purpose. Therefore reuse the
  1715. * read-only transition code to perform the quiesce. The caveat is that we don't
  1716. * currently have the ability to block tasks that want a write reference while
  1717. * the superblock is frozen. This is fine for now, but we should either add
  1718. * blocking support or find a way to integrate sb_start_intwrite() and friends.
  1719. */
  1720. static int bch2_freeze(struct super_block *sb)
  1721. {
  1722. struct bch_fs *c = sb->s_fs_info;
  1723. down_write(&c->state_lock);
  1724. bch2_fs_read_only(c);
  1725. up_write(&c->state_lock);
  1726. return 0;
  1727. }
  1728. static int bch2_unfreeze(struct super_block *sb)
  1729. {
  1730. struct bch_fs *c = sb->s_fs_info;
  1731. int ret;
  1732. if (test_bit(BCH_FS_emergency_ro, &c->flags))
  1733. return 0;
  1734. down_write(&c->state_lock);
  1735. ret = bch2_fs_read_write(c);
  1736. up_write(&c->state_lock);
  1737. return ret;
  1738. }
  1739. static const struct super_operations bch_super_operations = {
  1740. .alloc_inode = bch2_alloc_inode,
  1741. .free_inode = bch2_free_inode,
  1742. .write_inode = bch2_vfs_write_inode,
  1743. .evict_inode = bch2_evict_inode,
  1744. .sync_fs = bch2_sync_fs,
  1745. .statfs = bch2_statfs,
  1746. .show_devname = bch2_show_devname,
  1747. .show_options = bch2_show_options,
  1748. .put_super = bch2_put_super,
  1749. .freeze_fs = bch2_freeze,
  1750. .unfreeze_fs = bch2_unfreeze,
  1751. };
  1752. static int bch2_set_super(struct super_block *s, void *data)
  1753. {
  1754. s->s_fs_info = data;
  1755. return 0;
  1756. }
  1757. static int bch2_noset_super(struct super_block *s, void *data)
  1758. {
  1759. return -EBUSY;
  1760. }
  1761. typedef DARRAY(struct bch_fs *) darray_fs;
  1762. static int bch2_test_super(struct super_block *s, void *data)
  1763. {
  1764. struct bch_fs *c = s->s_fs_info;
  1765. darray_fs *d = data;
  1766. if (!c)
  1767. return false;
  1768. darray_for_each(*d, i)
  1769. if (c != *i)
  1770. return false;
  1771. return true;
  1772. }
  1773. static int bch2_fs_get_tree(struct fs_context *fc)
  1774. {
  1775. struct bch_fs *c;
  1776. struct super_block *sb;
  1777. struct inode *vinode;
  1778. struct bch2_opts_parse *opts_parse = fc->fs_private;
  1779. struct bch_opts opts = opts_parse->opts;
  1780. darray_str devs;
  1781. darray_fs devs_to_fs = {};
  1782. int ret;
  1783. opt_set(opts, read_only, (fc->sb_flags & SB_RDONLY) != 0);
  1784. opt_set(opts, nostart, true);
  1785. if (!fc->source || strlen(fc->source) == 0)
  1786. return -EINVAL;
  1787. ret = bch2_split_devs(fc->source, &devs);
  1788. if (ret)
  1789. return ret;
  1790. darray_for_each(devs, i) {
  1791. ret = darray_push(&devs_to_fs, bch2_path_to_fs(*i));
  1792. if (ret)
  1793. goto err;
  1794. }
  1795. sb = sget(fc->fs_type, bch2_test_super, bch2_noset_super, fc->sb_flags|SB_NOSEC, &devs_to_fs);
  1796. if (!IS_ERR(sb))
  1797. goto got_sb;
  1798. c = bch2_fs_open(devs.data, devs.nr, opts);
  1799. ret = PTR_ERR_OR_ZERO(c);
  1800. if (ret)
  1801. goto err;
  1802. /* Some options can't be parsed until after the fs is started: */
  1803. opts = bch2_opts_empty();
  1804. ret = bch2_parse_mount_opts(c, &opts, NULL, opts_parse->parse_later.buf);
  1805. if (ret)
  1806. goto err_stop_fs;
  1807. bch2_opts_apply(&c->opts, opts);
  1808. ret = bch2_fs_start(c);
  1809. if (ret)
  1810. goto err_stop_fs;
  1811. sb = sget(fc->fs_type, NULL, bch2_set_super, fc->sb_flags|SB_NOSEC, c);
  1812. ret = PTR_ERR_OR_ZERO(sb);
  1813. if (ret)
  1814. goto err_stop_fs;
  1815. got_sb:
  1816. c = sb->s_fs_info;
  1817. if (sb->s_root) {
  1818. if ((fc->sb_flags ^ sb->s_flags) & SB_RDONLY) {
  1819. ret = -EBUSY;
  1820. goto err_put_super;
  1821. }
  1822. goto out;
  1823. }
  1824. sb->s_blocksize = block_bytes(c);
  1825. sb->s_blocksize_bits = ilog2(block_bytes(c));
  1826. sb->s_maxbytes = MAX_LFS_FILESIZE;
  1827. sb->s_op = &bch_super_operations;
  1828. sb->s_export_op = &bch_export_ops;
  1829. #ifdef CONFIG_BCACHEFS_QUOTA
  1830. sb->s_qcop = &bch2_quotactl_operations;
  1831. sb->s_quota_types = QTYPE_MASK_USR|QTYPE_MASK_GRP|QTYPE_MASK_PRJ;
  1832. #endif
  1833. sb->s_xattr = bch2_xattr_handlers;
  1834. sb->s_magic = BCACHEFS_STATFS_MAGIC;
  1835. sb->s_time_gran = c->sb.nsec_per_time_unit;
  1836. sb->s_time_min = div_s64(S64_MIN, c->sb.time_units_per_sec) + 1;
  1837. sb->s_time_max = div_s64(S64_MAX, c->sb.time_units_per_sec);
  1838. sb->s_uuid = c->sb.user_uuid;
  1839. sb->s_shrink->seeks = 0;
  1840. c->vfs_sb = sb;
  1841. strscpy(sb->s_id, c->name, sizeof(sb->s_id));
  1842. ret = super_setup_bdi(sb);
  1843. if (ret)
  1844. goto err_put_super;
  1845. sb->s_bdi->ra_pages = VM_READAHEAD_PAGES;
  1846. for_each_online_member(c, ca) {
  1847. struct block_device *bdev = ca->disk_sb.bdev;
  1848. /* XXX: create an anonymous device for multi device filesystems */
  1849. sb->s_bdev = bdev;
  1850. sb->s_dev = bdev->bd_dev;
  1851. percpu_ref_put(&ca->io_ref);
  1852. break;
  1853. }
  1854. c->dev = sb->s_dev;
  1855. #ifdef CONFIG_BCACHEFS_POSIX_ACL
  1856. if (c->opts.acl)
  1857. sb->s_flags |= SB_POSIXACL;
  1858. #endif
  1859. sb->s_shrink->seeks = 0;
  1860. vinode = bch2_vfs_inode_get(c, BCACHEFS_ROOT_SUBVOL_INUM);
  1861. ret = PTR_ERR_OR_ZERO(vinode);
  1862. bch_err_msg(c, ret, "mounting: error getting root inode");
  1863. if (ret)
  1864. goto err_put_super;
  1865. sb->s_root = d_make_root(vinode);
  1866. if (!sb->s_root) {
  1867. bch_err(c, "error mounting: error allocating root dentry");
  1868. ret = -ENOMEM;
  1869. goto err_put_super;
  1870. }
  1871. sb->s_flags |= SB_ACTIVE;
  1872. out:
  1873. fc->root = dget(sb->s_root);
  1874. err:
  1875. darray_exit(&devs_to_fs);
  1876. bch2_darray_str_exit(&devs);
  1877. if (ret)
  1878. pr_err("error: %s", bch2_err_str(ret));
  1879. /*
  1880. * On an inconsistency error in recovery we might see an -EROFS derived
  1881. * errorcode (from the journal), but we don't want to return that to
  1882. * userspace as that causes util-linux to retry the mount RO - which is
  1883. * confusing:
  1884. */
  1885. if (bch2_err_matches(ret, EROFS) && ret != -EROFS)
  1886. ret = -EIO;
  1887. return bch2_err_class(ret);
  1888. err_stop_fs:
  1889. bch2_fs_stop(c);
  1890. goto err;
  1891. err_put_super:
  1892. __bch2_fs_stop(c);
  1893. deactivate_locked_super(sb);
  1894. goto err;
  1895. }
  1896. static void bch2_kill_sb(struct super_block *sb)
  1897. {
  1898. struct bch_fs *c = sb->s_fs_info;
  1899. generic_shutdown_super(sb);
  1900. bch2_fs_free(c);
  1901. }
  1902. static void bch2_fs_context_free(struct fs_context *fc)
  1903. {
  1904. struct bch2_opts_parse *opts = fc->fs_private;
  1905. if (opts) {
  1906. printbuf_exit(&opts->parse_later);
  1907. kfree(opts);
  1908. }
  1909. }
  1910. static int bch2_fs_parse_param(struct fs_context *fc,
  1911. struct fs_parameter *param)
  1912. {
  1913. /*
  1914. * the "source" param, i.e., the name of the device(s) to mount,
  1915. * is handled by the VFS layer.
  1916. */
  1917. if (!strcmp(param->key, "source"))
  1918. return -ENOPARAM;
  1919. struct bch2_opts_parse *opts = fc->fs_private;
  1920. struct bch_fs *c = NULL;
  1921. /* for reconfigure, we already have a struct bch_fs */
  1922. if (fc->root)
  1923. c = fc->root->d_sb->s_fs_info;
  1924. int ret = bch2_parse_one_mount_opt(c, &opts->opts,
  1925. &opts->parse_later, param->key,
  1926. param->string);
  1927. return bch2_err_class(ret);
  1928. }
  1929. static int bch2_fs_reconfigure(struct fs_context *fc)
  1930. {
  1931. struct super_block *sb = fc->root->d_sb;
  1932. struct bch2_opts_parse *opts = fc->fs_private;
  1933. return bch2_remount(sb, &fc->sb_flags, opts->opts);
  1934. }
  1935. static const struct fs_context_operations bch2_context_ops = {
  1936. .free = bch2_fs_context_free,
  1937. .parse_param = bch2_fs_parse_param,
  1938. .get_tree = bch2_fs_get_tree,
  1939. .reconfigure = bch2_fs_reconfigure,
  1940. };
  1941. static int bch2_init_fs_context(struct fs_context *fc)
  1942. {
  1943. struct bch2_opts_parse *opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  1944. if (!opts)
  1945. return -ENOMEM;
  1946. opts->parse_later = PRINTBUF;
  1947. fc->ops = &bch2_context_ops;
  1948. fc->fs_private = opts;
  1949. return 0;
  1950. }
  1951. void bch2_fs_vfs_exit(struct bch_fs *c)
  1952. {
  1953. if (c->vfs_inodes_table.tbl)
  1954. rhashtable_destroy(&c->vfs_inodes_table);
  1955. }
  1956. int bch2_fs_vfs_init(struct bch_fs *c)
  1957. {
  1958. return rhashtable_init(&c->vfs_inodes_table, &bch2_vfs_inodes_params);
  1959. }
  1960. static struct file_system_type bcache_fs_type = {
  1961. .owner = THIS_MODULE,
  1962. .name = "bcachefs",
  1963. .init_fs_context = bch2_init_fs_context,
  1964. .kill_sb = bch2_kill_sb,
  1965. .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
  1966. };
  1967. MODULE_ALIAS_FS("bcachefs");
  1968. void bch2_vfs_exit(void)
  1969. {
  1970. unregister_filesystem(&bcache_fs_type);
  1971. kmem_cache_destroy(bch2_inode_cache);
  1972. }
  1973. int __init bch2_vfs_init(void)
  1974. {
  1975. int ret = -ENOMEM;
  1976. bch2_inode_cache = KMEM_CACHE(bch_inode_info, SLAB_RECLAIM_ACCOUNT |
  1977. SLAB_ACCOUNT);
  1978. if (!bch2_inode_cache)
  1979. goto err;
  1980. ret = register_filesystem(&bcache_fs_type);
  1981. if (ret)
  1982. goto err;
  1983. return 0;
  1984. err:
  1985. bch2_vfs_exit();
  1986. return ret;
  1987. }
  1988. #endif /* NO_BCACHEFS_FS */