-
-
Notifications
You must be signed in to change notification settings - Fork 195
/
document-portal-fuse.c
3554 lines (2950 loc) · 92.4 KB
/
document-portal-fuse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "config.h"
#define FUSE_USE_VERSION 35
#include <glib-unix.h>
#include <fuse_lowlevel.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <assert.h>
#include <glib/gprintf.h>
#include <gio/gio.h>
#include <pthread.h>
#ifdef HAVE_SYS_STATFS_H
#include <sys/statfs.h>
#endif
#include <sys/types.h>
#ifdef HAVE_SYS_MOUNT_H
#include <sys/mount.h>
#endif
#ifdef HAVE_SYS_XATTR_H
#include <sys/xattr.h>
#endif
#ifdef HAVE_SYS_EXTATTR_H
#include <sys/extattr.h>
#endif
#include <sys/time.h>
#include <sys/resource.h>
#include "document-portal-fuse.h"
#include "document-store.h"
#include "src/xdp-utils.h"
#ifndef O_FSYNC
#define O_FSYNC O_SYNC
#endif
#ifndef ENODATA
#define ENODATA ENOATTR
#endif
/* Inode ownership model
*
* The document portal exposes something as a filesystem that it
* doesn't have full control over. For instance at any point some
* other process can rename an exposed file on the real filesystem and
* we won't be told about this. This means that in general we always
* return 0 for the cacheable lifetimes of entries and file attributes.
* (Except for the virtual directories we have full control of, the
* below only discusses real files).
*
* However, even though we don't have full control of the underlying
* filesystem the *kernel* has. This means we can used that to get
* the correct semantics.
*
* For example, assume that a directory is held opened by a process
* (for example, it could be the CWD of the process). When we opened
* the directory via a LOOKUP operation we returned an inode to it,
* and for as long as the kernel has this inode around (i.e. until it
* sent a FORGET message) it can send operations on this inode without
* looking it up again. For example if the above process used a
* relative path.
*
* Now, consider the case where the app chdir():ed into the fuse
* directory, but after that the backing directory was renamed ouside
* the fuse filesystem. The fuse inode representation for the inode
* cannot be the directory name, because the expected semantics is
* that further relative pathnames from the app will still resolve
* to the same directory independent of its location in the tree.
*
* The way we do this is to keep a O_PATH file descriptor around for
* each underlying inode. This is represented by the XdpPhysicalInode
* type and we have a hashtable from backing dev+inode to a these
* so that we can use one fd per backing inode even when the file
* is visible in many places.
*
* Since we don't do any caching, each LOOKUP operation will do a
* statat() on the underlying filesystem. However, we then use the
* result of that to lookup (via backing dev+ino) the previous inode
* (as long as it still lives) if the backing file was unchanged.
*
* One problem with this approach is that the kernel tends to keep
* inodes alive for a very long time even if they are *only*
* references by the dcache (event though we will not really use the
* dcache info due to the 0 valid time). This is unfortunate, because
* it means we will keep a lot of file descriptor open. But, we
* can't know if the kernel needs the inode for some non-dcache use
* so we can't close the file descriptors.
*
* To work around this we regularly emit entry invalidation calls
* to the kernel, which will make it forget the inodes that are
* only pinned by the dcache.
*/
#define NON_DOC_DIR_PERMS 0500
#define DOC_DIR_PERMS_FILE 0700
#define DOC_DIR_PERMS_DIR 0500
static GThread *fuse_thread = NULL;
static struct fuse_session *session = NULL;
G_LOCK_DEFINE (session);
static char *mount_path = NULL;
static pthread_t fuse_pthread = 0;
static uid_t my_uid;
static gid_t my_gid;
/* from libfuse */
#define FUSE_UNKNOWN_INO 0xffffffff
#define BY_APP_NAME "by-app"
typedef struct {
ino_t ino;
dev_t dev;
} DevIno;
typedef enum {
XDP_DOMAIN_ROOT,
XDP_DOMAIN_BY_APP,
XDP_DOMAIN_APP,
XDP_DOMAIN_DOCUMENT,
} XdpDomainType;
typedef struct _XdpDomain XdpDomain;
typedef struct _XdpInode XdpInode;
struct _XdpDomain {
gint ref_count; /* atomic */
XdpDomainType type;
XdpDomain *parent;
XdpInode *parent_inode; /* Inode of the parent domain (NULL for root) */
char *doc_id; /* NULL for root, by-app, app */
char *app_id; /* NULL for root, by-app, non-app id */
/* root: by docid
* app: by docid
* by_app: by app
* document: by physical
*/
GHashTable *inodes; /* Protected by domain_inodes */
/* Below only used for XDP_DOMAIN_DOCUMENT */
char *doc_path; /* path to the directory the files are in */
char *doc_file; /* != NULL for non-directory documents */
guint64 doc_dir_device;
guint64 doc_dir_inode;
guint32 doc_flags;
int doc_queued_invalidate; /* Access atomically, 1 if queued invalidate */
/* Below is mutable, protected by mutex */
GMutex tempfile_mutex;
GHashTable *tempfiles; /* Name -> physical */
};
static void xdp_domain_unref (XdpDomain *domain);
G_DEFINE_AUTOPTR_CLEANUP_FUNC (XdpDomain, xdp_domain_unref)
G_LOCK_DEFINE (domain_inodes);
typedef struct {
gint ref_count; /* atomic */
DevIno backing_devino;
int fd; /* O_PATH fd */
} XdpPhysicalInode;
static XdpPhysicalInode *xdp_physical_inode_ref (XdpPhysicalInode *inode);
static void xdp_physical_inode_unref (XdpPhysicalInode *inode);
G_DEFINE_AUTOPTR_CLEANUP_FUNC (XdpPhysicalInode, xdp_physical_inode_unref)
typedef struct {
gint ref_count; /* atomic */
char *name; /* This changes over time (i.e. in renames)
protected by domain->tempfile_mutex,
used as key in domain->tempfiles */
char *tempname; /* Real filename on disk.
This can be NULLed to avoid unlink at finalize */
XdpInode *inode;
} XdpTempfile;
static XdpTempfile *xdp_tempfile_ref (XdpTempfile *tempfile);
static void xdp_tempfile_unref (XdpTempfile *tempfile);
G_DEFINE_AUTOPTR_CLEANUP_FUNC (XdpTempfile, xdp_tempfile_unref)
struct _XdpInode {
guint64 ino;
gint ref_count; /* atomic, includes one ref if kernel_ref_count != 0 */
gint kernel_ref_count; /* atomic */
XdpDomain *domain;
/* The below are only used for XDP_DOMAIN_DOCUMENT inodes */
XdpPhysicalInode *physical;
/* The root of the domain, or NULL for the domain. We use this to
* keep the root document inode alive so that when the kernel
* forgets it and then looks it up we will not get a new inode and
* thus a new domain. */
XdpInode *domain_root_inode;
};
typedef struct {
int fd;
} XdpFile;
typedef struct {
DIR *dir;
struct dirent *entry;
off_t offset;
char *dirbuf;
gsize dirbuf_size;
} XdpDir;
XdpInode *root_inode;
XdpInode *by_app_inode;
static XdpInode *xdp_inode_ref (XdpInode *inode);
static void xdp_inode_unref (XdpInode *inode);
/* Lookup by inode for verification */
static GHashTable *all_inodes; /* guint64 -> XdpInode */
static guint64 next_virtual_inode = FUSE_ROOT_ID; /* root is the first inode created, so it gets this */
G_LOCK_DEFINE (all_inodes);
G_DEFINE_AUTOPTR_CLEANUP_FUNC (XdpInode, xdp_inode_unref)
static int ensure_docdir_inode (XdpInode *parent,
int o_path_fd_in, /* Takes ownership */
struct fuse_entry_param *e,
XdpInode **inode_out);
static gboolean
app_can_write_doc (PermissionDbEntry *entry, const char *app_id)
{
if (app_id == NULL)
return TRUE;
if (document_entry_has_permissions_by_app_id (entry, app_id, DOCUMENT_PERMISSION_FLAGS_WRITE))
return TRUE;
return FALSE;
}
static gboolean
app_can_see_doc (PermissionDbEntry *entry, const char *app_id)
{
if (app_id == NULL)
return TRUE;
if (document_entry_has_permissions_by_app_id (entry, app_id, DOCUMENT_PERMISSION_FLAGS_READ))
return TRUE;
return FALSE;
}
static char *
fd_to_path (int fd)
{
return g_strdup_printf ("/proc/self/fd/%d", fd);
}
static char *
open_flags_to_string (int flags)
{
GString *s;
const char *mode;
switch (flags & O_ACCMODE)
{
case O_RDONLY:
mode = "RDONLY";
break;
case O_WRONLY:
mode = "WRONLY";
break;
case O_RDWR:
default:
mode = "RDWR";
break;
}
s = g_string_new (mode);
if (flags & O_NONBLOCK)
g_string_append (s, ",NONBLOCK");
if (flags & O_APPEND)
g_string_append (s, ",APPEND");
if (flags & O_SYNC)
g_string_append (s, ",SYNC");
if (flags & O_ASYNC)
g_string_append (s, ",ASYNC");
if (flags & O_FSYNC)
g_string_append (s, ",FSYNC");
#ifdef O_DSYNC
if (flags & O_DSYNC)
g_string_append (s, ",DSYNC");
#endif
if (flags & O_CREAT)
g_string_append (s, ",CREAT");
if (flags & O_TRUNC)
g_string_append (s, ",TRUNC");
if (flags & O_EXCL)
g_string_append (s, ",EXCL");
if (flags & O_CLOEXEC)
g_string_append (s, ",CLOEXEC");
if (flags & O_DIRECT)
g_string_append (s, ",DIRECT");
#ifdef O_LARGEFILE
if (flags & O_LARGEFILE)
g_string_append (s, ",LARGEFILE");
#endif
#ifdef O_NOATIME
if (flags & O_NOATIME)
g_string_append (s, ",NOATIME");
#endif
if (flags & O_NOCTTY)
g_string_append (s, ",NOCTTY");
if (flags & O_PATH)
g_string_append (s, ",PATH");
#ifdef O_TMPFILE
if (flags & O_TMPFILE)
g_string_append (s, ",TMPFILE");
#endif
return g_string_free (s, FALSE);
}
static char *
setattr_flags_to_string (int flags)
{
GString *s = g_string_new ("");
if (flags & FUSE_SET_ATTR_MODE)
g_string_append (s, "MODE,");
if (flags & FUSE_SET_ATTR_UID)
g_string_append (s, "UID,");
if (flags & FUSE_SET_ATTR_GID)
g_string_append (s, "GID,");
if (flags & FUSE_SET_ATTR_SIZE)
g_string_append (s, "SIZE,");
if (flags & FUSE_SET_ATTR_ATIME)
g_string_append (s, "ATIME,");
if (flags & FUSE_SET_ATTR_MTIME)
g_string_append (s, "MTIME,");
if (flags & FUSE_SET_ATTR_ATIME_NOW)
g_string_append (s, "ATIME_NOW,");
if (flags & FUSE_SET_ATTR_MTIME_NOW)
g_string_append (s, "MTIME_NOW,");
/* Remove last comma */
if (s->len > 0)
g_string_truncate (s, s->len - 1);
return g_string_free (s, FALSE);
}
static char *
renameat2_flags_to_string (int flags)
{
#if HAVE_RENAMEAT2
GString *s = g_string_new ("");
if (flags & RENAME_EXCHANGE)
g_string_append (s, "EXCHANGE,");
if (flags & RENAME_NOREPLACE)
g_string_append (s, "NOREPLACE,");
if (flags & RENAME_WHITEOUT)
g_string_append (s, "WHITEOUT,");
/* Remove last comma */
if (s->len > 0)
g_string_truncate (s, s->len - 1);
return g_string_free (s, FALSE);
#else
return g_strdup_printf ("%#x", flags);
#endif
}
static guint
devino_hash (gconstpointer key)
{
DevIno *devino = (DevIno *)key;
return (devino->ino >> 2) ^ devino->dev;
}
static gboolean
devino_equal (gconstpointer _a,
gconstpointer _b)
{
DevIno *a = (DevIno *)_a;
DevIno *b = (DevIno *)_b;
return a->ino == b->ino && a->dev == b->dev;
}
/* Lookup by physical backing devino */
static GHashTable *physical_inodes;
G_LOCK_DEFINE (physical_inodes);
/* Takes ownership of the o_path fd if passed in */
static XdpPhysicalInode *
ensure_physical_inode (dev_t dev, ino_t ino, int o_path_fd)
{
DevIno devino = {ino, dev};
XdpPhysicalInode *inode = NULL;
G_LOCK (physical_inodes);
inode = g_hash_table_lookup (physical_inodes, &devino);
if (inode != NULL)
{
inode = xdp_physical_inode_ref (inode);
close (o_path_fd);
}
else
{
/* Takes ownership of fd */
inode = g_new0 (XdpPhysicalInode, 1);
inode->ref_count = 1;
inode->fd = o_path_fd;
inode->backing_devino = devino;
g_hash_table_insert (physical_inodes, &inode->backing_devino, inode);
}
G_UNLOCK (physical_inodes);
return inode;
}
static XdpPhysicalInode *
xdp_physical_inode_ref (XdpPhysicalInode *inode)
{
g_atomic_int_inc (&inode->ref_count);
return inode;
}
static void
xdp_physical_inode_unref (XdpPhysicalInode *inode)
{
gint old_ref;
/* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
retry_atomic_decrement1:
old_ref = g_atomic_int_get (&inode->ref_count);
if (old_ref > 1)
{
if (!g_atomic_int_compare_and_exchange ((int *) &inode->ref_count, old_ref, old_ref - 1))
goto retry_atomic_decrement1;
}
else
{
if (old_ref <= 0)
{
g_warning ("Can't unref dead inode");
return;
}
/* Might be revived from physical_inodes hash by this time, so protect by lock */
G_LOCK (physical_inodes);
if (!g_atomic_int_compare_and_exchange ((int *) &inode->ref_count, old_ref, old_ref - 1))
{
G_UNLOCK (physical_inodes);
goto retry_atomic_decrement1;
}
g_hash_table_remove (physical_inodes, &inode->backing_devino);
G_UNLOCK (physical_inodes);
close (inode->fd);
g_free (inode);
}
}
static XdpDomain *
xdp_domain_ref (XdpDomain *domain)
{
g_atomic_int_inc (&domain->ref_count);
return domain;
}
static void
xdp_domain_unref (XdpDomain *domain)
{
if (g_atomic_int_dec_and_test (&domain->ref_count))
{
g_free (domain->doc_id);
g_free (domain->app_id);
g_free (domain->doc_path);
g_free (domain->doc_file);
if (domain->inodes)
g_assert (g_hash_table_size (domain->inodes) == 0);
g_clear_pointer (&domain->inodes, g_hash_table_unref);
g_clear_pointer (&domain->parent, xdp_domain_unref);
g_clear_pointer (&domain->parent_inode, xdp_inode_unref);
g_clear_pointer (&domain->tempfiles, g_hash_table_unref);
g_mutex_clear (&domain->tempfile_mutex);
g_free (domain);
}
}
static gboolean
xdp_domain_is_virtual_type (XdpDomain *domain)
{
return
domain->type == XDP_DOMAIN_ROOT ||
domain->type == XDP_DOMAIN_BY_APP ||
domain->type == XDP_DOMAIN_APP;
}
static XdpDomain *
_xdp_domain_new (XdpDomainType type)
{
XdpDomain *domain = g_new0 (XdpDomain, 1);
domain->ref_count = 1;
domain->type = type;
g_mutex_init (&domain->tempfile_mutex);
return domain;
}
static XdpDomain *
xdp_domain_new_root (void)
{
XdpDomain *domain = _xdp_domain_new (XDP_DOMAIN_ROOT);
domain->inodes = g_hash_table_new (g_str_hash, g_str_equal);
return domain;
}
static XdpDomain *
xdp_domain_new_by_app (XdpInode *root_inode)
{
XdpDomain *root_domain = root_inode->domain;
XdpDomain *domain = _xdp_domain_new (XDP_DOMAIN_BY_APP);
domain->parent = xdp_domain_ref (root_domain);
domain->parent_inode = xdp_inode_ref (root_inode);
domain->inodes = g_hash_table_new (g_str_hash, g_str_equal);
return domain;
}
static XdpDomain *
xdp_domain_new_app (XdpInode *parent_inode,
const char *app_id)
{
XdpDomain *parent = parent_inode->domain;
XdpDomain *domain = _xdp_domain_new (XDP_DOMAIN_APP);
domain->parent = xdp_domain_ref (parent);
domain->parent_inode = xdp_inode_ref (parent_inode);
domain->app_id = g_strdup (app_id);
domain->inodes = g_hash_table_new (g_str_hash, g_str_equal);
return domain;
}
static gboolean
xdp_document_domain_is_dir (XdpDomain *domain)
{
return (domain->doc_flags & DOCUMENT_ENTRY_FLAG_DIRECTORY) != 0;
}
static XdpDomain *
xdp_domain_new_document (XdpDomain *parent,
const char *doc_id,
PermissionDbEntry *doc_entry)
{
XdpDomain *domain = _xdp_domain_new (XDP_DOMAIN_DOCUMENT);
const char *db_path;
domain->parent = xdp_domain_ref (parent);
domain->doc_id = g_strdup (doc_id);
domain->app_id = g_strdup (parent->app_id);
domain->inodes = g_hash_table_new (g_direct_hash, g_direct_equal);
domain->doc_flags = document_entry_get_flags (doc_entry);
domain->doc_dir_device = document_entry_get_device (doc_entry);
domain->doc_dir_inode = document_entry_get_inode (doc_entry);
db_path = document_entry_get_path (doc_entry);
if (xdp_document_domain_is_dir (domain))
{
domain->doc_path = g_strdup (db_path);
domain->doc_file = g_path_get_basename (db_path);
}
else
{
domain->doc_path = g_path_get_dirname (db_path);
domain->doc_file = g_path_get_basename (db_path);
}
domain->tempfiles = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, (GDestroyNotify)xdp_tempfile_unref);
return domain;
}
static gboolean
xdp_document_domain_can_see (XdpDomain *domain)
{
if (domain->app_id != NULL)
{
g_autoptr(PermissionDbEntry) entry = xdp_lookup_doc (domain->doc_id);
if (entry == NULL ||
!app_can_see_doc (entry, domain->app_id))
return FALSE;
}
return TRUE;
}
static gboolean
xdp_document_domain_can_write (XdpDomain *domain)
{
if (domain->app_id != NULL)
{
g_autoptr(PermissionDbEntry) entry = xdp_lookup_doc (domain->doc_id);
if (entry == NULL ||
!app_can_write_doc (entry, domain->app_id))
return FALSE;
}
return TRUE;
}
static char **
xdp_domain_get_inode_keys_as_string (XdpDomain *domain)
{
char **res;
guint length, i;
g_assert (domain->type == XDP_DOMAIN_BY_APP);
G_LOCK (domain_inodes);
res = (char **)g_hash_table_get_keys_as_array (domain->inodes, &length);
for (i = 0; i < length; i++)
res[i] = g_strdup (res[i]);
G_UNLOCK (domain_inodes);
return res;
}
static XdpTempfile *
xdp_tempfile_ref (XdpTempfile *tempfile)
{
g_atomic_int_inc (&tempfile->ref_count);
return tempfile;
}
static XdpTempfile *
xdp_tempfile_new (XdpInode *inode,
const char *name,
const char *tempname)
{
XdpTempfile *tempfile = g_new0 (XdpTempfile, 1);
tempfile->ref_count = 1;
tempfile->inode = xdp_inode_ref (inode);
tempfile->name = g_strdup (name);
tempfile->tempname = g_strdup (tempname);
return tempfile;
}
static void
xdp_tempfile_unref (XdpTempfile *tempfile)
{
if (g_atomic_int_dec_and_test (&tempfile->ref_count))
{
if (tempfile->tempname)
{
g_autofree char *temppath = g_build_filename (tempfile->inode->domain->doc_path, tempfile->tempname, NULL);
(void)unlink (temppath);
}
g_free (tempfile->name);
g_free (tempfile->tempname);
g_clear_pointer (&tempfile->inode, xdp_inode_unref);
g_free (tempfile);
}
}
static XdpInode *
_xdp_inode_new (void)
{
XdpInode *inode = g_new0 (XdpInode, 1);
inode->ref_count = 1;
inode->kernel_ref_count = 0;
return inode;
}
/* We try to create persistent inode nr based on the backing device and inode nrs, as
* well as the doc/app id (since the same backing dev/ino should be different inodes
* in the fuse filesystem). We do this by hashing the data to generate a value.
* For non-phsyical files or accidental collisions we just pick a free number
* by incrementing.
*/
static guint64
generate_persistent_ino (DevIno *backing_devino,
const char *doc_id,
const char *app_id)
{
g_autoptr(GChecksum) checksum = g_checksum_new (G_CHECKSUM_MD5);
guchar digest[64];
gsize digest_len = 64;
guint64 res;
g_checksum_update (checksum, (guchar *)backing_devino, sizeof (DevIno));
if (doc_id)
g_checksum_update (checksum, (guchar *)doc_id, strlen (doc_id));
if (app_id)
g_checksum_update (checksum, (guchar *)app_id, strlen (app_id));
g_checksum_get_digest (checksum, digest, &digest_len);
res = *(guint64 *)digest;
if (res == FUSE_ROOT_ID || res == 0)
res = FUSE_ROOT_ID + 1;
return res;
}
/* takes ownership of fd */
static XdpInode *
xdp_inode_new (XdpDomain *domain,
XdpPhysicalInode *physical)
{
XdpInode *inode = _xdp_inode_new ();
inode->domain = xdp_domain_ref (domain);
guint64 persistent_ino, try_ino;
if (physical)
{
inode->physical = xdp_physical_inode_ref (physical);
persistent_ino = generate_persistent_ino (&physical->backing_devino,
domain->doc_id,
domain->app_id);
}
G_LOCK (all_inodes);
if (physical)
try_ino = persistent_ino;
else
try_ino = next_virtual_inode++;
if (g_hash_table_contains (all_inodes, &try_ino))
try_ino++;
inode->ino = try_ino;
g_hash_table_insert (all_inodes, &inode->ino, inode);
G_UNLOCK (all_inodes);
return inode;
}
static ino_t
xdp_inode_to_ino (XdpInode *inode)
{
return inode->ino;
}
/* This is called on kernel upcalls, so it *should* be guaranteed that the inode exists due to the kernel refs */
static XdpInode *
xdp_inode_from_ino (ino_t ino)
{
XdpInode *inode;
G_LOCK (all_inodes);
inode = g_hash_table_lookup (all_inodes, &ino);
G_UNLOCK (all_inodes);
g_assert (inode != NULL);
/* Its safe to ref it here because we know it exists outside the lock due to the kernel refs */
return xdp_inode_ref (inode);
}
static XdpInode *
xdp_inode_ref (XdpInode *inode)
{
g_atomic_int_inc (&inode->ref_count);
return inode;
}
static void
xdp_inode_unref (XdpInode *inode)
{
gint old_ref;
XdpDomain *domain;
/* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
retry_atomic_decrement1:
old_ref = g_atomic_int_get (&inode->ref_count);
if (old_ref > 1)
{
if (!g_atomic_int_compare_and_exchange ((int *) &inode->ref_count, old_ref, old_ref - 1))
goto retry_atomic_decrement1;
}
else
{
if (old_ref <= 0)
{
g_warning ("Can't unref dead inode");
return;
}
/* Might be revived from domain->inodes hash by this time, so protect by lock */
G_LOCK (domain_inodes);
if (!g_atomic_int_compare_and_exchange ((int *) &inode->ref_count, old_ref, old_ref - 1))
{
G_UNLOCK (domain_inodes);
goto retry_atomic_decrement1;
}
domain = inode->domain;
if (domain->type == XDP_DOMAIN_APP)
g_hash_table_remove (domain->parent->inodes, domain->app_id);
else if (domain->type == XDP_DOMAIN_DOCUMENT)
{
if (inode->physical)
{
g_hash_table_remove (domain->inodes, inode->physical);
}
else
g_hash_table_remove (domain->parent->inodes, domain->doc_id);
}
/* Run this under domain_inodes lock to avoid race condition in ensure_docdir_inode + xdp_inode_new
* where we don't want a domain->inode lookup to fail, but then an all_inode lookup to succeed
* when looking for an ino collision
*
* Note: After the domain->inodes removal and here we don't allow resurrection, but we may
* still race with an all_inodes lookup (e.g. in xdp_fuse_lookup_id_for_inode), which *is*
* allowed and it can read the inode fields (while the lock is held) as they are still valid.
**/
G_LOCK (all_inodes);
g_hash_table_remove (all_inodes, &inode->ino);
G_UNLOCK (all_inodes);
G_UNLOCK (domain_inodes);
/* By now we have no refs outstanding and no way to get at the inode, so free it */
g_clear_pointer (&inode->domain_root_inode, xdp_inode_unref);
g_clear_pointer (&inode->physical, xdp_physical_inode_unref);
xdp_domain_unref (inode->domain);
g_free (inode);
}
}
static XdpInode *
xdp_inode_kernel_ref (XdpInode *inode)
{
int old;
old = g_atomic_int_add (&inode->kernel_ref_count, 1);
if (old == 0)
xdp_inode_ref (inode);
return inode;
}
static void
xdp_inode_kernel_unref (XdpInode *inode, unsigned long count)
{
gint old_ref, new_ref;
retry_atomic_decrement1:
old_ref = g_atomic_int_get (&inode->kernel_ref_count);
if (old_ref < count)
{
g_warning ("Can't kernel_unref inode with no kernel refs");
return;
}
new_ref = old_ref - count;
if (!g_atomic_int_compare_and_exchange (&inode->kernel_ref_count, old_ref, new_ref))
goto retry_atomic_decrement1;
if (new_ref == 0)
xdp_inode_unref (inode);
}
static int
verify_doc_dir_devino (int dirfd, XdpDomain *doc_domain)
{
struct stat buf;
if (fstat (dirfd, &buf) != 0)
return -errno;
if (buf.st_ino != doc_domain->doc_dir_inode ||
buf.st_dev != doc_domain->doc_dir_device)
return -ENOENT;
return 0;
}
/* Only for toplevel dirs, not this is a bit weird for toplevel dir
inodes as it returns the dir itself which isn't really the dirfd
for that (nonphysical) inode */
static int
xdp_nonphysical_document_inode_opendir (XdpInode *inode)
{
XdpDomain *domain = inode->domain;
xdp_autofd int dirfd = -1;
int res;
g_assert (domain->type == XDP_DOMAIN_DOCUMENT);
g_assert (inode->physical == NULL);
dirfd = open (domain->doc_path, O_PATH | O_DIRECTORY);
if (dirfd < 0)
return -errno;
res = verify_doc_dir_devino (dirfd, domain);
if (res != 0)
return res;
return xdp_steal_fd (&dirfd);
}
static int
xdp_document_inode_ensure_dirfd (XdpInode *inode,
int *close_fd_out)
{
int close_fd;
g_assert (inode->domain->type == XDP_DOMAIN_DOCUMENT);
*close_fd_out = -1;
if (inode->physical)
return inode->physical->fd;
else
{
if (xdp_document_domain_is_dir (inode->domain))
{
/* There is no dirfd for the toplevel dirs, happens for example
if renaming into toplevel, so just return EPERM */
return -EPERM;
}
close_fd = xdp_nonphysical_document_inode_opendir (inode);
if (close_fd < 0)
return close_fd;
*close_fd_out = close_fd;
return close_fd;
}
}
static gboolean
open_flags_has_write (int open_flags)
{
return
(open_flags & O_ACCMODE) == O_WRONLY ||
(open_flags & O_ACCMODE) == O_RDWR ||
open_flags & O_TRUNC;
}
static void
gen_temp_name (gchar *tmpl)
{
g_return_if_fail (tmpl != NULL);
const size_t len = strlen (tmpl);
g_return_if_fail (len >= 6);
static const char letters[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
static const int NLETTERS = sizeof (letters) - 1;
char *XXXXXX = tmpl + (len - 6);
for (int i = 0; i < 6; i++)
XXXXXX[i] = letters[g_random_int_range(0, NLETTERS)];
}
static int