hc
2024-05-10 9999e48639b3cecb08ffb37358bcba3b48161b29
kernel/fs/incfs/vfs.c
....@@ -4,32 +4,28 @@
44 */
55
66 #include <linux/blkdev.h>
7
-#include <linux/cred.h>
8
-#include <linux/eventpoll.h>
7
+#include <linux/compat.h>
8
+#include <linux/delay.h>
99 #include <linux/file.h>
1010 #include <linux/fs.h>
1111 #include <linux/fs_stack.h>
12
+#include <linux/fsnotify.h>
13
+#include <linux/fsverity.h>
14
+#include <linux/mmap_lock.h>
1215 #include <linux/namei.h>
1316 #include <linux/parser.h>
14
-#include <linux/poll.h>
1517 #include <linux/seq_file.h>
16
-#include <linux/syscalls.h>
17
-#include <linux/xattr.h>
1818
1919 #include <uapi/linux/incrementalfs.h>
2020
2121 #include "vfs.h"
22
+
2223 #include "data_mgmt.h"
2324 #include "format.h"
24
-#include "integrity.h"
2525 #include "internal.h"
26
-
27
-#define INCFS_PENDING_READS_INODE 2
28
-#define INCFS_LOG_INODE 3
29
-#define INCFS_START_INO_RANGE 10
30
-#define READ_FILE_MODE 0444
31
-#define READ_EXEC_FILE_MODE 0555
32
-#define READ_WRITE_FILE_MODE 0666
26
+#include "pseudo_files.h"
27
+#include "sysfs.h"
28
+#include "verity.h"
3329
3430 static int incfs_remount_fs(struct super_block *sb, int *flags, char *data);
3531
....@@ -52,23 +48,19 @@
5248 static int read_single_page(struct file *f, struct page *page);
5349 static long dispatch_ioctl(struct file *f, unsigned int req, unsigned long arg);
5450
55
-static ssize_t pending_reads_read(struct file *f, char __user *buf, size_t len,
56
- loff_t *ppos);
57
-static __poll_t pending_reads_poll(struct file *file, poll_table *wait);
58
-static int pending_reads_open(struct inode *inode, struct file *file);
59
-static int pending_reads_release(struct inode *, struct file *);
60
-
61
-static ssize_t log_read(struct file *f, char __user *buf, size_t len,
62
- loff_t *ppos);
63
-static __poll_t log_poll(struct file *file, poll_table *wait);
64
-static int log_open(struct inode *inode, struct file *file);
65
-static int log_release(struct inode *, struct file *);
51
+#ifdef CONFIG_COMPAT
52
+static long incfs_compat_ioctl(struct file *file, unsigned int cmd,
53
+ unsigned long arg);
54
+#endif
6655
6756 static struct inode *alloc_inode(struct super_block *sb);
6857 static void free_inode(struct inode *inode);
6958 static void evict_inode(struct inode *inode);
7059
7160 static int incfs_setattr(struct dentry *dentry, struct iattr *ia);
61
+static int incfs_getattr(const struct path *path,
62
+ struct kstat *stat, u32 request_mask,
63
+ unsigned int query_flags);
7264 static ssize_t incfs_getxattr(struct dentry *d, const char *name,
7365 void *value, size_t size);
7466 static ssize_t incfs_setxattr(struct dentry *d, const char *name,
....@@ -109,8 +101,6 @@
109101 .iterate = iterate_incfs_dir,
110102 .open = file_open,
111103 .release = file_release,
112
- .unlocked_ioctl = dispatch_ioctl,
113
- .compat_ioctl = dispatch_ioctl
114104 };
115105
116106 static const struct dentry_operations incfs_dentry_ops = {
....@@ -123,51 +113,54 @@
123113 /* .readpages = readpages */
124114 };
125115
126
-static const struct file_operations incfs_file_ops = {
116
+static vm_fault_t incfs_fault(struct vm_fault *vmf)
117
+{
118
+ vmf->flags &= ~FAULT_FLAG_ALLOW_RETRY;
119
+ return filemap_fault(vmf);
120
+}
121
+
122
+static const struct vm_operations_struct incfs_file_vm_ops = {
123
+ .fault = incfs_fault,
124
+ .map_pages = filemap_map_pages,
125
+ .page_mkwrite = filemap_page_mkwrite,
126
+};
127
+
128
+/* This is used for a general mmap of a disk file */
129
+
130
+static int incfs_file_mmap(struct file *file, struct vm_area_struct *vma)
131
+{
132
+ struct address_space *mapping = file->f_mapping;
133
+
134
+ if (!mapping->a_ops->readpage)
135
+ return -ENOEXEC;
136
+ file_accessed(file);
137
+ vma->vm_ops = &incfs_file_vm_ops;
138
+ return 0;
139
+}
140
+
141
+const struct file_operations incfs_file_ops = {
127142 .open = file_open,
128143 .release = file_release,
129144 .read_iter = generic_file_read_iter,
130
- .mmap = generic_file_mmap,
145
+ .mmap = incfs_file_mmap,
131146 .splice_read = generic_file_splice_read,
132147 .llseek = generic_file_llseek,
133148 .unlocked_ioctl = dispatch_ioctl,
134
- .compat_ioctl = dispatch_ioctl
149
+#ifdef CONFIG_COMPAT
150
+ .compat_ioctl = incfs_compat_ioctl,
151
+#endif
135152 };
136153
137
-enum FILL_PERMISSION {
138
- CANT_FILL = 0,
139
- CAN_FILL = 1,
140
-};
141
-
142
-static const struct file_operations incfs_pending_read_file_ops = {
143
- .read = pending_reads_read,
144
- .poll = pending_reads_poll,
145
- .open = pending_reads_open,
146
- .release = pending_reads_release,
147
- .llseek = noop_llseek,
148
- .unlocked_ioctl = dispatch_ioctl,
149
- .compat_ioctl = dispatch_ioctl
150
-};
151
-
152
-static const struct file_operations incfs_log_file_ops = {
153
- .read = log_read,
154
- .poll = log_poll,
155
- .open = log_open,
156
- .release = log_release,
157
- .llseek = noop_llseek,
158
- .unlocked_ioctl = dispatch_ioctl,
159
- .compat_ioctl = dispatch_ioctl
160
-};
161
-
162
-static const struct inode_operations incfs_file_inode_ops = {
154
+const struct inode_operations incfs_file_inode_ops = {
163155 .setattr = incfs_setattr,
164
- .getattr = simple_getattr,
156
+ .getattr = incfs_getattr,
165157 .listxattr = incfs_listxattr
166158 };
167159
168160 static int incfs_handler_getxattr(const struct xattr_handler *xh,
169161 struct dentry *d, struct inode *inode,
170
- const char *name, void *buffer, size_t size)
162
+ const char *name, void *buffer, size_t size,
163
+ int flags)
171164 {
172165 return incfs_getxattr(d, name, buffer, size);
173166 }
....@@ -191,56 +184,41 @@
191184 NULL,
192185 };
193186
194
-/* State of an open .pending_reads file, unique for each file descriptor. */
195
-struct pending_reads_state {
196
- /* A serial number of the last pending read obtained from this file. */
197
- int last_pending_read_sn;
198
-};
199
-
200
-/* State of an open .log file, unique for each file descriptor. */
201
-struct log_file_state {
202
- struct read_log_state state;
203
-};
204
-
205187 struct inode_search {
206188 unsigned long ino;
207189
208190 struct dentry *backing_dentry;
209191
210192 size_t size;
193
+
194
+ bool verity;
211195 };
212196
213197 enum parse_parameter {
214198 Opt_read_timeout,
215199 Opt_readahead_pages,
216
- Opt_no_backing_file_cache,
217
- Opt_no_backing_file_readahead,
218200 Opt_rlog_pages,
219201 Opt_rlog_wakeup_cnt,
202
+ Opt_report_uid,
203
+ Opt_sysfs_name,
220204 Opt_err
221
-};
222
-
223
-static const char pending_reads_file_name[] = INCFS_PENDING_READS_FILENAME;
224
-static struct mem_range pending_reads_file_name_range = {
225
- .data = (u8 *)pending_reads_file_name,
226
- .len = ARRAY_SIZE(pending_reads_file_name) - 1
227
-};
228
-
229
-static const char log_file_name[] = INCFS_LOG_FILENAME;
230
-static struct mem_range log_file_name_range = {
231
- .data = (u8 *)log_file_name,
232
- .len = ARRAY_SIZE(log_file_name) - 1
233205 };
234206
235207 static const match_table_t option_tokens = {
236208 { Opt_read_timeout, "read_timeout_ms=%u" },
237209 { Opt_readahead_pages, "readahead=%u" },
238
- { Opt_no_backing_file_cache, "no_bf_cache=%u" },
239
- { Opt_no_backing_file_readahead, "no_bf_readahead=%u" },
240210 { Opt_rlog_pages, "rlog_pages=%u" },
241211 { Opt_rlog_wakeup_cnt, "rlog_wakeup_cnt=%u" },
212
+ { Opt_report_uid, "report_uid" },
213
+ { Opt_sysfs_name, "sysfs_name=%s" },
242214 { Opt_err, NULL }
243215 };
216
+
217
+static void free_options(struct mount_options *opts)
218
+{
219
+ kfree(opts->sysfs_name);
220
+ opts->sysfs_name = NULL;
221
+}
244222
245223 static int parse_options(struct mount_options *opts, char *str)
246224 {
....@@ -251,12 +229,13 @@
251229 if (opts == NULL)
252230 return -EFAULT;
253231
254
- opts->read_timeout_ms = 1000; /* Default: 1s */
255
- opts->readahead_pages = 10;
256
- opts->read_log_pages = 2;
257
- opts->read_log_wakeup_count = 10;
258
- opts->no_backing_file_cache = false;
259
- opts->no_backing_file_readahead = false;
232
+ *opts = (struct mount_options) {
233
+ .read_timeout_ms = 1000, /* Default: 1s */
234
+ .readahead_pages = 10,
235
+ .read_log_pages = 2,
236
+ .read_log_wakeup_count = 10,
237
+ };
238
+
260239 if (str == NULL || *str == 0)
261240 return 0;
262241
....@@ -272,22 +251,14 @@
272251 case Opt_read_timeout:
273252 if (match_int(&args[0], &value))
274253 return -EINVAL;
254
+ if (value > 3600000)
255
+ return -EINVAL;
275256 opts->read_timeout_ms = value;
276257 break;
277258 case Opt_readahead_pages:
278259 if (match_int(&args[0], &value))
279260 return -EINVAL;
280261 opts->readahead_pages = value;
281
- break;
282
- case Opt_no_backing_file_cache:
283
- if (match_int(&args[0], &value))
284
- return -EINVAL;
285
- opts->no_backing_file_cache = (value != 0);
286
- break;
287
- case Opt_no_backing_file_readahead:
288
- if (match_int(&args[0], &value))
289
- return -EINVAL;
290
- opts->no_backing_file_readahead = (value != 0);
291262 break;
292263 case Opt_rlog_pages:
293264 if (match_int(&args[0], &value))
....@@ -299,26 +270,19 @@
299270 return -EINVAL;
300271 opts->read_log_wakeup_count = value;
301272 break;
273
+ case Opt_report_uid:
274
+ opts->report_uid = true;
275
+ break;
276
+ case Opt_sysfs_name:
277
+ opts->sysfs_name = match_strdup(&args[0]);
278
+ break;
302279 default:
280
+ free_options(opts);
303281 return -EINVAL;
304282 }
305283 }
306284
307285 return 0;
308
-}
309
-
310
-static struct super_block *file_superblock(struct file *f)
311
-{
312
- struct inode *inode = file_inode(f);
313
-
314
- return inode->i_sb;
315
-}
316
-
317
-static struct mount_info *get_mount_info(struct super_block *sb)
318
-{
319
- struct mount_info *result = sb->s_fs_info;
320
-
321
- return result;
322286 }
323287
324288 /* Read file size from the attribute. Quicker than reading the header */
....@@ -336,100 +300,66 @@
336300 return le64_to_cpu(attr_value);
337301 }
338302
303
+/* Read verity flag from the attribute. Quicker than reading the header */
304
+static bool read_verity_attr(struct dentry *backing_dentry)
305
+{
306
+ return vfs_getxattr(backing_dentry, INCFS_XATTR_VERITY_NAME, NULL, 0)
307
+ >= 0;
308
+}
309
+
339310 static int inode_test(struct inode *inode, void *opaque)
340311 {
341312 struct inode_search *search = opaque;
342313 struct inode_info *node = get_incfs_node(inode);
314
+ struct inode *backing_inode = d_inode(search->backing_dentry);
343315
344316 if (!node)
345317 return 0;
346318
347
- if (search->backing_dentry) {
348
- struct inode *backing_inode = d_inode(search->backing_dentry);
349
-
350
- return (node->n_backing_inode == backing_inode) &&
351
- inode->i_ino == search->ino;
352
- } else
353
- return inode->i_ino == search->ino;
319
+ return node->n_backing_inode == backing_inode &&
320
+ inode->i_ino == search->ino;
354321 }
355322
356323 static int inode_set(struct inode *inode, void *opaque)
357324 {
358325 struct inode_search *search = opaque;
359326 struct inode_info *node = get_incfs_node(inode);
327
+ struct dentry *backing_dentry = search->backing_dentry;
328
+ struct inode *backing_inode = d_inode(backing_dentry);
360329
361
- if (search->backing_dentry) {
362
- /* It's a regular inode that has corresponding backing inode */
363
- struct dentry *backing_dentry = search->backing_dentry;
364
- struct inode *backing_inode = d_inode(backing_dentry);
330
+ fsstack_copy_attr_all(inode, backing_inode);
331
+ if (S_ISREG(inode->i_mode)) {
332
+ u64 size = search->size;
365333
366
- fsstack_copy_attr_all(inode, backing_inode);
367
- if (S_ISREG(inode->i_mode)) {
368
- u64 size = search->size;
369
-
370
- inode->i_size = size;
371
- inode->i_blocks = get_blocks_count_for_size(size);
372
- inode->i_mapping->a_ops = &incfs_address_space_ops;
373
- inode->i_op = &incfs_file_inode_ops;
374
- inode->i_fop = &incfs_file_ops;
375
- inode->i_mode &= ~0222;
376
- } else if (S_ISDIR(inode->i_mode)) {
377
- inode->i_size = 0;
378
- inode->i_blocks = 1;
379
- inode->i_mapping->a_ops = &incfs_address_space_ops;
380
- inode->i_op = &incfs_dir_inode_ops;
381
- inode->i_fop = &incfs_dir_fops;
382
- } else {
383
- pr_warn_once("incfs: Unexpected inode type\n");
384
- return -EBADF;
385
- }
386
-
387
- ihold(backing_inode);
388
- node->n_backing_inode = backing_inode;
389
- node->n_mount_info = get_mount_info(inode->i_sb);
390
- inode->i_ctime = backing_inode->i_ctime;
391
- inode->i_mtime = backing_inode->i_mtime;
392
- inode->i_atime = backing_inode->i_atime;
393
- inode->i_ino = backing_inode->i_ino;
394
- if (backing_inode->i_ino < INCFS_START_INO_RANGE) {
395
- pr_warn("incfs: ino conflict with backing FS %ld\n",
396
- backing_inode->i_ino);
397
- }
398
-
399
- return 0;
400
- } else if (search->ino == INCFS_PENDING_READS_INODE) {
401
- /* It's an inode for .pending_reads pseudo file. */
402
-
403
- inode->i_ctime = (struct timespec64){};
404
- inode->i_mtime = inode->i_ctime;
405
- inode->i_atime = inode->i_ctime;
406
- inode->i_size = 0;
407
- inode->i_ino = INCFS_PENDING_READS_INODE;
408
- inode->i_private = NULL;
409
-
410
- inode_init_owner(inode, NULL, S_IFREG | READ_WRITE_FILE_MODE);
411
-
334
+ inode->i_size = size;
335
+ inode->i_blocks = get_blocks_count_for_size(size);
336
+ inode->i_mapping->a_ops = &incfs_address_space_ops;
412337 inode->i_op = &incfs_file_inode_ops;
413
- inode->i_fop = &incfs_pending_read_file_ops;
414
-
415
- } else if (search->ino == INCFS_LOG_INODE) {
416
- /* It's an inode for .log pseudo file. */
417
-
418
- inode->i_ctime = (struct timespec64){};
419
- inode->i_mtime = inode->i_ctime;
420
- inode->i_atime = inode->i_ctime;
338
+ inode->i_fop = &incfs_file_ops;
339
+ inode->i_mode &= ~0222;
340
+ if (search->verity)
341
+ inode_set_flags(inode, S_VERITY, S_VERITY);
342
+ } else if (S_ISDIR(inode->i_mode)) {
421343 inode->i_size = 0;
422
- inode->i_ino = INCFS_LOG_INODE;
423
- inode->i_private = NULL;
424
-
425
- inode_init_owner(inode, NULL, S_IFREG | READ_WRITE_FILE_MODE);
426
-
427
- inode->i_op = &incfs_file_inode_ops;
428
- inode->i_fop = &incfs_log_file_ops;
429
-
344
+ inode->i_blocks = 1;
345
+ inode->i_mapping->a_ops = &incfs_address_space_ops;
346
+ inode->i_op = &incfs_dir_inode_ops;
347
+ inode->i_fop = &incfs_dir_fops;
430348 } else {
431
- /* Unknown inode requested. */
432
- return -EINVAL;
349
+ pr_warn_once("incfs: Unexpected inode type\n");
350
+ return -EBADF;
351
+ }
352
+
353
+ ihold(backing_inode);
354
+ node->n_backing_inode = backing_inode;
355
+ node->n_mount_info = get_mount_info(inode->i_sb);
356
+ inode->i_ctime = backing_inode->i_ctime;
357
+ inode->i_mtime = backing_inode->i_mtime;
358
+ inode->i_atime = backing_inode->i_atime;
359
+ inode->i_ino = backing_inode->i_ino;
360
+ if (backing_inode->i_ino < INCFS_START_INO_RANGE) {
361
+ pr_warn("incfs: ino conflict with backing FS %ld\n",
362
+ backing_inode->i_ino);
433363 }
434364
435365 return 0;
....@@ -443,222 +373,7 @@
443373 .ino = backing_inode->i_ino,
444374 .backing_dentry = backing_dentry,
445375 .size = read_size_attr(backing_dentry),
446
- };
447
- struct inode *inode = iget5_locked(sb, search.ino, inode_test,
448
- inode_set, &search);
449
-
450
- if (!inode)
451
- return ERR_PTR(-ENOMEM);
452
-
453
- if (inode->i_state & I_NEW)
454
- unlock_new_inode(inode);
455
-
456
- return inode;
457
-}
458
-
459
-static ssize_t pending_reads_read(struct file *f, char __user *buf, size_t len,
460
- loff_t *ppos)
461
-{
462
- struct pending_reads_state *pr_state = f->private_data;
463
- struct mount_info *mi = get_mount_info(file_superblock(f));
464
- struct incfs_pending_read_info *reads_buf = NULL;
465
- size_t reads_to_collect = len / sizeof(*reads_buf);
466
- int last_known_read_sn = READ_ONCE(pr_state->last_pending_read_sn);
467
- int new_max_sn = last_known_read_sn;
468
- int reads_collected = 0;
469
- ssize_t result = 0;
470
- int i = 0;
471
-
472
- if (!incfs_fresh_pending_reads_exist(mi, last_known_read_sn))
473
- return 0;
474
-
475
- reads_buf = (struct incfs_pending_read_info *)get_zeroed_page(GFP_NOFS);
476
- if (!reads_buf)
477
- return -ENOMEM;
478
-
479
- reads_to_collect =
480
- min_t(size_t, PAGE_SIZE / sizeof(*reads_buf), reads_to_collect);
481
-
482
- reads_collected = incfs_collect_pending_reads(
483
- mi, last_known_read_sn, reads_buf, reads_to_collect);
484
- if (reads_collected < 0) {
485
- result = reads_collected;
486
- goto out;
487
- }
488
-
489
- for (i = 0; i < reads_collected; i++)
490
- if (reads_buf[i].serial_number > new_max_sn)
491
- new_max_sn = reads_buf[i].serial_number;
492
-
493
- /*
494
- * Just to make sure that we don't accidentally copy more data
495
- * to reads buffer than userspace can handle.
496
- */
497
- reads_collected = min_t(size_t, reads_collected, reads_to_collect);
498
- result = reads_collected * sizeof(*reads_buf);
499
-
500
- /* Copy reads info to the userspace buffer */
501
- if (copy_to_user(buf, reads_buf, result)) {
502
- result = -EFAULT;
503
- goto out;
504
- }
505
-
506
- WRITE_ONCE(pr_state->last_pending_read_sn, new_max_sn);
507
- *ppos = 0;
508
-out:
509
- if (reads_buf)
510
- free_page((unsigned long)reads_buf);
511
- return result;
512
-}
513
-
514
-
515
-static __poll_t pending_reads_poll(struct file *file, poll_table *wait)
516
-{
517
- struct pending_reads_state *state = file->private_data;
518
- struct mount_info *mi = get_mount_info(file_superblock(file));
519
- __poll_t ret = 0;
520
-
521
- poll_wait(file, &mi->mi_pending_reads_notif_wq, wait);
522
- if (incfs_fresh_pending_reads_exist(mi,
523
- state->last_pending_read_sn))
524
- ret = EPOLLIN | EPOLLRDNORM;
525
-
526
- return ret;
527
-}
528
-
529
-static int pending_reads_open(struct inode *inode, struct file *file)
530
-{
531
- struct pending_reads_state *state = NULL;
532
-
533
- state = kzalloc(sizeof(*state), GFP_NOFS);
534
- if (!state)
535
- return -ENOMEM;
536
-
537
- file->private_data = state;
538
- return 0;
539
-}
540
-
541
-static int pending_reads_release(struct inode *inode, struct file *file)
542
-{
543
- kfree(file->private_data);
544
- return 0;
545
-}
546
-
547
-static struct inode *fetch_pending_reads_inode(struct super_block *sb)
548
-{
549
- struct inode_search search = {
550
- .ino = INCFS_PENDING_READS_INODE
551
- };
552
- struct inode *inode = iget5_locked(sb, search.ino, inode_test,
553
- inode_set, &search);
554
-
555
- if (!inode)
556
- return ERR_PTR(-ENOMEM);
557
-
558
- if (inode->i_state & I_NEW)
559
- unlock_new_inode(inode);
560
-
561
- return inode;
562
-}
563
-
564
-static int log_open(struct inode *inode, struct file *file)
565
-{
566
- struct log_file_state *log_state = NULL;
567
- struct mount_info *mi = get_mount_info(file_superblock(file));
568
-
569
- log_state = kzalloc(sizeof(*log_state), GFP_NOFS);
570
- if (!log_state)
571
- return -ENOMEM;
572
-
573
- log_state->state = incfs_get_log_state(mi);
574
- file->private_data = log_state;
575
- return 0;
576
-}
577
-
578
-static int log_release(struct inode *inode, struct file *file)
579
-{
580
- kfree(file->private_data);
581
- return 0;
582
-}
583
-
584
-static ssize_t log_read(struct file *f, char __user *buf, size_t len,
585
- loff_t *ppos)
586
-{
587
- struct log_file_state *log_state = f->private_data;
588
- struct mount_info *mi = get_mount_info(file_superblock(f));
589
- int total_reads_collected = 0;
590
- int rl_size;
591
- ssize_t result = 0;
592
- struct incfs_pending_read_info *reads_buf;
593
- ssize_t reads_to_collect = len / sizeof(*reads_buf);
594
- ssize_t reads_per_page = PAGE_SIZE / sizeof(*reads_buf);
595
-
596
- rl_size = READ_ONCE(mi->mi_log.rl_size);
597
- if (rl_size == 0)
598
- return 0;
599
-
600
- reads_buf = (struct incfs_pending_read_info *)__get_free_page(GFP_NOFS);
601
- if (!reads_buf)
602
- return -ENOMEM;
603
-
604
- reads_to_collect = min_t(ssize_t, rl_size, reads_to_collect);
605
- while (reads_to_collect > 0) {
606
- struct read_log_state next_state;
607
- int reads_collected;
608
-
609
- memcpy(&next_state, &log_state->state, sizeof(next_state));
610
- reads_collected = incfs_collect_logged_reads(
611
- mi, &next_state, reads_buf,
612
- min_t(ssize_t, reads_to_collect, reads_per_page));
613
- if (reads_collected <= 0) {
614
- result = total_reads_collected ?
615
- total_reads_collected *
616
- sizeof(*reads_buf) :
617
- reads_collected;
618
- goto out;
619
- }
620
- if (copy_to_user(buf, reads_buf,
621
- reads_collected * sizeof(*reads_buf))) {
622
- result = total_reads_collected ?
623
- total_reads_collected *
624
- sizeof(*reads_buf) :
625
- -EFAULT;
626
- goto out;
627
- }
628
-
629
- memcpy(&log_state->state, &next_state, sizeof(next_state));
630
- total_reads_collected += reads_collected;
631
- buf += reads_collected * sizeof(*reads_buf);
632
- reads_to_collect -= reads_collected;
633
- }
634
-
635
- result = total_reads_collected * sizeof(*reads_buf);
636
- *ppos = 0;
637
-out:
638
- if (reads_buf)
639
- free_page((unsigned long)reads_buf);
640
- return result;
641
-}
642
-
643
-static __poll_t log_poll(struct file *file, poll_table *wait)
644
-{
645
- struct log_file_state *log_state = file->private_data;
646
- struct mount_info *mi = get_mount_info(file_superblock(file));
647
- int count;
648
- __poll_t ret = 0;
649
-
650
- poll_wait(file, &mi->mi_log.ml_notif_wq, wait);
651
- count = incfs_get_uncollected_logs_count(mi, &log_state->state);
652
- if (count >= mi->mi_options.read_log_wakeup_count)
653
- ret = EPOLLIN | EPOLLRDNORM;
654
-
655
- return ret;
656
-}
657
-
658
-static struct inode *fetch_log_inode(struct super_block *sb)
659
-{
660
- struct inode_search search = {
661
- .ino = INCFS_LOG_INODE
376
+ .verity = read_verity_attr(backing_dentry),
662377 };
663378 struct inode *inode = iget5_locked(sb, search.ino, inode_test,
664379 inode_set, &search);
....@@ -679,7 +394,7 @@
679394 struct mount_info *mi = get_mount_info(file_superblock(file));
680395 bool root;
681396
682
- if (!dir || !mi) {
397
+ if (!dir) {
683398 error = -EBADF;
684399 goto out;
685400 }
....@@ -687,29 +402,15 @@
687402 root = dir->backing_dir->f_inode
688403 == d_inode(mi->mi_backing_dir_path.dentry);
689404
690
- if (root && ctx->pos == 0) {
691
- if (!dir_emit(ctx, pending_reads_file_name,
692
- ARRAY_SIZE(pending_reads_file_name) - 1,
693
- INCFS_PENDING_READS_INODE, DT_REG)) {
694
- error = -EINVAL;
405
+ if (root) {
406
+ error = emit_pseudo_files(ctx);
407
+ if (error)
695408 goto out;
696
- }
697
- ctx->pos++;
698409 }
699410
700
- if (root && ctx->pos == 1) {
701
- if (!dir_emit(ctx, log_file_name,
702
- ARRAY_SIZE(log_file_name) - 1,
703
- INCFS_LOG_INODE, DT_REG)) {
704
- error = -EINVAL;
705
- goto out;
706
- }
707
- ctx->pos++;
708
- }
709
-
710
- ctx->pos -= 2;
411
+ ctx->pos -= PSEUDO_FILE_COUNT;
711412 error = iterate_dir(dir->backing_dir, ctx);
712
- ctx->pos += 2;
413
+ ctx->pos += PSEUDO_FILE_COUNT;
713414 file->f_pos = dir->backing_dir->f_pos;
714415 out:
715416 if (error)
....@@ -736,29 +437,10 @@
736437 return 0;
737438 }
738439
739
-static struct dentry *incfs_lookup_dentry(struct dentry *parent,
740
- const char *name)
440
+static struct dentry *open_or_create_special_dir(struct dentry *backing_dir,
441
+ const char *name,
442
+ bool *created)
741443 {
742
- struct inode *inode;
743
- struct dentry *result = NULL;
744
-
745
- if (!parent)
746
- return ERR_PTR(-EFAULT);
747
-
748
- inode = d_inode(parent);
749
- inode_lock_nested(inode, I_MUTEX_PARENT);
750
- result = lookup_one_len(name, parent, strlen(name));
751
- inode_unlock(inode);
752
-
753
- if (IS_ERR(result))
754
- pr_warn("%s err:%ld\n", __func__, PTR_ERR(result));
755
-
756
- return result;
757
-}
758
-
759
-static struct dentry *open_or_create_index_dir(struct dentry *backing_dir)
760
-{
761
- static const char name[] = ".index";
762444 struct dentry *index_dentry;
763445 struct inode *backing_inode = d_inode(backing_dir);
764446 int err = 0;
....@@ -770,6 +452,7 @@
770452 return index_dentry;
771453 } else if (d_really_is_positive(index_dentry)) {
772454 /* Index already exists. */
455
+ *created = false;
773456 return index_dentry;
774457 }
775458
....@@ -783,12 +466,68 @@
783466 return ERR_PTR(err);
784467 }
785468
786
- if (!d_really_is_positive(index_dentry)) {
469
+ if (!d_really_is_positive(index_dentry) ||
470
+ unlikely(d_unhashed(index_dentry))) {
787471 dput(index_dentry);
788472 return ERR_PTR(-EINVAL);
789473 }
790474
475
+ *created = true;
791476 return index_dentry;
477
+}
478
+
479
+static int read_single_page_timeouts(struct data_file *df, struct file *f,
480
+ int block_index, struct mem_range range,
481
+ struct mem_range tmp,
482
+ unsigned int *delayed_min_us)
483
+{
484
+ struct mount_info *mi = df->df_mount_info;
485
+ struct incfs_read_data_file_timeouts timeouts = {
486
+ .max_pending_time_us = U32_MAX,
487
+ };
488
+ int uid = current_uid().val;
489
+ int i;
490
+
491
+ spin_lock(&mi->mi_per_uid_read_timeouts_lock);
492
+ for (i = 0; i < mi->mi_per_uid_read_timeouts_size /
493
+ sizeof(*mi->mi_per_uid_read_timeouts); ++i) {
494
+ struct incfs_per_uid_read_timeouts *t =
495
+ &mi->mi_per_uid_read_timeouts[i];
496
+
497
+ if(t->uid == uid) {
498
+ timeouts.min_time_us = t->min_time_us;
499
+ timeouts.min_pending_time_us = t->min_pending_time_us;
500
+ timeouts.max_pending_time_us = t->max_pending_time_us;
501
+ break;
502
+ }
503
+ }
504
+ spin_unlock(&mi->mi_per_uid_read_timeouts_lock);
505
+ if (timeouts.max_pending_time_us == U32_MAX) {
506
+ u64 read_timeout_us = (u64)mi->mi_options.read_timeout_ms *
507
+ 1000;
508
+
509
+ timeouts.max_pending_time_us = read_timeout_us <= U32_MAX ?
510
+ read_timeout_us : U32_MAX;
511
+ }
512
+
513
+ return incfs_read_data_file_block(range, f, block_index, tmp,
514
+ &timeouts, delayed_min_us);
515
+}
516
+
517
+static int usleep_interruptible(u32 us)
518
+{
519
+ /* See:
520
+ * https://www.kernel.org/doc/Documentation/timers/timers-howto.txt
521
+ * for explanation
522
+ */
523
+ if (us < 10) {
524
+ udelay(us);
525
+ return 0;
526
+ } else if (us < 20000) {
527
+ usleep_range(us, us + us / 10);
528
+ return 0;
529
+ } else
530
+ return msleep_interruptible(us / 1000);
792531 }
793532
794533 static int read_single_page(struct file *f, struct page *page)
....@@ -799,28 +538,36 @@
799538 ssize_t read_result = 0;
800539 struct data_file *df = get_incfs_data_file(f);
801540 int result = 0;
802
- void *page_start = kmap(page);
541
+ void *page_start;
803542 int block_index;
804
- int timeout_ms;
543
+ unsigned int delayed_min_us = 0;
805544
806
- if (!df)
545
+ if (!df) {
546
+ SetPageError(page);
547
+ unlock_page(page);
807548 return -EBADF;
549
+ }
808550
551
+ page_start = kmap(page);
809552 offset = page_offset(page);
810
- block_index = offset / INCFS_DATA_FILE_BLOCK_SIZE;
553
+ block_index = (offset + df->df_mapped_offset) /
554
+ INCFS_DATA_FILE_BLOCK_SIZE;
811555 size = df->df_size;
812
- timeout_ms = df->df_mount_info->mi_options.read_timeout_ms;
813556
814557 if (offset < size) {
815558 struct mem_range tmp = {
816559 .len = 2 * INCFS_DATA_FILE_BLOCK_SIZE
817560 };
818
-
819561 tmp.data = (u8 *)__get_free_pages(GFP_NOFS, get_order(tmp.len));
562
+ if (!tmp.data) {
563
+ read_result = -ENOMEM;
564
+ goto err;
565
+ }
820566 bytes_to_read = min_t(loff_t, size - offset, PAGE_SIZE);
821
- read_result = incfs_read_data_file_block(
822
- range(page_start, bytes_to_read), f, block_index,
823
- timeout_ms, tmp);
567
+
568
+ read_result = read_single_page_timeouts(df, f, block_index,
569
+ range(page_start, bytes_to_read), tmp,
570
+ &delayed_min_us);
824571
825572 free_pages((unsigned long)tmp.data, get_order(tmp.len));
826573 } else {
....@@ -828,6 +575,7 @@
828575 read_result = 0;
829576 }
830577
578
+err:
831579 if (read_result < 0)
832580 result = read_result;
833581 else if (read_result < PAGE_SIZE)
....@@ -841,140 +589,12 @@
841589 flush_dcache_page(page);
842590 kunmap(page);
843591 unlock_page(page);
592
+ if (delayed_min_us)
593
+ usleep_interruptible(delayed_min_us);
844594 return result;
845595 }
846596
847
-static char *file_id_to_str(incfs_uuid_t id)
848
-{
849
- char *result = kmalloc(1 + sizeof(id.bytes) * 2, GFP_NOFS);
850
- char *end;
851
-
852
- if (!result)
853
- return NULL;
854
-
855
- end = bin2hex(result, id.bytes, sizeof(id.bytes));
856
- *end = 0;
857
- return result;
858
-}
859
-
860
-static struct mem_range incfs_copy_signature_info_from_user(u8 __user *original,
861
- u64 size)
862
-{
863
- u8 *result;
864
-
865
- if (!original)
866
- return range(NULL, 0);
867
-
868
- if (size > INCFS_MAX_SIGNATURE_SIZE)
869
- return range(ERR_PTR(-EFAULT), 0);
870
-
871
- result = kzalloc(size, GFP_NOFS | __GFP_COMP);
872
- if (!result)
873
- return range(ERR_PTR(-ENOMEM), 0);
874
-
875
- if (copy_from_user(result, original, size)) {
876
- kfree(result);
877
- return range(ERR_PTR(-EFAULT), 0);
878
- }
879
-
880
- return range(result, size);
881
-}
882
-
883
-static int init_new_file(struct mount_info *mi, struct dentry *dentry,
884
- incfs_uuid_t *uuid, u64 size, struct mem_range attr,
885
- u8 __user *user_signature_info, u64 signature_size)
886
-{
887
- struct path path = {};
888
- struct file *new_file;
889
- int error = 0;
890
- struct backing_file_context *bfc = NULL;
891
- u32 block_count;
892
- struct mem_range raw_signature = { NULL };
893
- struct mtree *hash_tree = NULL;
894
-
895
- if (!mi || !dentry || !uuid)
896
- return -EFAULT;
897
-
898
- /* Resize newly created file to its true size. */
899
- path = (struct path) {
900
- .mnt = mi->mi_backing_dir_path.mnt,
901
- .dentry = dentry
902
- };
903
- new_file = dentry_open(&path, O_RDWR | O_NOATIME | O_LARGEFILE,
904
- current_cred());
905
-
906
- if (IS_ERR(new_file)) {
907
- error = PTR_ERR(new_file);
908
- goto out;
909
- }
910
-
911
- bfc = incfs_alloc_bfc(mi, new_file);
912
- fput(new_file);
913
- if (IS_ERR(bfc)) {
914
- error = PTR_ERR(bfc);
915
- bfc = NULL;
916
- goto out;
917
- }
918
-
919
- mutex_lock(&bfc->bc_mutex);
920
- error = incfs_write_fh_to_backing_file(bfc, uuid, size);
921
- if (error)
922
- goto out;
923
-
924
- if (attr.data && attr.len) {
925
- error = incfs_write_file_attr_to_backing_file(bfc,
926
- attr, NULL);
927
- if (error)
928
- goto out;
929
- }
930
-
931
- block_count = (u32)get_blocks_count_for_size(size);
932
-
933
- if (user_signature_info) {
934
- raw_signature = incfs_copy_signature_info_from_user(
935
- user_signature_info, signature_size);
936
-
937
- if (IS_ERR(raw_signature.data)) {
938
- error = PTR_ERR(raw_signature.data);
939
- raw_signature.data = NULL;
940
- goto out;
941
- }
942
-
943
- hash_tree = incfs_alloc_mtree(raw_signature, block_count);
944
- if (IS_ERR(hash_tree)) {
945
- error = PTR_ERR(hash_tree);
946
- hash_tree = NULL;
947
- goto out;
948
- }
949
-
950
- error = incfs_write_signature_to_backing_file(
951
- bfc, raw_signature, hash_tree->hash_tree_area_size);
952
- if (error)
953
- goto out;
954
-
955
- block_count += get_blocks_count_for_size(
956
- hash_tree->hash_tree_area_size);
957
- }
958
-
959
- if (block_count)
960
- error = incfs_write_blockmap_to_backing_file(bfc, block_count);
961
-
962
- if (error)
963
- goto out;
964
-out:
965
- if (bfc) {
966
- mutex_unlock(&bfc->bc_mutex);
967
- incfs_free_bfc(bfc);
968
- }
969
- incfs_free_mtree(hash_tree);
970
- kfree(raw_signature.data);
971
-
972
- if (error)
973
- pr_debug("incfs: %s error: %d\n", __func__, error);
974
- return error;
975
-}
976
-
977
-static int incfs_link(struct dentry *what, struct dentry *where)
597
+int incfs_link(struct dentry *what, struct dentry *where)
978598 {
979599 struct dentry *parent_dentry = dget_parent(where);
980600 struct inode *pinode = d_inode(parent_dentry);
....@@ -988,7 +608,7 @@
988608 return error;
989609 }
990610
991
-static int incfs_unlink(struct dentry *dentry)
611
+int incfs_unlink(struct dentry *dentry)
992612 {
993613 struct dentry *parent_dentry = dget_parent(dentry);
994614 struct inode *pinode = d_inode(parent_dentry);
....@@ -1016,274 +636,108 @@
1016636 return error;
1017637 }
1018638
1019
-static int dir_relative_path_resolve(
1020
- struct mount_info *mi,
1021
- const char __user *relative_path,
1022
- struct path *result_path)
639
+static void notify_unlink(struct dentry *dentry, const char *file_id_str,
640
+ const char *special_directory)
1023641 {
1024
- struct path *base_path = &mi->mi_backing_dir_path;
1025
- int dir_fd = get_unused_fd_flags(0);
1026
- struct file *dir_f = NULL;
642
+ struct dentry *root = dentry;
643
+ struct dentry *file = NULL;
644
+ struct dentry *dir = NULL;
1027645 int error = 0;
646
+ bool take_lock = root->d_parent != root->d_parent->d_parent;
1028647
1029
- if (dir_fd < 0)
1030
- return dir_fd;
648
+ while (root != root->d_parent)
649
+ root = root->d_parent;
1031650
1032
- dir_f = dentry_open(base_path, O_RDONLY | O_NOATIME, current_cred());
651
+ if (take_lock)
652
+ dir = incfs_lookup_dentry(root, special_directory);
653
+ else
654
+ dir = lookup_one_len(special_directory, root,
655
+ strlen(special_directory));
1033656
1034
- if (IS_ERR(dir_f)) {
1035
- error = PTR_ERR(dir_f);
657
+ if (IS_ERR(dir)) {
658
+ error = PTR_ERR(dir);
1036659 goto out;
1037660 }
1038
- fd_install(dir_fd, dir_f);
1039
-
1040
- if (!relative_path) {
1041
- /* No relative path given, just return the base dir. */
1042
- *result_path = *base_path;
1043
- path_get(result_path);
661
+ if (d_is_negative(dir)) {
662
+ error = -ENOENT;
1044663 goto out;
1045664 }
1046665
1047
- error = user_path_at_empty(dir_fd, relative_path,
1048
- LOOKUP_FOLLOW | LOOKUP_DIRECTORY, result_path, NULL);
666
+ file = incfs_lookup_dentry(dir, file_id_str);
667
+ if (IS_ERR(file)) {
668
+ error = PTR_ERR(file);
669
+ goto out;
670
+ }
671
+ if (d_is_negative(file)) {
672
+ error = -ENOENT;
673
+ goto out;
674
+ }
675
+
676
+ fsnotify_unlink(d_inode(dir), file);
677
+ d_delete(file);
1049678
1050679 out:
1051
- ksys_close(dir_fd);
1052680 if (error)
1053
- pr_debug("incfs: %s %d\n", __func__, error);
1054
- return error;
681
+ pr_warn("%s failed with error %d\n", __func__, error);
682
+
683
+ dput(dir);
684
+ dput(file);
1055685 }
1056686
1057
-static int validate_name(char *file_name)
687
+static void handle_file_completed(struct file *f, struct data_file *df)
1058688 {
1059
- struct mem_range name = range(file_name, strlen(file_name));
1060
- int i = 0;
1061
-
1062
- if (name.len > INCFS_MAX_NAME_LEN)
1063
- return -ENAMETOOLONG;
1064
-
1065
- if (incfs_equal_ranges(pending_reads_file_name_range, name))
1066
- return -EINVAL;
1067
-
1068
- for (i = 0; i < name.len; i++)
1069
- if (name.data[i] == '/')
1070
- return -EINVAL;
1071
-
1072
- return 0;
1073
-}
1074
-
1075
-static int chmod(struct dentry *dentry, umode_t mode)
1076
-{
1077
- struct inode *inode = dentry->d_inode;
1078
- struct inode *delegated_inode = NULL;
1079
- struct iattr newattrs;
689
+ struct backing_file_context *bfc;
690
+ struct mount_info *mi = df->df_mount_info;
691
+ char *file_id_str = NULL;
692
+ struct dentry *incomplete_file_dentry = NULL;
693
+ const struct cred *old_cred = override_creds(mi->mi_owner);
1080694 int error;
1081695
1082
-retry_deleg:
1083
- inode_lock(inode);
1084
- newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
1085
- newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
1086
- error = notify_change(dentry, &newattrs, &delegated_inode);
1087
- inode_unlock(inode);
1088
- if (delegated_inode) {
1089
- error = break_deleg_wait(&delegated_inode);
1090
- if (!error)
1091
- goto retry_deleg;
1092
- }
1093
- return error;
1094
-}
696
+ /* Truncate file to remove any preallocated space */
697
+ bfc = df->df_backing_file_context;
698
+ if (bfc) {
699
+ struct file *f = bfc->bc_file;
1095700
1096
-static long ioctl_create_file(struct mount_info *mi,
1097
- struct incfs_new_file_args __user *usr_args)
1098
-{
1099
- struct incfs_new_file_args args;
1100
- char *file_id_str = NULL;
1101
- struct dentry *index_file_dentry = NULL;
1102
- struct dentry *named_file_dentry = NULL;
1103
- struct path parent_dir_path = {};
1104
- struct inode *index_dir_inode = NULL;
1105
- __le64 size_attr_value = 0;
1106
- char *file_name = NULL;
1107
- char *attr_value = NULL;
1108
- int error = 0;
1109
- bool locked = false;
701
+ if (f) {
702
+ loff_t size = i_size_read(file_inode(f));
1110703
1111
- if (!mi || !mi->mi_index_dir) {
1112
- error = -EFAULT;
1113
- goto out;
1114
- }
1115
-
1116
- if (copy_from_user(&args, usr_args, sizeof(args)) > 0) {
1117
- error = -EFAULT;
1118
- goto out;
1119
- }
1120
-
1121
- file_name = strndup_user(u64_to_user_ptr(args.file_name), PATH_MAX);
1122
- if (IS_ERR(file_name)) {
1123
- error = PTR_ERR(file_name);
1124
- file_name = NULL;
1125
- goto out;
1126
- }
1127
-
1128
- error = validate_name(file_name);
1129
- if (error)
1130
- goto out;
1131
-
1132
- file_id_str = file_id_to_str(args.file_id);
1133
- if (!file_id_str) {
1134
- error = -ENOMEM;
1135
- goto out;
1136
- }
1137
-
1138
- error = mutex_lock_interruptible(&mi->mi_dir_struct_mutex);
1139
- if (error)
1140
- goto out;
1141
- locked = true;
1142
-
1143
- /* Find a directory to put the file into. */
1144
- error = dir_relative_path_resolve(mi,
1145
- u64_to_user_ptr(args.directory_path),
1146
- &parent_dir_path);
1147
- if (error)
1148
- goto out;
1149
-
1150
- if (parent_dir_path.dentry == mi->mi_index_dir) {
1151
- /* Can't create a file directly inside .index */
1152
- error = -EBUSY;
1153
- goto out;
1154
- }
1155
-
1156
- /* Look up a dentry in the parent dir. It should be negative. */
1157
- named_file_dentry = incfs_lookup_dentry(parent_dir_path.dentry,
1158
- file_name);
1159
- if (!named_file_dentry) {
1160
- error = -EFAULT;
1161
- goto out;
1162
- }
1163
- if (IS_ERR(named_file_dentry)) {
1164
- error = PTR_ERR(named_file_dentry);
1165
- named_file_dentry = NULL;
1166
- goto out;
1167
- }
1168
- if (d_really_is_positive(named_file_dentry)) {
1169
- /* File with this path already exists. */
1170
- error = -EEXIST;
1171
- goto out;
1172
- }
1173
- /* Look up a dentry in the .index dir. It should be negative. */
1174
- index_file_dentry = incfs_lookup_dentry(mi->mi_index_dir, file_id_str);
1175
- if (!index_file_dentry) {
1176
- error = -EFAULT;
1177
- goto out;
1178
- }
1179
- if (IS_ERR(index_file_dentry)) {
1180
- error = PTR_ERR(index_file_dentry);
1181
- index_file_dentry = NULL;
1182
- goto out;
1183
- }
1184
- if (d_really_is_positive(index_file_dentry)) {
1185
- /* File with this ID already exists in index. */
1186
- error = -EEXIST;
1187
- goto out;
1188
- }
1189
-
1190
- /* Creating a file in the .index dir. */
1191
- index_dir_inode = d_inode(mi->mi_index_dir);
1192
- inode_lock_nested(index_dir_inode, I_MUTEX_PARENT);
1193
- error = vfs_create(index_dir_inode, index_file_dentry, args.mode | 0222,
1194
- true);
1195
- inode_unlock(index_dir_inode);
1196
-
1197
- if (error)
1198
- goto out;
1199
- if (!d_really_is_positive(index_file_dentry)) {
1200
- error = -EINVAL;
1201
- goto out;
1202
- }
1203
-
1204
- error = chmod(index_file_dentry, args.mode | 0222);
1205
- if (error) {
1206
- pr_debug("incfs: chmod err: %d\n", error);
1207
- goto delete_index_file;
1208
- }
1209
-
1210
- /* Save the file's ID as an xattr for easy fetching in future. */
1211
- error = vfs_setxattr(index_file_dentry, INCFS_XATTR_ID_NAME,
1212
- file_id_str, strlen(file_id_str), XATTR_CREATE);
1213
- if (error) {
1214
- pr_debug("incfs: vfs_setxattr err:%d\n", error);
1215
- goto delete_index_file;
1216
- }
1217
-
1218
- /* Save the file's size as an xattr for easy fetching in future. */
1219
- size_attr_value = cpu_to_le64(args.size);
1220
- error = vfs_setxattr(index_file_dentry, INCFS_XATTR_SIZE_NAME,
1221
- (char *)&size_attr_value, sizeof(size_attr_value),
1222
- XATTR_CREATE);
1223
- if (error) {
1224
- pr_debug("incfs: vfs_setxattr err:%d\n", error);
1225
- goto delete_index_file;
1226
- }
1227
-
1228
- /* Save the file's attribute as an xattr */
1229
- if (args.file_attr_len && args.file_attr) {
1230
- if (args.file_attr_len > INCFS_MAX_FILE_ATTR_SIZE) {
1231
- error = -E2BIG;
1232
- goto delete_index_file;
704
+ error = vfs_truncate(&f->f_path, size);
705
+ if (error)
706
+ /* No useful action on failure */
707
+ pr_warn("incfs: Failed to truncate complete file: %d\n",
708
+ error);
1233709 }
1234
-
1235
- attr_value = kmalloc(args.file_attr_len, GFP_NOFS);
1236
- if (!attr_value) {
1237
- error = -ENOMEM;
1238
- goto delete_index_file;
1239
- }
1240
-
1241
- if (copy_from_user(attr_value,
1242
- u64_to_user_ptr(args.file_attr),
1243
- args.file_attr_len) > 0) {
1244
- error = -EFAULT;
1245
- goto delete_index_file;
1246
- }
1247
-
1248
- error = vfs_setxattr(index_file_dentry,
1249
- INCFS_XATTR_METADATA_NAME,
1250
- attr_value, args.file_attr_len,
1251
- XATTR_CREATE);
1252
-
1253
- if (error)
1254
- goto delete_index_file;
1255710 }
1256711
1257
- /* Initializing a newly created file. */
1258
- error = init_new_file(mi, index_file_dentry, &args.file_id, args.size,
1259
- range(attr_value, args.file_attr_len),
1260
- (u8 __user *)args.signature_info,
1261
- args.signature_size);
1262
- if (error)
1263
- goto delete_index_file;
1264
-
1265
- /* Linking a file with it's real name from the requested dir. */
1266
- error = incfs_link(index_file_dentry, named_file_dentry);
1267
-
1268
- if (!error)
712
+ /* This is best effort - there is no useful action to take on failure */
713
+ file_id_str = file_id_to_str(df->df_id);
714
+ if (!file_id_str)
1269715 goto out;
1270716
1271
-delete_index_file:
1272
- incfs_unlink(index_file_dentry);
717
+ incomplete_file_dentry = incfs_lookup_dentry(
718
+ df->df_mount_info->mi_incomplete_dir,
719
+ file_id_str);
720
+ if (!incomplete_file_dentry || IS_ERR(incomplete_file_dentry)) {
721
+ incomplete_file_dentry = NULL;
722
+ goto out;
723
+ }
724
+
725
+ if (!d_really_is_positive(incomplete_file_dentry))
726
+ goto out;
727
+
728
+ vfs_fsync(df->df_backing_file_context->bc_file, 0);
729
+ error = incfs_unlink(incomplete_file_dentry);
730
+ if (error) {
731
+ pr_warn("incfs: Deleting incomplete file failed: %d\n", error);
732
+ goto out;
733
+ }
734
+
735
+ notify_unlink(f->f_path.dentry, file_id_str, INCFS_INCOMPLETE_NAME);
1273736
1274737 out:
1275
- if (error)
1276
- pr_debug("incfs: %s err:%d\n", __func__, error);
1277
-
738
+ dput(incomplete_file_dentry);
1278739 kfree(file_id_str);
1279
- kfree(file_name);
1280
- kfree(attr_value);
1281
- dput(named_file_dentry);
1282
- dput(index_file_dentry);
1283
- path_put(&parent_dir_path);
1284
- if (locked)
1285
- mutex_unlock(&mi->mi_dir_struct_mutex);
1286
- return error;
740
+ revert_creds(old_cred);
1287741 }
1288742
1289743 static long ioctl_fill_blocks(struct file *f, void __user *arg)
....@@ -1292,15 +746,17 @@
1292746 struct incfs_fill_blocks fill_blocks;
1293747 struct incfs_fill_block __user *usr_fill_block_array;
1294748 struct data_file *df = get_incfs_data_file(f);
749
+ struct incfs_file_data *fd = f->private_data;
1295750 const ssize_t data_buf_size = 2 * INCFS_DATA_FILE_BLOCK_SIZE;
1296751 u8 *data_buf = NULL;
1297752 ssize_t error = 0;
1298753 int i = 0;
754
+ bool complete = false;
1299755
1300756 if (!df)
1301757 return -EBADF;
1302758
1303
- if ((uintptr_t)f->private_data != CAN_FILL)
759
+ if (!fd || fd->fd_fill_permission != CAN_FILL)
1304760 return -EPERM;
1305761
1306762 if (copy_from_user(&fill_blocks, usr_fill_blocks, sizeof(fill_blocks)))
....@@ -1337,7 +793,7 @@
1337793 data_buf);
1338794 } else {
1339795 error = incfs_process_new_data_block(df, &fill_block,
1340
- data_buf);
796
+ data_buf, &complete);
1341797 }
1342798 if (error)
1343799 break;
....@@ -1345,6 +801,9 @@
1345801
1346802 if (data_buf)
1347803 free_pages((unsigned long)data_buf, get_order(data_buf_size));
804
+
805
+ if (complete)
806
+ handle_file_completed(f, df);
1348807
1349808 /*
1350809 * Only report the error if no records were processed, otherwise
....@@ -1354,53 +813,6 @@
1354813 return error;
1355814
1356815 return i;
1357
-}
1358
-
1359
-static long ioctl_permit_fill(struct file *f, void __user *arg)
1360
-{
1361
- struct incfs_permit_fill __user *usr_permit_fill = arg;
1362
- struct incfs_permit_fill permit_fill;
1363
- long error = 0;
1364
- struct file *file = NULL;
1365
-
1366
- if (f->f_op != &incfs_pending_read_file_ops)
1367
- return -EPERM;
1368
-
1369
- if (copy_from_user(&permit_fill, usr_permit_fill, sizeof(permit_fill)))
1370
- return -EFAULT;
1371
-
1372
- file = fget(permit_fill.file_descriptor);
1373
- if (IS_ERR(file))
1374
- return PTR_ERR(file);
1375
-
1376
- if (file->f_op != &incfs_file_ops) {
1377
- error = -EPERM;
1378
- goto out;
1379
- }
1380
-
1381
- if (file->f_inode->i_sb != f->f_inode->i_sb) {
1382
- error = -EPERM;
1383
- goto out;
1384
- }
1385
-
1386
- switch ((uintptr_t)file->private_data) {
1387
- case CANT_FILL:
1388
- file->private_data = (void *)CAN_FILL;
1389
- break;
1390
-
1391
- case CAN_FILL:
1392
- pr_debug("CAN_FILL already set");
1393
- break;
1394
-
1395
- default:
1396
- pr_warn("Invalid file private data");
1397
- error = -EFAULT;
1398
- goto out;
1399
- }
1400
-
1401
-out:
1402
- fput(file);
1403
- return error;
1404816 }
1405817
1406818 static long ioctl_read_file_signature(struct file *f, void __user *arg)
....@@ -1456,18 +868,19 @@
1456868 struct incfs_get_filled_blocks_args __user *args_usr_ptr = arg;
1457869 struct incfs_get_filled_blocks_args args = {};
1458870 struct data_file *df = get_incfs_data_file(f);
871
+ struct incfs_file_data *fd = f->private_data;
1459872 int error;
1460873
1461
- if (!df)
874
+ if (!df || !fd)
1462875 return -EINVAL;
1463876
1464
- if ((uintptr_t)f->private_data != CAN_FILL)
877
+ if (fd->fd_fill_permission != CAN_FILL)
1465878 return -EPERM;
1466879
1467880 if (copy_from_user(&args, args_usr_ptr, sizeof(args)) > 0)
1468881 return -EINVAL;
1469882
1470
- error = incfs_get_filled_blocks(df, &args);
883
+ error = incfs_get_filled_blocks(df, fd, &args);
1471884
1472885 if (copy_to_user(args_usr_ptr, &args, sizeof(args)))
1473886 return -EFAULT;
....@@ -1475,25 +888,80 @@
1475888 return error;
1476889 }
1477890
891
+static long ioctl_get_block_count(struct file *f, void __user *arg)
892
+{
893
+ struct incfs_get_block_count_args __user *args_usr_ptr = arg;
894
+ struct incfs_get_block_count_args args = {};
895
+ struct data_file *df = get_incfs_data_file(f);
896
+
897
+ if (!df)
898
+ return -EINVAL;
899
+
900
+ args.total_data_blocks_out = df->df_data_block_count;
901
+ args.filled_data_blocks_out = atomic_read(&df->df_data_blocks_written);
902
+ args.total_hash_blocks_out = df->df_total_block_count -
903
+ df->df_data_block_count;
904
+ args.filled_hash_blocks_out = atomic_read(&df->df_hash_blocks_written);
905
+
906
+ if (copy_to_user(args_usr_ptr, &args, sizeof(args)))
907
+ return -EFAULT;
908
+
909
+ return 0;
910
+}
911
+
912
+static int incfs_ioctl_get_flags(struct file *f, void __user *arg)
913
+{
914
+ u32 flags = IS_VERITY(file_inode(f)) ? FS_VERITY_FL : 0;
915
+
916
+ return put_user(flags, (int __user *) arg);
917
+}
918
+
1478919 static long dispatch_ioctl(struct file *f, unsigned int req, unsigned long arg)
1479920 {
1480
- struct mount_info *mi = get_mount_info(file_superblock(f));
1481
-
1482921 switch (req) {
1483
- case INCFS_IOC_CREATE_FILE:
1484
- return ioctl_create_file(mi, (void __user *)arg);
1485922 case INCFS_IOC_FILL_BLOCKS:
1486923 return ioctl_fill_blocks(f, (void __user *)arg);
1487
- case INCFS_IOC_PERMIT_FILL:
1488
- return ioctl_permit_fill(f, (void __user *)arg);
1489924 case INCFS_IOC_READ_FILE_SIGNATURE:
1490925 return ioctl_read_file_signature(f, (void __user *)arg);
1491926 case INCFS_IOC_GET_FILLED_BLOCKS:
1492927 return ioctl_get_filled_blocks(f, (void __user *)arg);
928
+ case INCFS_IOC_GET_BLOCK_COUNT:
929
+ return ioctl_get_block_count(f, (void __user *)arg);
930
+ case FS_IOC_ENABLE_VERITY:
931
+ return incfs_ioctl_enable_verity(f, (const void __user *)arg);
932
+ case FS_IOC_GETFLAGS:
933
+ return incfs_ioctl_get_flags(f, (void __user *) arg);
934
+ case FS_IOC_MEASURE_VERITY:
935
+ return incfs_ioctl_measure_verity(f, (void __user *)arg);
936
+ case FS_IOC_READ_VERITY_METADATA:
937
+ return incfs_ioctl_read_verity_metadata(f, (void __user *)arg);
1493938 default:
1494939 return -EINVAL;
1495940 }
1496941 }
942
+
943
+#ifdef CONFIG_COMPAT
944
+static long incfs_compat_ioctl(struct file *file, unsigned int cmd,
945
+ unsigned long arg)
946
+{
947
+ switch (cmd) {
948
+ case FS_IOC32_GETFLAGS:
949
+ cmd = FS_IOC_GETFLAGS;
950
+ break;
951
+ case INCFS_IOC_FILL_BLOCKS:
952
+ case INCFS_IOC_READ_FILE_SIGNATURE:
953
+ case INCFS_IOC_GET_FILLED_BLOCKS:
954
+ case INCFS_IOC_GET_BLOCK_COUNT:
955
+ case FS_IOC_ENABLE_VERITY:
956
+ case FS_IOC_MEASURE_VERITY:
957
+ case FS_IOC_READ_VERITY_METADATA:
958
+ break;
959
+ default:
960
+ return -ENOIOCTLCMD;
961
+ }
962
+ return dispatch_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
963
+}
964
+#endif
1497965
1498966 static struct dentry *dir_lookup(struct inode *dir_inode, struct dentry *dentry,
1499967 unsigned int flags)
....@@ -1503,40 +971,18 @@
1503971 struct dentry *backing_dentry = NULL;
1504972 struct path dir_backing_path = {};
1505973 struct inode_info *dir_info = get_incfs_node(dir_inode);
1506
- struct mem_range name_range =
1507
- range((u8 *)dentry->d_name.name, dentry->d_name.len);
1508974 int err = 0;
975
+
976
+ if (!mi || !dir_info || !dir_info->n_backing_inode)
977
+ return ERR_PTR(-EBADF);
1509978
1510979 if (d_inode(mi->mi_backing_dir_path.dentry) ==
1511980 dir_info->n_backing_inode) {
1512981 /* We do lookup in the FS root. Show pseudo files. */
1513
-
1514
- if (incfs_equal_ranges(pending_reads_file_name_range,
1515
- name_range)) {
1516
- struct inode *inode = fetch_pending_reads_inode(
1517
- dir_inode->i_sb);
1518
-
1519
- if (IS_ERR(inode)) {
1520
- err = PTR_ERR(inode);
1521
- goto out;
1522
- }
1523
-
1524
- d_add(dentry, inode);
982
+ err = dir_lookup_pseudo_files(dir_inode->i_sb, dentry);
983
+ if (err != -ENOENT)
1525984 goto out;
1526
- }
1527
-
1528
- if (incfs_equal_ranges(log_file_name_range, name_range)) {
1529
- struct inode *inode = fetch_log_inode(
1530
- dir_inode->i_sb);
1531
-
1532
- if (IS_ERR(inode)) {
1533
- err = PTR_ERR(inode);
1534
- goto out;
1535
- }
1536
-
1537
- d_add(dentry, inode);
1538
- goto out;
1539
- }
985
+ err = 0;
1540986 }
1541987
1542988 dir_dentry = dget_parent(dentry);
....@@ -1622,7 +1068,7 @@
16221068
16231069 if (!backing_dentry) {
16241070 err = -EBADF;
1625
- goto out;
1071
+ goto path_err;
16261072 }
16271073
16281074 if (backing_dentry->d_parent == mi->mi_index_dir) {
....@@ -1631,13 +1077,19 @@
16311077 goto out;
16321078 }
16331079
1080
+ if (backing_dentry->d_parent == mi->mi_incomplete_dir) {
1081
+ /* Can't create a subdir inside .incomplete */
1082
+ err = -EBUSY;
1083
+ goto out;
1084
+ }
16341085 inode_lock_nested(dir_node->n_backing_inode, I_MUTEX_PARENT);
16351086 err = vfs_mkdir(dir_node->n_backing_inode, backing_dentry, mode | 0222);
16361087 inode_unlock(dir_node->n_backing_inode);
16371088 if (!err) {
16381089 struct inode *inode = NULL;
16391090
1640
- if (d_really_is_negative(backing_dentry)) {
1091
+ if (d_really_is_negative(backing_dentry) ||
1092
+ unlikely(d_unhashed(backing_dentry))) {
16411093 err = -EINVAL;
16421094 goto out;
16431095 }
....@@ -1654,23 +1106,33 @@
16541106 if (d_really_is_negative(dentry))
16551107 d_drop(dentry);
16561108 path_put(&backing_path);
1109
+
1110
+path_err:
16571111 mutex_unlock(&mi->mi_dir_struct_mutex);
16581112 if (err)
16591113 pr_debug("incfs: %s err:%d\n", __func__, err);
16601114 return err;
16611115 }
16621116
1663
-/* Delete file referenced by backing_dentry and also its hardlink from .index */
1664
-static int final_file_delete(struct mount_info *mi,
1665
- struct dentry *backing_dentry)
1117
+/*
1118
+ * Delete file referenced by backing_dentry and if appropriate its hardlink
1119
+ * from .index and .incomplete
1120
+ */
1121
+static int file_delete(struct mount_info *mi, struct dentry *dentry,
1122
+ struct dentry *backing_dentry, int nlink)
16661123 {
16671124 struct dentry *index_file_dentry = NULL;
1125
+ struct dentry *incomplete_file_dentry = NULL;
16681126 /* 2 chars per byte of file ID + 1 char for \0 */
16691127 char file_id_str[2 * sizeof(incfs_uuid_t) + 1] = {0};
16701128 ssize_t uuid_size = 0;
16711129 int error = 0;
16721130
16731131 WARN_ON(!mutex_is_locked(&mi->mi_dir_struct_mutex));
1132
+
1133
+ if (nlink > 3)
1134
+ goto just_unlink;
1135
+
16741136 uuid_size = vfs_getxattr(backing_dentry, INCFS_XATTR_ID_NAME,
16751137 file_id_str, 2 * sizeof(incfs_uuid_t));
16761138 if (uuid_size < 0) {
....@@ -1686,17 +1148,50 @@
16861148 index_file_dentry = incfs_lookup_dentry(mi->mi_index_dir, file_id_str);
16871149 if (IS_ERR(index_file_dentry)) {
16881150 error = PTR_ERR(index_file_dentry);
1151
+ index_file_dentry = NULL;
16891152 goto out;
16901153 }
16911154
1692
- error = incfs_unlink(backing_dentry);
1693
- if (error)
1694
- goto out;
1155
+ if (d_really_is_positive(index_file_dentry) && nlink > 0)
1156
+ nlink--;
16951157
1696
- if (d_really_is_positive(index_file_dentry))
1158
+ if (nlink > 2)
1159
+ goto just_unlink;
1160
+
1161
+ incomplete_file_dentry = incfs_lookup_dentry(mi->mi_incomplete_dir,
1162
+ file_id_str);
1163
+ if (IS_ERR(incomplete_file_dentry)) {
1164
+ error = PTR_ERR(incomplete_file_dentry);
1165
+ incomplete_file_dentry = NULL;
1166
+ goto out;
1167
+ }
1168
+
1169
+ if (d_really_is_positive(incomplete_file_dentry) && nlink > 0)
1170
+ nlink--;
1171
+
1172
+ if (nlink > 1)
1173
+ goto just_unlink;
1174
+
1175
+ if (d_really_is_positive(index_file_dentry)) {
16971176 error = incfs_unlink(index_file_dentry);
1177
+ if (error)
1178
+ goto out;
1179
+ notify_unlink(dentry, file_id_str, INCFS_INDEX_NAME);
1180
+ }
1181
+
1182
+ if (d_really_is_positive(incomplete_file_dentry)) {
1183
+ error = incfs_unlink(incomplete_file_dentry);
1184
+ if (error)
1185
+ goto out;
1186
+ notify_unlink(dentry, file_id_str, INCFS_INCOMPLETE_NAME);
1187
+ }
1188
+
1189
+just_unlink:
1190
+ error = incfs_unlink(backing_dentry);
1191
+
16981192 out:
16991193 dput(index_file_dentry);
1194
+ dput(incomplete_file_dentry);
17001195 if (error)
17011196 pr_debug("incfs: delete_file_from_index err:%d\n", error);
17021197 return error;
....@@ -1709,6 +1204,9 @@
17091204 struct kstat stat;
17101205 int err = 0;
17111206
1207
+ if (!mi)
1208
+ return -EBADF;
1209
+
17121210 err = mutex_lock_interruptible(&mi->mi_dir_struct_mutex);
17131211 if (err)
17141212 return err;
....@@ -1716,11 +1214,17 @@
17161214 get_incfs_backing_path(dentry, &backing_path);
17171215 if (!backing_path.dentry) {
17181216 err = -EBADF;
1719
- goto out;
1217
+ goto path_err;
17201218 }
17211219
17221220 if (backing_path.dentry->d_parent == mi->mi_index_dir) {
17231221 /* Direct unlink from .index are not allowed. */
1222
+ err = -EBUSY;
1223
+ goto out;
1224
+ }
1225
+
1226
+ if (backing_path.dentry->d_parent == mi->mi_incomplete_dir) {
1227
+ /* Direct unlink from .incomplete are not allowed. */
17241228 err = -EBUSY;
17251229 goto out;
17261230 }
....@@ -1730,20 +1234,12 @@
17301234 if (err)
17311235 goto out;
17321236
1733
- if (stat.nlink == 2) {
1734
- /*
1735
- * This is the last named link to this file. The only one left
1736
- * is in .index. Remove them both now.
1737
- */
1738
- err = final_file_delete(mi, backing_path.dentry);
1739
- } else {
1740
- /* There are other links to this file. Remove just this one. */
1741
- err = incfs_unlink(backing_path.dentry);
1742
- }
1237
+ err = file_delete(mi, dentry, backing_path.dentry, stat.nlink);
17431238
17441239 d_drop(dentry);
17451240 out:
17461241 path_put(&backing_path);
1242
+path_err:
17471243 if (err)
17481244 pr_debug("incfs: %s err:%d\n", __func__, err);
17491245 mutex_unlock(&mi->mi_dir_struct_mutex);
....@@ -1758,6 +1254,9 @@
17581254 struct path backing_new_path = {};
17591255 int error = 0;
17601256
1257
+ if (!mi)
1258
+ return -EBADF;
1259
+
17611260 error = mutex_lock_interruptible(&mi->mi_dir_struct_mutex);
17621261 if (error)
17631262 return error;
....@@ -1767,6 +1266,12 @@
17671266
17681267 if (backing_new_path.dentry->d_parent == mi->mi_index_dir) {
17691268 /* Can't link to .index */
1269
+ error = -EBUSY;
1270
+ goto out;
1271
+ }
1272
+
1273
+ if (backing_new_path.dentry->d_parent == mi->mi_incomplete_dir) {
1274
+ /* Can't link to .incomplete */
17701275 error = -EBUSY;
17711276 goto out;
17721277 }
....@@ -1804,6 +1309,9 @@
18041309 struct path backing_path = {};
18051310 int err = 0;
18061311
1312
+ if (!mi)
1313
+ return -EBADF;
1314
+
18071315 err = mutex_lock_interruptible(&mi->mi_dir_struct_mutex);
18081316 if (err)
18091317 return err;
....@@ -1811,11 +1319,17 @@
18111319 get_incfs_backing_path(dentry, &backing_path);
18121320 if (!backing_path.dentry) {
18131321 err = -EBADF;
1814
- goto out;
1322
+ goto path_err;
18151323 }
18161324
18171325 if (backing_path.dentry == mi->mi_index_dir) {
18181326 /* Can't delete .index */
1327
+ err = -EBUSY;
1328
+ goto out;
1329
+ }
1330
+
1331
+ if (backing_path.dentry == mi->mi_incomplete_dir) {
1332
+ /* Can't delete .incomplete */
18191333 err = -EBUSY;
18201334 goto out;
18211335 }
....@@ -1825,6 +1339,8 @@
18251339 d_drop(dentry);
18261340 out:
18271341 path_put(&backing_path);
1342
+
1343
+path_err:
18281344 if (err)
18291345 pr_debug("incfs: %s err:%d\n", __func__, err);
18301346 mutex_unlock(&mi->mi_dir_struct_mutex);
....@@ -1843,14 +1359,19 @@
18431359 struct dentry *trap;
18441360 int error = 0;
18451361
1846
- if (!mi)
1847
- return -EBADF;
1848
-
18491362 error = mutex_lock_interruptible(&mi->mi_dir_struct_mutex);
18501363 if (error)
18511364 return error;
18521365
18531366 backing_old_dentry = get_incfs_dentry(old_dentry)->backing_path.dentry;
1367
+
1368
+ if (!backing_old_dentry || backing_old_dentry == mi->mi_index_dir ||
1369
+ backing_old_dentry == mi->mi_incomplete_dir) {
1370
+ /* Renaming .index or .incomplete not allowed */
1371
+ error = -EBUSY;
1372
+ goto exit;
1373
+ }
1374
+
18541375 backing_new_dentry = get_incfs_dentry(new_dentry)->backing_path.dentry;
18551376 dget(backing_old_dentry);
18561377 dget(backing_new_dentry);
....@@ -1859,8 +1380,9 @@
18591380 backing_new_dir_dentry = dget_parent(backing_new_dentry);
18601381 target_inode = d_inode(new_dentry);
18611382
1862
- if (backing_old_dir_dentry == mi->mi_index_dir) {
1863
- /* Direct moves from .index are not allowed. */
1383
+ if (backing_old_dir_dentry == mi->mi_index_dir ||
1384
+ backing_old_dir_dentry == mi->mi_incomplete_dir) {
1385
+ /* Direct moves from .index or .incomplete are not allowed. */
18641386 error = -EBUSY;
18651387 goto out;
18661388 }
....@@ -1897,6 +1419,7 @@
18971419 dput(backing_new_dentry);
18981420 dput(backing_old_dentry);
18991421
1422
+exit:
19001423 mutex_unlock(&mi->mi_dir_struct_mutex);
19011424 if (error)
19021425 pr_debug("incfs: %s err:%d\n", __func__, error);
....@@ -1910,12 +1433,21 @@
19101433 struct file *backing_file = NULL;
19111434 struct path backing_path = {};
19121435 int err = 0;
1436
+ int flags = O_NOATIME | O_LARGEFILE |
1437
+ (S_ISDIR(inode->i_mode) ? O_RDONLY : O_RDWR);
19131438 const struct cred *old_cred;
19141439
1440
+ WARN_ON(file->private_data);
1441
+
1442
+ if (!mi)
1443
+ return -EBADF;
1444
+
19151445 get_incfs_backing_path(file->f_path.dentry, &backing_path);
1446
+ if (!backing_path.dentry)
1447
+ return -EBADF;
1448
+
19161449 old_cred = override_creds(mi->mi_owner);
1917
- backing_file = dentry_open(&backing_path,
1918
- O_RDWR | O_NOATIME | O_LARGEFILE, current_cred());
1450
+ backing_file = dentry_open(&backing_path, flags, current_cred());
19191451 revert_creds(old_cred);
19201452 path_put(&backing_path);
19211453
....@@ -1926,8 +1458,25 @@
19261458 }
19271459
19281460 if (S_ISREG(inode->i_mode)) {
1461
+ struct incfs_file_data *fd = kzalloc(sizeof(*fd), GFP_NOFS);
1462
+
1463
+ if (!fd) {
1464
+ err = -ENOMEM;
1465
+ goto out;
1466
+ }
1467
+
1468
+ *fd = (struct incfs_file_data) {
1469
+ .fd_fill_permission = CANT_FILL,
1470
+ };
1471
+ file->private_data = fd;
1472
+
19291473 err = make_inode_ready_for_data_ops(mi, inode, backing_file);
1930
- file->private_data = (void *)CANT_FILL;
1474
+ if (err)
1475
+ goto out;
1476
+
1477
+ err = incfs_fsverity_file_open(inode, file);
1478
+ if (err)
1479
+ goto out;
19311480 } else if (S_ISDIR(inode->i_mode)) {
19321481 struct dir_file *dir = NULL;
19331482
....@@ -1940,9 +1489,17 @@
19401489 err = -EBADF;
19411490
19421491 out:
1943
- if (err)
1944
- pr_debug("incfs: %s name:%s err: %d\n", __func__,
1945
- file->f_path.dentry->d_name.name, err);
1492
+ if (err) {
1493
+ pr_debug("name:%s err: %d\n",
1494
+ file->f_path.dentry->d_name.name, err);
1495
+ if (S_ISREG(inode->i_mode))
1496
+ kfree(file->private_data);
1497
+ else if (S_ISDIR(inode->i_mode))
1498
+ incfs_free_dir_file(file->private_data);
1499
+
1500
+ file->private_data = NULL;
1501
+ }
1502
+
19461503 if (backing_file)
19471504 fput(backing_file);
19481505 return err;
....@@ -1951,9 +1508,8 @@
19511508 static int file_release(struct inode *inode, struct file *file)
19521509 {
19531510 if (S_ISREG(inode->i_mode)) {
1954
- /* Do nothing.
1955
- * data_file is released only by inode eviction.
1956
- */
1511
+ kfree(file->private_data);
1512
+ file->private_data = NULL;
19571513 } else if (S_ISDIR(inode->i_mode)) {
19581514 struct dir_file *dir = get_incfs_dir_file(file);
19591515
....@@ -2056,6 +1612,10 @@
20561612 if (ia->ia_valid & ATTR_SIZE)
20571613 return -EINVAL;
20581614
1615
+ if ((ia->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
1616
+ (ia->ia_valid & ATTR_MODE))
1617
+ return -EINVAL;
1618
+
20591619 if (!di)
20601620 return -EINVAL;
20611621 backing_dentry = di->backing_path.dentry;
....@@ -2085,6 +1645,43 @@
20851645 return simple_setattr(dentry, ia);
20861646 }
20871647
1648
+
1649
+static int incfs_getattr(const struct path *path,
1650
+ struct kstat *stat, u32 request_mask,
1651
+ unsigned int query_flags)
1652
+{
1653
+ struct inode *inode = d_inode(path->dentry);
1654
+
1655
+ generic_fillattr(inode, stat);
1656
+
1657
+ if (inode->i_ino < INCFS_START_INO_RANGE)
1658
+ return 0;
1659
+
1660
+ stat->attributes &= ~STATX_ATTR_VERITY;
1661
+ if (IS_VERITY(inode))
1662
+ stat->attributes |= STATX_ATTR_VERITY;
1663
+ stat->attributes_mask |= STATX_ATTR_VERITY;
1664
+
1665
+ if (request_mask & STATX_BLOCKS) {
1666
+ struct kstat backing_kstat;
1667
+ struct dentry_info *di = get_incfs_dentry(path->dentry);
1668
+ int error = 0;
1669
+ struct path *backing_path;
1670
+
1671
+ if (!di)
1672
+ return -EFSCORRUPTED;
1673
+ backing_path = &di->backing_path;
1674
+ error = vfs_getattr(backing_path, &backing_kstat, STATX_BLOCKS,
1675
+ AT_STATX_SYNC_AS_STAT);
1676
+ if (error)
1677
+ return error;
1678
+
1679
+ stat->blocks = backing_kstat.blocks;
1680
+ }
1681
+
1682
+ return 0;
1683
+}
1684
+
20881685 static ssize_t incfs_getxattr(struct dentry *d, const char *name,
20891686 void *value, size_t size)
20901687 {
....@@ -2092,9 +1689,7 @@
20921689 struct mount_info *mi = get_mount_info(d->d_sb);
20931690 char *stored_value;
20941691 size_t stored_size;
2095
-
2096
- if (!mi)
2097
- return -EBADF;
1692
+ int i;
20981693
20991694 if (di && di->backing_path.dentry)
21001695 return vfs_getxattr(di->backing_path.dentry, name, value, size);
....@@ -2102,16 +1697,14 @@
21021697 if (strcmp(name, "security.selinux"))
21031698 return -ENODATA;
21041699
2105
- if (!strcmp(d->d_iname, INCFS_PENDING_READS_FILENAME)) {
2106
- stored_value = mi->pending_read_xattr;
2107
- stored_size = mi->pending_read_xattr_size;
2108
- } else if (!strcmp(d->d_iname, INCFS_LOG_FILENAME)) {
2109
- stored_value = mi->log_xattr;
2110
- stored_size = mi->log_xattr_size;
2111
- } else {
1700
+ for (i = 0; i < PSEUDO_FILE_COUNT; ++i)
1701
+ if (!strcmp(d->d_iname, incfs_pseudo_file_names[i].data))
1702
+ break;
1703
+ if (i == PSEUDO_FILE_COUNT)
21121704 return -ENODATA;
2113
- }
21141705
1706
+ stored_value = mi->pseudo_file_xattr[i].data;
1707
+ stored_size = mi->pseudo_file_xattr[i].len;
21151708 if (!stored_value)
21161709 return -ENODATA;
21171710
....@@ -2120,7 +1713,6 @@
21201713
21211714 memcpy(value, stored_value, stored_size);
21221715 return stored_size;
2123
-
21241716 }
21251717
21261718
....@@ -2129,11 +1721,9 @@
21291721 {
21301722 struct dentry_info *di = get_incfs_dentry(d);
21311723 struct mount_info *mi = get_mount_info(d->d_sb);
2132
- void **stored_value;
1724
+ u8 **stored_value;
21331725 size_t *stored_size;
2134
-
2135
- if (!mi)
2136
- return -EBADF;
1726
+ int i;
21371727
21381728 if (di && di->backing_path.dentry)
21391729 return vfs_setxattr(di->backing_path.dentry, name, value, size,
....@@ -2145,16 +1735,14 @@
21451735 if (size > INCFS_MAX_FILE_ATTR_SIZE)
21461736 return -E2BIG;
21471737
2148
- if (!strcmp(d->d_iname, INCFS_PENDING_READS_FILENAME)) {
2149
- stored_value = &mi->pending_read_xattr;
2150
- stored_size = &mi->pending_read_xattr_size;
2151
- } else if (!strcmp(d->d_iname, INCFS_LOG_FILENAME)) {
2152
- stored_value = &mi->log_xattr;
2153
- stored_size = &mi->log_xattr_size;
2154
- } else {
1738
+ for (i = 0; i < PSEUDO_FILE_COUNT; ++i)
1739
+ if (!strcmp(d->d_iname, incfs_pseudo_file_names[i].data))
1740
+ break;
1741
+ if (i == PSEUDO_FILE_COUNT)
21551742 return -ENODATA;
2156
- }
21571743
1744
+ stored_value = &mi->pseudo_file_xattr[i].data;
1745
+ stored_size = &mi->pseudo_file_xattr[i].len;
21581746 kfree (*stored_value);
21591747 *stored_value = kzalloc(size, GFP_NOFS);
21601748 if (!*stored_value)
....@@ -2175,22 +1763,18 @@
21751763 return vfs_listxattr(di->backing_path.dentry, list, size);
21761764 }
21771765
2178
-static int incfs_test_super(struct super_block *s, void *p)
2179
-{
2180
- return s->s_fs_info != NULL;
2181
-}
2182
-
21831766 struct dentry *incfs_mount_fs(struct file_system_type *type, int flags,
21841767 const char *dev_name, void *data)
21851768 {
21861769 struct mount_options options = {};
21871770 struct mount_info *mi = NULL;
21881771 struct path backing_dir_path = {};
2189
- struct dentry *index_dir;
1772
+ struct dentry *index_dir = NULL;
1773
+ struct dentry *incomplete_dir = NULL;
21901774 struct super_block *src_fs_sb = NULL;
21911775 struct inode *root_inode = NULL;
2192
- struct super_block *sb = sget(type, incfs_test_super, set_anon_super,
2193
- flags, NULL);
1776
+ struct super_block *sb = sget(type, NULL, set_anon_super, flags, NULL);
1777
+ bool dir_created = false;
21941778 int error = 0;
21951779
21961780 if (IS_ERR(sb))
....@@ -2199,7 +1783,7 @@
21991783 sb->s_op = &incfs_super_ops;
22001784 sb->s_d_op = &incfs_dentry_ops;
22011785 sb->s_flags |= S_NOATIME;
2202
- sb->s_magic = (long)INCFS_MAGIC_NUMBER;
1786
+ sb->s_magic = INCFS_MAGIC_NUMBER;
22031787 sb->s_time_gran = 1;
22041788 sb->s_blocksize = INCFS_DATA_FILE_BLOCK_SIZE;
22051789 sb->s_blocksize_bits = blksize_bits(sb->s_blocksize);
....@@ -2207,17 +1791,23 @@
22071791
22081792 BUILD_BUG_ON(PAGE_SIZE != INCFS_DATA_FILE_BLOCK_SIZE);
22091793
1794
+ if (!dev_name) {
1795
+ pr_err("incfs: Backing dir is not set, filesystem can't be mounted.\n");
1796
+ error = -ENOENT;
1797
+ goto err_deactivate;
1798
+ }
1799
+
22101800 error = parse_options(&options, (char *)data);
22111801 if (error != 0) {
22121802 pr_err("incfs: Options parsing error. %d\n", error);
2213
- goto err;
1803
+ goto err_deactivate;
22141804 }
22151805
22161806 sb->s_bdi->ra_pages = options.readahead_pages;
22171807 if (!dev_name) {
22181808 pr_err("incfs: Backing dir is not set, filesystem can't be mounted.\n");
22191809 error = -ENOENT;
2220
- goto err;
1810
+ goto err_free_opts;
22211811 }
22221812
22231813 error = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
....@@ -2226,62 +1816,80 @@
22261816 !d_really_is_positive(backing_dir_path.dentry)) {
22271817 pr_err("incfs: Error accessing: %s.\n",
22281818 dev_name);
2229
- goto err;
1819
+ goto err_free_opts;
22301820 }
22311821 src_fs_sb = backing_dir_path.dentry->d_sb;
22321822 sb->s_maxbytes = src_fs_sb->s_maxbytes;
1823
+ sb->s_stack_depth = src_fs_sb->s_stack_depth + 1;
22331824
2234
- if (!sb->s_fs_info) {
2235
- mi = incfs_alloc_mount_info(sb, &options, &backing_dir_path);
2236
-
2237
- if (IS_ERR_OR_NULL(mi)) {
2238
- error = PTR_ERR(mi);
2239
- pr_err("incfs: Error allocating mount info. %d\n",
2240
- error);
2241
- mi = NULL;
2242
- goto err;
2243
- }
2244
- sb->s_fs_info = mi;
2245
- } else {
2246
- mi = sb->s_fs_info;
1825
+ if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1826
+ error = -EINVAL;
1827
+ goto err_put_path;
22471828 }
22481829
2249
- index_dir = open_or_create_index_dir(backing_dir_path.dentry);
1830
+ mi = incfs_alloc_mount_info(sb, &options, &backing_dir_path);
1831
+ if (IS_ERR_OR_NULL(mi)) {
1832
+ error = PTR_ERR(mi);
1833
+ pr_err("incfs: Error allocating mount info. %d\n", error);
1834
+ goto err_put_path;
1835
+ }
1836
+
1837
+ sb->s_fs_info = mi;
1838
+ mi->mi_backing_dir_path = backing_dir_path;
1839
+ index_dir = open_or_create_special_dir(backing_dir_path.dentry,
1840
+ INCFS_INDEX_NAME, &dir_created);
22501841 if (IS_ERR_OR_NULL(index_dir)) {
22511842 error = PTR_ERR(index_dir);
22521843 pr_err("incfs: Can't find or create .index dir in %s\n",
22531844 dev_name);
2254
- goto err;
1845
+ /* No need to null index_dir since we don't put it */
1846
+ goto err_put_path;
22551847 }
1848
+
22561849 mi->mi_index_dir = index_dir;
1850
+ mi->mi_index_free = dir_created;
1851
+
1852
+ incomplete_dir = open_or_create_special_dir(backing_dir_path.dentry,
1853
+ INCFS_INCOMPLETE_NAME,
1854
+ &dir_created);
1855
+ if (IS_ERR_OR_NULL(incomplete_dir)) {
1856
+ error = PTR_ERR(incomplete_dir);
1857
+ pr_err("incfs: Can't find or create .incomplete dir in %s\n",
1858
+ dev_name);
1859
+ /* No need to null incomplete_dir since we don't put it */
1860
+ goto err_put_path;
1861
+ }
1862
+ mi->mi_incomplete_dir = incomplete_dir;
1863
+ mi->mi_incomplete_free = dir_created;
22571864
22581865 root_inode = fetch_regular_inode(sb, backing_dir_path.dentry);
22591866 if (IS_ERR(root_inode)) {
22601867 error = PTR_ERR(root_inode);
2261
- goto err;
1868
+ goto err_put_path;
22621869 }
22631870
1871
+ sb->s_root = d_make_root(root_inode);
22641872 if (!sb->s_root) {
2265
- sb->s_root = d_make_root(root_inode);
2266
- if (!sb->s_root) {
2267
- error = -ENOMEM;
2268
- goto err;
2269
- }
2270
- error = incfs_init_dentry(sb->s_root, &backing_dir_path);
2271
- if (error)
2272
- goto err;
1873
+ error = -ENOMEM;
1874
+ goto err_put_path;
22731875 }
1876
+ error = incfs_init_dentry(sb->s_root, &backing_dir_path);
1877
+ if (error)
1878
+ goto err_put_path;
22741879
2275
- mi->mi_backing_dir_path = backing_dir_path;
1880
+ path_put(&backing_dir_path);
22761881 sb->s_flags |= SB_ACTIVE;
22771882
22781883 pr_debug("incfs: mount\n");
22791884 return dget(sb->s_root);
2280
-err:
2281
- sb->s_fs_info = NULL;
1885
+
1886
+err_put_path:
22821887 path_put(&backing_dir_path);
2283
- incfs_free_mount_info(mi);
1888
+err_free_opts:
1889
+ free_options(&options);
1890
+err_deactivate:
22841891 deactivate_locked_super(sb);
1892
+ pr_err("incfs: mount failed %d\n", error);
22851893 return ERR_PTR(error);
22861894 }
22871895
....@@ -2291,39 +1899,62 @@
22911899 struct mount_info *mi = get_mount_info(sb);
22921900 int err = 0;
22931901
2294
- if (!mi)
2295
- return err;
2296
-
22971902 sync_filesystem(sb);
22981903 err = parse_options(&options, (char *)data);
22991904 if (err)
23001905 return err;
23011906
1907
+ if (options.report_uid != mi->mi_options.report_uid) {
1908
+ pr_err("incfs: Can't change report_uid mount option on remount\n");
1909
+ err = -EOPNOTSUPP;
1910
+ goto out;
1911
+ }
1912
+
23021913 err = incfs_realloc_mount_info(mi, &options);
23031914 if (err)
2304
- return err;
1915
+ goto out;
23051916
23061917 pr_debug("incfs: remount\n");
2307
- return 0;
1918
+
1919
+out:
1920
+ free_options(&options);
1921
+ return err;
23081922 }
23091923
23101924 void incfs_kill_sb(struct super_block *sb)
23111925 {
23121926 struct mount_info *mi = sb->s_fs_info;
1927
+ struct inode *dinode = NULL;
23131928
23141929 pr_debug("incfs: unmount\n");
2315
- vfs_rmdir(d_inode(mi->mi_backing_dir_path.dentry), mi->mi_index_dir);
1930
+
1931
+ /*
1932
+ * We must kill the super before freeing mi, since killing the super
1933
+ * triggers inode eviction, which triggers the final update of the
1934
+ * backing file, which uses certain information for mi
1935
+ */
23161936 kill_anon_super(sb);
2317
- incfs_free_mount_info(mi);
2318
- sb->s_fs_info = NULL;
1937
+
1938
+ if (mi) {
1939
+ if (mi->mi_backing_dir_path.dentry)
1940
+ dinode = d_inode(mi->mi_backing_dir_path.dentry);
1941
+
1942
+ if (dinode) {
1943
+ if (mi->mi_index_dir && mi->mi_index_free)
1944
+ vfs_rmdir(dinode, mi->mi_index_dir);
1945
+
1946
+ if (mi->mi_incomplete_dir && mi->mi_incomplete_free)
1947
+ vfs_rmdir(dinode, mi->mi_incomplete_dir);
1948
+ }
1949
+
1950
+ incfs_free_mount_info(mi);
1951
+ sb->s_fs_info = NULL;
1952
+ }
23191953 }
23201954
23211955 static int show_options(struct seq_file *m, struct dentry *root)
23221956 {
23231957 struct mount_info *mi = get_mount_info(root->d_sb);
2324
-
2325
- if (!mi)
2326
- return -EBADF;
23271958
23281959 seq_printf(m, ",read_timeout_ms=%u", mi->mi_options.read_timeout_ms);
23291960 seq_printf(m, ",readahead=%u", mi->mi_options.readahead_pages);
....@@ -2332,9 +1963,11 @@
23321963 seq_printf(m, ",rlog_wakeup_cnt=%u",
23331964 mi->mi_options.read_log_wakeup_count);
23341965 }
2335
- if (mi->mi_options.no_backing_file_cache)
2336
- seq_puts(m, ",no_bf_cache");
2337
- if (mi->mi_options.no_backing_file_readahead)
2338
- seq_puts(m, ",no_bf_readahead");
1966
+ if (mi->mi_options.report_uid)
1967
+ seq_puts(m, ",report_uid");
1968
+
1969
+ if (mi->mi_sysfs_node)
1970
+ seq_printf(m, ",sysfs_name=%s",
1971
+ kobject_name(&mi->mi_sysfs_node->isn_sysfs_node));
23391972 return 0;
23401973 }