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