]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/fs-common.c
New upstream snapshot
[bcachefs-tools-debian] / libbcachefs / fs-common.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include "bcachefs.h"
4 #include "acl.h"
5 #include "btree_update.h"
6 #include "dirent.h"
7 #include "fs-common.h"
8 #include "inode.h"
9 #include "xattr.h"
10
11 #include <linux/posix_acl.h>
12
13 int bch2_create_trans(struct btree_trans *trans, u64 dir_inum,
14                       struct bch_inode_unpacked *dir_u,
15                       struct bch_inode_unpacked *new_inode,
16                       const struct qstr *name,
17                       uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
18                       struct posix_acl *default_acl,
19                       struct posix_acl *acl)
20 {
21         struct bch_fs *c = trans->c;
22         struct btree_iter *dir_iter = NULL;
23         struct btree_iter *inode_iter = NULL;
24         struct bch_hash_info hash = bch2_hash_info_init(c, new_inode);
25         u64 now = bch2_current_time(c);
26         u64 dir_offset = 0;
27         int ret;
28
29         dir_iter = bch2_inode_peek(trans, dir_u, dir_inum, BTREE_ITER_INTENT);
30         ret = PTR_ERR_OR_ZERO(dir_iter);
31         if (ret)
32                 goto err;
33
34         bch2_inode_init_late(new_inode, now, uid, gid, mode, rdev, dir_u);
35
36         if (!name)
37                 new_inode->bi_flags |= BCH_INODE_UNLINKED;
38
39         inode_iter = bch2_inode_create(trans, new_inode, U32_MAX);
40         ret = PTR_ERR_OR_ZERO(inode_iter);
41         if (ret)
42                 goto err;
43
44         if (default_acl) {
45                 ret = bch2_set_acl_trans(trans, new_inode, &hash,
46                                          default_acl, ACL_TYPE_DEFAULT);
47                 if (ret)
48                         goto err;
49         }
50
51         if (acl) {
52                 ret = bch2_set_acl_trans(trans, new_inode, &hash,
53                                          acl, ACL_TYPE_ACCESS);
54                 if (ret)
55                         goto err;
56         }
57
58         if (name) {
59                 struct bch_hash_info dir_hash = bch2_hash_info_init(c, dir_u);
60                 dir_u->bi_mtime = dir_u->bi_ctime = now;
61
62                 if (S_ISDIR(new_inode->bi_mode))
63                         dir_u->bi_nlink++;
64
65                 ret = bch2_inode_write(trans, dir_iter, dir_u);
66                 if (ret)
67                         goto err;
68
69                 ret = bch2_dirent_create(trans, dir_inum, &dir_hash,
70                                          mode_to_type(new_inode->bi_mode),
71                                          name, new_inode->bi_inum,
72                                          &dir_offset,
73                                          BCH_HASH_SET_MUST_CREATE);
74                 if (ret)
75                         goto err;
76         }
77
78         if (c->sb.version >= bcachefs_metadata_version_inode_backpointers) {
79                 new_inode->bi_dir               = dir_u->bi_inum;
80                 new_inode->bi_dir_offset        = dir_offset;
81         }
82
83         /* XXX use bch2_btree_iter_set_snapshot() */
84         inode_iter->snapshot = U32_MAX;
85         bch2_btree_iter_set_pos(inode_iter, SPOS(0, new_inode->bi_inum, U32_MAX));
86
87         ret = bch2_inode_write(trans, inode_iter, new_inode);
88 err:
89         bch2_trans_iter_put(trans, inode_iter);
90         bch2_trans_iter_put(trans, dir_iter);
91         return ret;
92 }
93
94 int bch2_link_trans(struct btree_trans *trans, u64 dir_inum,
95                     u64 inum, struct bch_inode_unpacked *dir_u,
96                     struct bch_inode_unpacked *inode_u, const struct qstr *name)
97 {
98         struct bch_fs *c = trans->c;
99         struct btree_iter *dir_iter = NULL, *inode_iter = NULL;
100         struct bch_hash_info dir_hash;
101         u64 now = bch2_current_time(c);
102         u64 dir_offset = 0;
103         int ret;
104
105         inode_iter = bch2_inode_peek(trans, inode_u, inum, BTREE_ITER_INTENT);
106         ret = PTR_ERR_OR_ZERO(inode_iter);
107         if (ret)
108                 goto err;
109
110         inode_u->bi_ctime = now;
111         bch2_inode_nlink_inc(inode_u);
112
113         inode_u->bi_flags |= BCH_INODE_BACKPTR_UNTRUSTED;
114
115         dir_iter = bch2_inode_peek(trans, dir_u, dir_inum, 0);
116         ret = PTR_ERR_OR_ZERO(dir_iter);
117         if (ret)
118                 goto err;
119
120         dir_u->bi_mtime = dir_u->bi_ctime = now;
121
122         dir_hash = bch2_hash_info_init(c, dir_u);
123
124         ret = bch2_dirent_create(trans, dir_inum, &dir_hash,
125                                  mode_to_type(inode_u->bi_mode),
126                                  name, inum, &dir_offset,
127                                  BCH_HASH_SET_MUST_CREATE);
128         if (ret)
129                 goto err;
130
131         if (c->sb.version >= bcachefs_metadata_version_inode_backpointers) {
132                 inode_u->bi_dir         = dir_inum;
133                 inode_u->bi_dir_offset  = dir_offset;
134         }
135
136         ret =   bch2_inode_write(trans, dir_iter, dir_u) ?:
137                 bch2_inode_write(trans, inode_iter, inode_u);
138 err:
139         bch2_trans_iter_put(trans, dir_iter);
140         bch2_trans_iter_put(trans, inode_iter);
141         return ret;
142 }
143
144 int bch2_unlink_trans(struct btree_trans *trans,
145                       u64 dir_inum, struct bch_inode_unpacked *dir_u,
146                       struct bch_inode_unpacked *inode_u,
147                       const struct qstr *name)
148 {
149         struct bch_fs *c = trans->c;
150         struct btree_iter *dir_iter = NULL, *dirent_iter = NULL,
151                           *inode_iter = NULL;
152         struct bch_hash_info dir_hash;
153         u64 inum, now = bch2_current_time(c);
154         struct bkey_s_c k;
155         int ret;
156
157         dir_iter = bch2_inode_peek(trans, dir_u, dir_inum, BTREE_ITER_INTENT);
158         ret = PTR_ERR_OR_ZERO(dir_iter);
159         if (ret)
160                 goto err;
161
162         dir_hash = bch2_hash_info_init(c, dir_u);
163
164         dirent_iter = __bch2_dirent_lookup_trans(trans, dir_inum, &dir_hash,
165                                                  name, BTREE_ITER_INTENT);
166         ret = PTR_ERR_OR_ZERO(dirent_iter);
167         if (ret)
168                 goto err;
169
170         k = bch2_btree_iter_peek_slot(dirent_iter);
171         inum = le64_to_cpu(bkey_s_c_to_dirent(k).v->d_inum);
172
173         inode_iter = bch2_inode_peek(trans, inode_u, inum, BTREE_ITER_INTENT);
174         ret = PTR_ERR_OR_ZERO(inode_iter);
175         if (ret)
176                 goto err;
177
178         dir_u->bi_mtime = dir_u->bi_ctime = inode_u->bi_ctime = now;
179         dir_u->bi_nlink -= S_ISDIR(inode_u->bi_mode);
180         bch2_inode_nlink_dec(inode_u);
181
182         ret =   (S_ISDIR(inode_u->bi_mode)
183                  ? bch2_empty_dir_trans(trans, inum)
184                  : 0) ?:
185                 bch2_dirent_delete_at(trans, &dir_hash, dirent_iter) ?:
186                 bch2_inode_write(trans, dir_iter, dir_u) ?:
187                 bch2_inode_write(trans, inode_iter, inode_u);
188 err:
189         bch2_trans_iter_put(trans, inode_iter);
190         bch2_trans_iter_put(trans, dirent_iter);
191         bch2_trans_iter_put(trans, dir_iter);
192         return ret;
193 }
194
195 bool bch2_reinherit_attrs(struct bch_inode_unpacked *dst_u,
196                           struct bch_inode_unpacked *src_u)
197 {
198         u64 src, dst;
199         unsigned id;
200         bool ret = false;
201
202         for (id = 0; id < Inode_opt_nr; id++) {
203                 if (dst_u->bi_fields_set & (1 << id))
204                         continue;
205
206                 src = bch2_inode_opt_get(src_u, id);
207                 dst = bch2_inode_opt_get(dst_u, id);
208
209                 if (src == dst)
210                         continue;
211
212                 bch2_inode_opt_set(dst_u, id, src);
213                 ret = true;
214         }
215
216         return ret;
217 }
218
219 int bch2_rename_trans(struct btree_trans *trans,
220                       u64 src_dir, struct bch_inode_unpacked *src_dir_u,
221                       u64 dst_dir, struct bch_inode_unpacked *dst_dir_u,
222                       struct bch_inode_unpacked *src_inode_u,
223                       struct bch_inode_unpacked *dst_inode_u,
224                       const struct qstr *src_name,
225                       const struct qstr *dst_name,
226                       enum bch_rename_mode mode)
227 {
228         struct bch_fs *c = trans->c;
229         struct btree_iter *src_dir_iter = NULL, *dst_dir_iter = NULL;
230         struct btree_iter *src_inode_iter = NULL, *dst_inode_iter = NULL;
231         struct bch_hash_info src_hash, dst_hash;
232         u64 src_inode, src_offset, dst_inode, dst_offset;
233         u64 now = bch2_current_time(c);
234         int ret;
235
236         src_dir_iter = bch2_inode_peek(trans, src_dir_u, src_dir,
237                                        BTREE_ITER_INTENT);
238         ret = PTR_ERR_OR_ZERO(src_dir_iter);
239         if (ret)
240                 goto err;
241
242         src_hash = bch2_hash_info_init(c, src_dir_u);
243
244         if (dst_dir != src_dir) {
245                 dst_dir_iter = bch2_inode_peek(trans, dst_dir_u, dst_dir,
246                                                BTREE_ITER_INTENT);
247                 ret = PTR_ERR_OR_ZERO(dst_dir_iter);
248                 if (ret)
249                         goto err;
250
251                 dst_hash = bch2_hash_info_init(c, dst_dir_u);
252         } else {
253                 dst_dir_u = src_dir_u;
254                 dst_hash = src_hash;
255         }
256
257         ret = bch2_dirent_rename(trans,
258                                  src_dir, &src_hash,
259                                  dst_dir, &dst_hash,
260                                  src_name, &src_inode, &src_offset,
261                                  dst_name, &dst_inode, &dst_offset,
262                                  mode);
263         if (ret)
264                 goto err;
265
266         src_inode_iter = bch2_inode_peek(trans, src_inode_u, src_inode,
267                                          BTREE_ITER_INTENT);
268         ret = PTR_ERR_OR_ZERO(src_inode_iter);
269         if (ret)
270                 goto err;
271
272         if (dst_inode) {
273                 dst_inode_iter = bch2_inode_peek(trans, dst_inode_u, dst_inode,
274                                                  BTREE_ITER_INTENT);
275                 ret = PTR_ERR_OR_ZERO(dst_inode_iter);
276                 if (ret)
277                         goto err;
278         }
279
280         if (c->sb.version >= bcachefs_metadata_version_inode_backpointers) {
281                 src_inode_u->bi_dir             = dst_dir_u->bi_inum;
282                 src_inode_u->bi_dir_offset      = dst_offset;
283
284                 if (mode == BCH_RENAME_EXCHANGE) {
285                         dst_inode_u->bi_dir             = src_dir_u->bi_inum;
286                         dst_inode_u->bi_dir_offset      = src_offset;
287                 }
288         }
289
290         if (mode == BCH_RENAME_OVERWRITE) {
291                 if (S_ISDIR(src_inode_u->bi_mode) !=
292                     S_ISDIR(dst_inode_u->bi_mode)) {
293                         ret = -ENOTDIR;
294                         goto err;
295                 }
296
297                 if (S_ISDIR(dst_inode_u->bi_mode) &&
298                     bch2_empty_dir_trans(trans, dst_inode)) {
299                         ret = -ENOTEMPTY;
300                         goto err;
301                 }
302         }
303
304         if (bch2_reinherit_attrs(src_inode_u, dst_dir_u) &&
305             S_ISDIR(src_inode_u->bi_mode)) {
306                 ret = -EXDEV;
307                 goto err;
308         }
309
310         if (mode == BCH_RENAME_EXCHANGE &&
311             bch2_reinherit_attrs(dst_inode_u, src_dir_u) &&
312             S_ISDIR(dst_inode_u->bi_mode)) {
313                 ret = -EXDEV;
314                 goto err;
315         }
316
317         if (S_ISDIR(src_inode_u->bi_mode)) {
318                 src_dir_u->bi_nlink--;
319                 dst_dir_u->bi_nlink++;
320         }
321
322         if (dst_inode && S_ISDIR(dst_inode_u->bi_mode)) {
323                 dst_dir_u->bi_nlink--;
324                 src_dir_u->bi_nlink += mode == BCH_RENAME_EXCHANGE;
325         }
326
327         if (mode == BCH_RENAME_OVERWRITE)
328                 bch2_inode_nlink_dec(dst_inode_u);
329
330         src_dir_u->bi_mtime             = now;
331         src_dir_u->bi_ctime             = now;
332
333         if (src_dir != dst_dir) {
334                 dst_dir_u->bi_mtime     = now;
335                 dst_dir_u->bi_ctime     = now;
336         }
337
338         src_inode_u->bi_ctime           = now;
339
340         if (dst_inode)
341                 dst_inode_u->bi_ctime   = now;
342
343         ret =   bch2_inode_write(trans, src_dir_iter, src_dir_u) ?:
344                 (src_dir != dst_dir
345                  ? bch2_inode_write(trans, dst_dir_iter, dst_dir_u)
346                  : 0 ) ?:
347                 bch2_inode_write(trans, src_inode_iter, src_inode_u) ?:
348                 (dst_inode
349                  ? bch2_inode_write(trans, dst_inode_iter, dst_inode_u)
350                  : 0 );
351 err:
352         bch2_trans_iter_put(trans, dst_inode_iter);
353         bch2_trans_iter_put(trans, src_inode_iter);
354         bch2_trans_iter_put(trans, dst_dir_iter);
355         bch2_trans_iter_put(trans, src_dir_iter);
356         return ret;
357 }