]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/fs-common.c
d543480be111796702d9f5c5f2ced168f4815e16
[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 "subvolume.h"
10 #include "xattr.h"
11
12 #include <linux/posix_acl.h>
13
14 static inline int is_subdir_for_nlink(struct bch_inode_unpacked *inode)
15 {
16         return S_ISDIR(inode->bi_mode) && !inode->bi_subvol;
17 }
18
19 int bch2_create_trans(struct btree_trans *trans,
20                       subvol_inum dir,
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,
28                       unsigned flags)
29 {
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();
36         u64 dir_target;
37         u32 snapshot;
38         unsigned dir_type = mode_to_type(mode);
39         int ret;
40
41         ret = bch2_subvolume_get_snapshot(trans, dir.subvol, &snapshot);
42         if (ret)
43                 goto err;
44
45         ret = bch2_inode_peek(trans, &dir_iter, dir_u, dir, BTREE_ITER_INTENT);
46         if (ret)
47                 goto err;
48
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);
52
53                 if (flags & BCH_CREATE_TMPFILE)
54                         new_inode->bi_flags |= BCH_INODE_UNLINKED;
55
56                 ret = bch2_inode_create(trans, &inode_iter, new_inode, snapshot, cpu);
57                 if (ret)
58                         goto err;
59
60                 snapshot_src = (subvol_inum) { 0 };
61         } else {
62                 /*
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):
66                  */
67
68                 if (!snapshot_src.inum) {
69                         /* Inode wasn't specified, just snapshot: */
70                         struct bch_subvolume s;
71
72                         ret = bch2_subvolume_get(trans, snapshot_src.subvol, true,
73                                                  BTREE_ITER_CACHED, &s);
74                         if (ret)
75                                 goto err;
76
77                         snapshot_src.inum = le64_to_cpu(s.inode);
78                 }
79
80                 ret = bch2_inode_peek(trans, &inode_iter, new_inode, snapshot_src,
81                                       BTREE_ITER_INTENT);
82                 if (ret)
83                         goto err;
84
85                 if (new_inode->bi_subvol != snapshot_src.subvol) {
86                         /* Not a subvolume root: */
87                         ret = -EINVAL;
88                         goto err;
89                 }
90
91                 /*
92                  * If we're not root, we have to own the subvolume being
93                  * snapshotted:
94                  */
95                 if (uid && new_inode->bi_uid != uid) {
96                         ret = -EPERM;
97                         goto err;
98                 }
99
100                 flags |= BCH_CREATE_SUBVOL;
101         }
102
103         new_inum.inum   = new_inode->bi_inum;
104         dir_target      = new_inode->bi_inum;
105
106         if (flags & BCH_CREATE_SUBVOL) {
107                 u32 new_subvol, dir_snapshot;
108
109                 ret = bch2_subvolume_create(trans, new_inode->bi_inum,
110                                             snapshot_src.subvol,
111                                             &new_subvol, &snapshot,
112                                             (flags & BCH_CREATE_SNAPSHOT_RO) != 0);
113                 if (ret)
114                         goto err;
115
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;
121
122                 ret = bch2_subvolume_get_snapshot(trans, dir.subvol, &dir_snapshot);
123                 if (ret)
124                         goto err;
125
126                 bch2_btree_iter_set_snapshot(&dir_iter, dir_snapshot);
127                 ret = bch2_btree_iter_traverse(&dir_iter);
128                 if (ret)
129                         goto err;
130         }
131
132         if (!(flags & BCH_CREATE_SNAPSHOT)) {
133                 if (default_acl) {
134                         ret = bch2_set_acl_trans(trans, new_inum, new_inode,
135                                                  default_acl, ACL_TYPE_DEFAULT);
136                         if (ret)
137                                 goto err;
138                 }
139
140                 if (acl) {
141                         ret = bch2_set_acl_trans(trans, new_inum, new_inode,
142                                                  acl, ACL_TYPE_ACCESS);
143                         if (ret)
144                                 goto err;
145                 }
146         }
147
148         if (!(flags & BCH_CREATE_TMPFILE)) {
149                 struct bch_hash_info dir_hash = bch2_hash_info_init(c, dir_u);
150                 u64 dir_offset;
151
152                 if (is_subdir_for_nlink(new_inode))
153                         dir_u->bi_nlink++;
154                 dir_u->bi_mtime = dir_u->bi_ctime = now;
155
156                 ret = bch2_inode_write(trans, &dir_iter, dir_u);
157                 if (ret)
158                         goto err;
159
160                 ret = bch2_dirent_create(trans, dir, &dir_hash,
161                                          dir_type,
162                                          name,
163                                          dir_target,
164                                          &dir_offset,
165                                          BCH_HASH_SET_MUST_CREATE);
166                 if (ret)
167                         goto err;
168
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;
172                 }
173         }
174
175         inode_iter.flags &= ~BTREE_ITER_ALL_SNAPSHOTS;
176         bch2_btree_iter_set_snapshot(&inode_iter, snapshot);
177
178         ret   = bch2_btree_iter_traverse(&inode_iter) ?:
179                 bch2_inode_write(trans, &inode_iter, new_inode);
180 err:
181         bch2_trans_iter_exit(trans, &inode_iter);
182         bch2_trans_iter_exit(trans, &dir_iter);
183         return ret;
184 }
185
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)
190 {
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);
196         u64 dir_offset = 0;
197         int ret;
198
199         if (dir.subvol != inum.subvol)
200                 return -EXDEV;
201
202         ret = bch2_inode_peek(trans, &inode_iter, inode_u, inum, BTREE_ITER_INTENT);
203         if (ret)
204                 goto err;
205
206         inode_u->bi_ctime = now;
207         bch2_inode_nlink_inc(inode_u);
208
209         ret = bch2_inode_peek(trans, &dir_iter, dir_u, dir, BTREE_ITER_INTENT);
210         if (ret)
211                 goto err;
212
213         dir_u->bi_mtime = dir_u->bi_ctime = now;
214
215         dir_hash = bch2_hash_info_init(c, dir_u);
216
217         ret = bch2_dirent_create(trans, dir, &dir_hash,
218                                  mode_to_type(inode_u->bi_mode),
219                                  name, inum.inum, &dir_offset,
220                                  BCH_HASH_SET_MUST_CREATE);
221         if (ret)
222                 goto err;
223
224         if (c->sb.version >= bcachefs_metadata_version_inode_backpointers) {
225                 inode_u->bi_dir         = dir.inum;
226                 inode_u->bi_dir_offset  = dir_offset;
227         }
228
229         ret =   bch2_inode_write(trans, &dir_iter, dir_u) ?:
230                 bch2_inode_write(trans, &inode_iter, inode_u);
231 err:
232         bch2_trans_iter_exit(trans, &dir_iter);
233         bch2_trans_iter_exit(trans, &inode_iter);
234         return ret;
235 }
236
237 int bch2_unlink_trans(struct btree_trans *trans,
238                       subvol_inum dir,
239                       struct bch_inode_unpacked *dir_u,
240                       struct bch_inode_unpacked *inode_u,
241                       const struct qstr *name,
242                       bool deleting_snapshot)
243 {
244         struct bch_fs *c = trans->c;
245         struct btree_iter dir_iter = { NULL };
246         struct btree_iter dirent_iter = { NULL };
247         struct btree_iter inode_iter = { NULL };
248         struct bch_hash_info dir_hash;
249         subvol_inum inum;
250         u64 now = bch2_current_time(c);
251         struct bkey_s_c k;
252         int ret;
253
254         ret = bch2_inode_peek(trans, &dir_iter, dir_u, dir, BTREE_ITER_INTENT);
255         if (ret)
256                 goto err;
257
258         dir_hash = bch2_hash_info_init(c, dir_u);
259
260         ret = __bch2_dirent_lookup_trans(trans, &dirent_iter, dir, &dir_hash,
261                                          name, &inum, BTREE_ITER_INTENT);
262         if (ret)
263                 goto err;
264
265         ret = bch2_inode_peek(trans, &inode_iter, inode_u, inum,
266                               BTREE_ITER_INTENT);
267         if (ret)
268                 goto err;
269
270         if (!deleting_snapshot && S_ISDIR(inode_u->bi_mode)) {
271                 ret = bch2_empty_dir_trans(trans, inum);
272                 if (ret)
273                         goto err;
274         }
275
276         if (deleting_snapshot && !inode_u->bi_subvol) {
277                 ret = -ENOENT;
278                 goto err;
279         }
280
281         if (deleting_snapshot || inode_u->bi_subvol) {
282                 ret = bch2_subvolume_unlink(trans, inode_u->bi_subvol);
283                 if (ret)
284                         goto err;
285
286                 k = bch2_btree_iter_peek_slot(&dirent_iter);
287                 ret = bkey_err(k);
288                 if (ret)
289                         goto err;
290
291                 /*
292                  * If we're deleting a subvolume, we need to really delete the
293                  * dirent, not just emit a whiteout in the current snapshot:
294                  */
295                 bch2_btree_iter_set_snapshot(&dirent_iter, k.k->p.snapshot);
296                 ret = bch2_btree_iter_traverse(&dirent_iter);
297                 if (ret)
298                         goto err;
299         } else {
300                 bch2_inode_nlink_dec(inode_u);
301         }
302
303         if (inode_u->bi_dir             == dirent_iter.pos.inode &&
304             inode_u->bi_dir_offset      == dirent_iter.pos.offset) {
305                 inode_u->bi_dir         = 0;
306                 inode_u->bi_dir_offset  = 0;
307         }
308
309         dir_u->bi_mtime = dir_u->bi_ctime = inode_u->bi_ctime = now;
310         dir_u->bi_nlink -= is_subdir_for_nlink(inode_u);
311
312         ret =   bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
313                                     &dir_hash, &dirent_iter,
314                                     BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
315                 bch2_inode_write(trans, &dir_iter, dir_u) ?:
316                 bch2_inode_write(trans, &inode_iter, inode_u);
317 err:
318         bch2_trans_iter_exit(trans, &inode_iter);
319         bch2_trans_iter_exit(trans, &dirent_iter);
320         bch2_trans_iter_exit(trans, &dir_iter);
321         return ret;
322 }
323
324 bool bch2_reinherit_attrs(struct bch_inode_unpacked *dst_u,
325                           struct bch_inode_unpacked *src_u)
326 {
327         u64 src, dst;
328         unsigned id;
329         bool ret = false;
330
331         for (id = 0; id < Inode_opt_nr; id++) {
332                 /* Skip attributes that were explicitly set on this inode */
333                 if (dst_u->bi_fields_set & (1 << id))
334                         continue;
335
336                 src = bch2_inode_opt_get(src_u, id);
337                 dst = bch2_inode_opt_get(dst_u, id);
338
339                 if (src == dst)
340                         continue;
341
342                 bch2_inode_opt_set(dst_u, id, src);
343                 ret = true;
344         }
345
346         return ret;
347 }
348
349 int bch2_rename_trans(struct btree_trans *trans,
350                       subvol_inum src_dir, struct bch_inode_unpacked *src_dir_u,
351                       subvol_inum dst_dir, struct bch_inode_unpacked *dst_dir_u,
352                       struct bch_inode_unpacked *src_inode_u,
353                       struct bch_inode_unpacked *dst_inode_u,
354                       const struct qstr *src_name,
355                       const struct qstr *dst_name,
356                       enum bch_rename_mode mode)
357 {
358         struct bch_fs *c = trans->c;
359         struct btree_iter src_dir_iter = { NULL };
360         struct btree_iter dst_dir_iter = { NULL };
361         struct btree_iter src_inode_iter = { NULL };
362         struct btree_iter dst_inode_iter = { NULL };
363         struct bch_hash_info src_hash, dst_hash;
364         subvol_inum src_inum, dst_inum;
365         u64 src_offset, dst_offset;
366         u64 now = bch2_current_time(c);
367         int ret;
368
369         ret = bch2_inode_peek(trans, &src_dir_iter, src_dir_u, src_dir,
370                               BTREE_ITER_INTENT);
371         if (ret)
372                 goto err;
373
374         src_hash = bch2_hash_info_init(c, src_dir_u);
375
376         if (dst_dir.inum        != src_dir.inum ||
377             dst_dir.subvol      != src_dir.subvol) {
378                 ret = bch2_inode_peek(trans, &dst_dir_iter, dst_dir_u, dst_dir,
379                                       BTREE_ITER_INTENT);
380                 if (ret)
381                         goto err;
382
383                 dst_hash = bch2_hash_info_init(c, dst_dir_u);
384         } else {
385                 dst_dir_u = src_dir_u;
386                 dst_hash = src_hash;
387         }
388
389         ret = bch2_dirent_rename(trans,
390                                  src_dir, &src_hash,
391                                  dst_dir, &dst_hash,
392                                  src_name, &src_inum, &src_offset,
393                                  dst_name, &dst_inum, &dst_offset,
394                                  mode);
395         if (ret)
396                 goto err;
397
398         ret = bch2_inode_peek(trans, &src_inode_iter, src_inode_u, src_inum,
399                               BTREE_ITER_INTENT);
400         if (ret)
401                 goto err;
402
403         if (dst_inum.inum) {
404                 ret = bch2_inode_peek(trans, &dst_inode_iter, dst_inode_u, dst_inum,
405                                       BTREE_ITER_INTENT);
406                 if (ret)
407                         goto err;
408         }
409
410         if (c->sb.version >= bcachefs_metadata_version_inode_backpointers) {
411                 src_inode_u->bi_dir             = dst_dir_u->bi_inum;
412                 src_inode_u->bi_dir_offset      = dst_offset;
413
414                 if (mode == BCH_RENAME_EXCHANGE) {
415                         dst_inode_u->bi_dir             = src_dir_u->bi_inum;
416                         dst_inode_u->bi_dir_offset      = src_offset;
417                 }
418
419                 if (mode == BCH_RENAME_OVERWRITE &&
420                     dst_inode_u->bi_dir         == dst_dir_u->bi_inum &&
421                     dst_inode_u->bi_dir_offset  == src_offset) {
422                         dst_inode_u->bi_dir             = 0;
423                         dst_inode_u->bi_dir_offset      = 0;
424                 }
425         }
426
427         if (mode == BCH_RENAME_OVERWRITE) {
428                 if (S_ISDIR(src_inode_u->bi_mode) !=
429                     S_ISDIR(dst_inode_u->bi_mode)) {
430                         ret = -ENOTDIR;
431                         goto err;
432                 }
433
434                 if (S_ISDIR(dst_inode_u->bi_mode) &&
435                     bch2_empty_dir_trans(trans, dst_inum)) {
436                         ret = -ENOTEMPTY;
437                         goto err;
438                 }
439         }
440
441         if (bch2_reinherit_attrs(src_inode_u, dst_dir_u) &&
442             S_ISDIR(src_inode_u->bi_mode)) {
443                 ret = -EXDEV;
444                 goto err;
445         }
446
447         if (mode == BCH_RENAME_EXCHANGE &&
448             bch2_reinherit_attrs(dst_inode_u, src_dir_u) &&
449             S_ISDIR(dst_inode_u->bi_mode)) {
450                 ret = -EXDEV;
451                 goto err;
452         }
453
454         if (is_subdir_for_nlink(src_inode_u)) {
455                 src_dir_u->bi_nlink--;
456                 dst_dir_u->bi_nlink++;
457         }
458
459         if (dst_inum.inum && is_subdir_for_nlink(dst_inode_u)) {
460                 dst_dir_u->bi_nlink--;
461                 src_dir_u->bi_nlink += mode == BCH_RENAME_EXCHANGE;
462         }
463
464         if (mode == BCH_RENAME_OVERWRITE)
465                 bch2_inode_nlink_dec(dst_inode_u);
466
467         src_dir_u->bi_mtime             = now;
468         src_dir_u->bi_ctime             = now;
469
470         if (src_dir.inum != dst_dir.inum) {
471                 dst_dir_u->bi_mtime     = now;
472                 dst_dir_u->bi_ctime     = now;
473         }
474
475         src_inode_u->bi_ctime           = now;
476
477         if (dst_inum.inum)
478                 dst_inode_u->bi_ctime   = now;
479
480         ret =   bch2_inode_write(trans, &src_dir_iter, src_dir_u) ?:
481                 (src_dir.inum != dst_dir.inum
482                  ? bch2_inode_write(trans, &dst_dir_iter, dst_dir_u)
483                  : 0 ) ?:
484                 bch2_inode_write(trans, &src_inode_iter, src_inode_u) ?:
485                 (dst_inum.inum
486                  ? bch2_inode_write(trans, &dst_inode_iter, dst_inode_u)
487                  : 0 );
488 err:
489         bch2_trans_iter_exit(trans, &dst_inode_iter);
490         bch2_trans_iter_exit(trans, &src_inode_iter);
491         bch2_trans_iter_exit(trans, &dst_dir_iter);
492         bch2_trans_iter_exit(trans, &src_dir_iter);
493         return ret;
494 }