1 // SPDX-License-Identifier: GPL-2.0
5 #include "btree_update.h"
12 #include <linux/posix_acl.h>
14 static inline int is_subdir_for_nlink(struct bch_inode_unpacked *inode)
16 return S_ISDIR(inode->bi_mode) && !inode->bi_subvol;
19 int bch2_create_trans(struct btree_trans *trans,
21 struct bch_inode_unpacked *dir_u,
22 struct bch_inode_unpacked *new_inode,
23 const struct qstr *name,
24 uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
25 struct posix_acl *default_acl,
26 struct posix_acl *acl,
27 subvol_inum snapshot_src,
30 struct bch_fs *c = trans->c;
31 struct btree_iter dir_iter = { NULL };
32 struct btree_iter inode_iter = { NULL };
33 subvol_inum new_inum = dir;
34 u64 now = bch2_current_time(c);
35 u64 cpu = raw_smp_processor_id();
38 unsigned dir_type = mode_to_type(mode);
41 ret = bch2_subvolume_get_snapshot(trans, dir.subvol, &snapshot);
45 ret = bch2_inode_peek(trans, &dir_iter, dir_u, dir, BTREE_ITER_INTENT);
49 if (!(flags & BCH_CREATE_SNAPSHOT)) {
50 /* Normal create path - allocate a new inode: */
51 bch2_inode_init_late(new_inode, now, uid, gid, mode, rdev, dir_u);
53 if (flags & BCH_CREATE_TMPFILE)
54 new_inode->bi_flags |= BCH_INODE_UNLINKED;
56 ret = bch2_inode_create(trans, &inode_iter, new_inode, snapshot, cpu);
60 snapshot_src = (subvol_inum) { 0 };
63 * Creating a snapshot - we're not allocating a new inode, but
64 * we do have to lookup the root inode of the subvolume we're
65 * snapshotting and update it (in the new snapshot):
68 if (!snapshot_src.inum) {
69 /* Inode wasn't specified, just snapshot: */
70 struct bch_subvolume s;
72 ret = bch2_subvolume_get(trans, snapshot_src.subvol, true,
73 BTREE_ITER_CACHED, &s);
77 snapshot_src.inum = le64_to_cpu(s.inode);
80 ret = bch2_inode_peek(trans, &inode_iter, new_inode, snapshot_src,
85 if (new_inode->bi_subvol != snapshot_src.subvol) {
86 /* Not a subvolume root: */
92 * If we're not root, we have to own the subvolume being
95 if (uid && new_inode->bi_uid != uid) {
100 flags |= BCH_CREATE_SUBVOL;
103 new_inum.inum = new_inode->bi_inum;
104 dir_target = new_inode->bi_inum;
106 if (flags & BCH_CREATE_SUBVOL) {
107 u32 new_subvol, dir_snapshot;
109 ret = bch2_subvolume_create(trans, new_inode->bi_inum,
111 &new_subvol, &snapshot,
112 (flags & BCH_CREATE_SNAPSHOT_RO) != 0);
116 new_inode->bi_parent_subvol = dir.subvol;
117 new_inode->bi_subvol = new_subvol;
118 new_inum.subvol = new_subvol;
119 dir_target = new_subvol;
120 dir_type = DT_SUBVOL;
122 ret = bch2_subvolume_get_snapshot(trans, dir.subvol, &dir_snapshot);
126 bch2_btree_iter_set_snapshot(&dir_iter, dir_snapshot);
127 ret = bch2_btree_iter_traverse(&dir_iter);
132 if (!(flags & BCH_CREATE_SNAPSHOT)) {
134 ret = bch2_set_acl_trans(trans, new_inum, new_inode,
135 default_acl, ACL_TYPE_DEFAULT);
141 ret = bch2_set_acl_trans(trans, new_inum, new_inode,
142 acl, ACL_TYPE_ACCESS);
148 if (!(flags & BCH_CREATE_TMPFILE)) {
149 struct bch_hash_info dir_hash = bch2_hash_info_init(c, dir_u);
152 if (is_subdir_for_nlink(new_inode))
154 dir_u->bi_mtime = dir_u->bi_ctime = now;
156 ret = bch2_inode_write(trans, &dir_iter, dir_u);
160 ret = bch2_dirent_create(trans, dir, &dir_hash,
165 BCH_HASH_SET_MUST_CREATE);
169 if (c->sb.version >= bcachefs_metadata_version_inode_backpointers) {
170 new_inode->bi_dir = dir_u->bi_inum;
171 new_inode->bi_dir_offset = dir_offset;
175 inode_iter.flags &= ~BTREE_ITER_ALL_SNAPSHOTS;
176 bch2_btree_iter_set_snapshot(&inode_iter, snapshot);
178 ret = bch2_btree_iter_traverse(&inode_iter) ?:
179 bch2_inode_write(trans, &inode_iter, new_inode);
181 bch2_trans_iter_exit(trans, &inode_iter);
182 bch2_trans_iter_exit(trans, &dir_iter);
186 int bch2_link_trans(struct btree_trans *trans,
187 subvol_inum dir, struct bch_inode_unpacked *dir_u,
188 subvol_inum inum, struct bch_inode_unpacked *inode_u,
189 const struct qstr *name)
191 struct bch_fs *c = trans->c;
192 struct btree_iter dir_iter = { NULL };
193 struct btree_iter inode_iter = { NULL };
194 struct bch_hash_info dir_hash;
195 u64 now = bch2_current_time(c);
199 if (dir.subvol != inum.subvol)
202 ret = bch2_inode_peek(trans, &inode_iter, inode_u, inum, BTREE_ITER_INTENT);
206 inode_u->bi_ctime = now;
207 ret = bch2_inode_nlink_inc(inode_u);
211 ret = bch2_inode_peek(trans, &dir_iter, dir_u, dir, BTREE_ITER_INTENT);
215 dir_u->bi_mtime = dir_u->bi_ctime = now;
217 dir_hash = bch2_hash_info_init(c, dir_u);
219 ret = bch2_dirent_create(trans, dir, &dir_hash,
220 mode_to_type(inode_u->bi_mode),
221 name, inum.inum, &dir_offset,
222 BCH_HASH_SET_MUST_CREATE);
226 if (c->sb.version >= bcachefs_metadata_version_inode_backpointers) {
227 inode_u->bi_dir = dir.inum;
228 inode_u->bi_dir_offset = dir_offset;
231 ret = bch2_inode_write(trans, &dir_iter, dir_u) ?:
232 bch2_inode_write(trans, &inode_iter, inode_u);
234 bch2_trans_iter_exit(trans, &dir_iter);
235 bch2_trans_iter_exit(trans, &inode_iter);
239 int bch2_unlink_trans(struct btree_trans *trans,
241 struct bch_inode_unpacked *dir_u,
242 struct bch_inode_unpacked *inode_u,
243 const struct qstr *name,
244 bool deleting_snapshot)
246 struct bch_fs *c = trans->c;
247 struct btree_iter dir_iter = { NULL };
248 struct btree_iter dirent_iter = { NULL };
249 struct btree_iter inode_iter = { NULL };
250 struct bch_hash_info dir_hash;
252 u64 now = bch2_current_time(c);
256 ret = bch2_inode_peek(trans, &dir_iter, dir_u, dir, BTREE_ITER_INTENT);
260 dir_hash = bch2_hash_info_init(c, dir_u);
262 ret = __bch2_dirent_lookup_trans(trans, &dirent_iter, dir, &dir_hash,
263 name, &inum, BTREE_ITER_INTENT);
267 ret = bch2_inode_peek(trans, &inode_iter, inode_u, inum,
272 if (!deleting_snapshot && S_ISDIR(inode_u->bi_mode)) {
273 ret = bch2_empty_dir_trans(trans, inum);
278 if (deleting_snapshot && !inode_u->bi_subvol) {
283 if (deleting_snapshot || inode_u->bi_subvol) {
284 ret = bch2_subvolume_unlink(trans, inode_u->bi_subvol);
288 k = bch2_btree_iter_peek_slot(&dirent_iter);
294 * If we're deleting a subvolume, we need to really delete the
295 * dirent, not just emit a whiteout in the current snapshot:
297 bch2_btree_iter_set_snapshot(&dirent_iter, k.k->p.snapshot);
298 ret = bch2_btree_iter_traverse(&dirent_iter);
302 bch2_inode_nlink_dec(trans, inode_u);
305 if (inode_u->bi_dir == dirent_iter.pos.inode &&
306 inode_u->bi_dir_offset == dirent_iter.pos.offset) {
308 inode_u->bi_dir_offset = 0;
311 dir_u->bi_mtime = dir_u->bi_ctime = inode_u->bi_ctime = now;
312 dir_u->bi_nlink -= is_subdir_for_nlink(inode_u);
314 ret = bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
315 &dir_hash, &dirent_iter,
316 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
317 bch2_inode_write(trans, &dir_iter, dir_u) ?:
318 bch2_inode_write(trans, &inode_iter, inode_u);
320 bch2_trans_iter_exit(trans, &inode_iter);
321 bch2_trans_iter_exit(trans, &dirent_iter);
322 bch2_trans_iter_exit(trans, &dir_iter);
326 bool bch2_reinherit_attrs(struct bch_inode_unpacked *dst_u,
327 struct bch_inode_unpacked *src_u)
333 for (id = 0; id < Inode_opt_nr; id++) {
334 /* Skip attributes that were explicitly set on this inode */
335 if (dst_u->bi_fields_set & (1 << id))
338 src = bch2_inode_opt_get(src_u, id);
339 dst = bch2_inode_opt_get(dst_u, id);
344 bch2_inode_opt_set(dst_u, id, src);
351 int bch2_rename_trans(struct btree_trans *trans,
352 subvol_inum src_dir, struct bch_inode_unpacked *src_dir_u,
353 subvol_inum dst_dir, struct bch_inode_unpacked *dst_dir_u,
354 struct bch_inode_unpacked *src_inode_u,
355 struct bch_inode_unpacked *dst_inode_u,
356 const struct qstr *src_name,
357 const struct qstr *dst_name,
358 enum bch_rename_mode mode)
360 struct bch_fs *c = trans->c;
361 struct btree_iter src_dir_iter = { NULL };
362 struct btree_iter dst_dir_iter = { NULL };
363 struct btree_iter src_inode_iter = { NULL };
364 struct btree_iter dst_inode_iter = { NULL };
365 struct bch_hash_info src_hash, dst_hash;
366 subvol_inum src_inum, dst_inum;
367 u64 src_offset, dst_offset;
368 u64 now = bch2_current_time(c);
371 ret = bch2_inode_peek(trans, &src_dir_iter, src_dir_u, src_dir,
376 src_hash = bch2_hash_info_init(c, src_dir_u);
378 if (dst_dir.inum != src_dir.inum ||
379 dst_dir.subvol != src_dir.subvol) {
380 ret = bch2_inode_peek(trans, &dst_dir_iter, dst_dir_u, dst_dir,
385 dst_hash = bch2_hash_info_init(c, dst_dir_u);
387 dst_dir_u = src_dir_u;
391 ret = bch2_dirent_rename(trans,
394 src_name, &src_inum, &src_offset,
395 dst_name, &dst_inum, &dst_offset,
400 ret = bch2_inode_peek(trans, &src_inode_iter, src_inode_u, src_inum,
406 ret = bch2_inode_peek(trans, &dst_inode_iter, dst_inode_u, dst_inum,
412 if (c->sb.version >= bcachefs_metadata_version_inode_backpointers) {
413 src_inode_u->bi_dir = dst_dir_u->bi_inum;
414 src_inode_u->bi_dir_offset = dst_offset;
416 if (mode == BCH_RENAME_EXCHANGE) {
417 dst_inode_u->bi_dir = src_dir_u->bi_inum;
418 dst_inode_u->bi_dir_offset = src_offset;
421 if (mode == BCH_RENAME_OVERWRITE &&
422 dst_inode_u->bi_dir == dst_dir_u->bi_inum &&
423 dst_inode_u->bi_dir_offset == src_offset) {
424 dst_inode_u->bi_dir = 0;
425 dst_inode_u->bi_dir_offset = 0;
429 if (mode == BCH_RENAME_OVERWRITE) {
430 if (S_ISDIR(src_inode_u->bi_mode) !=
431 S_ISDIR(dst_inode_u->bi_mode)) {
436 if (S_ISDIR(dst_inode_u->bi_mode) &&
437 bch2_empty_dir_trans(trans, dst_inum)) {
443 if (bch2_reinherit_attrs(src_inode_u, dst_dir_u) &&
444 S_ISDIR(src_inode_u->bi_mode)) {
449 if (mode == BCH_RENAME_EXCHANGE &&
450 bch2_reinherit_attrs(dst_inode_u, src_dir_u) &&
451 S_ISDIR(dst_inode_u->bi_mode)) {
456 if (is_subdir_for_nlink(src_inode_u)) {
457 src_dir_u->bi_nlink--;
458 dst_dir_u->bi_nlink++;
461 if (dst_inum.inum && is_subdir_for_nlink(dst_inode_u)) {
462 dst_dir_u->bi_nlink--;
463 src_dir_u->bi_nlink += mode == BCH_RENAME_EXCHANGE;
466 if (mode == BCH_RENAME_OVERWRITE)
467 bch2_inode_nlink_dec(trans, dst_inode_u);
469 src_dir_u->bi_mtime = now;
470 src_dir_u->bi_ctime = now;
472 if (src_dir.inum != dst_dir.inum) {
473 dst_dir_u->bi_mtime = now;
474 dst_dir_u->bi_ctime = now;
477 src_inode_u->bi_ctime = now;
480 dst_inode_u->bi_ctime = now;
482 ret = bch2_inode_write(trans, &src_dir_iter, src_dir_u) ?:
483 (src_dir.inum != dst_dir.inum
484 ? bch2_inode_write(trans, &dst_dir_iter, dst_dir_u)
486 bch2_inode_write(trans, &src_inode_iter, src_inode_u) ?:
488 ? bch2_inode_write(trans, &dst_inode_iter, dst_inode_u)
491 bch2_trans_iter_exit(trans, &dst_inode_iter);
492 bch2_trans_iter_exit(trans, &src_inode_iter);
493 bch2_trans_iter_exit(trans, &dst_dir_iter);
494 bch2_trans_iter_exit(trans, &src_dir_iter);