hc
2023-12-06 08f87f769b595151be1afeff53e144f543faa614
kernel/fs/incfs/vfs.c
....@@ -4,32 +4,27 @@
44 */
55
66 #include <linux/blkdev.h>
7
-#include <linux/cred.h>
8
-#include <linux/eventpoll.h>
7
+#include <linux/compat.h>
98 #include <linux/file.h>
109 #include <linux/fs.h>
1110 #include <linux/fs_stack.h>
11
+#include <linux/fsnotify.h>
12
+#include <linux/fsverity.h>
13
+#include <linux/mmap_lock.h>
1214 #include <linux/namei.h>
1315 #include <linux/parser.h>
14
-#include <linux/poll.h>
1516 #include <linux/seq_file.h>
16
-#include <linux/syscalls.h>
17
-#include <linux/xattr.h>
1817
1918 #include <uapi/linux/incrementalfs.h>
2019
2120 #include "vfs.h"
21
+
2222 #include "data_mgmt.h"
2323 #include "format.h"
24
-#include "integrity.h"
2524 #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
25
+#include "pseudo_files.h"
26
+#include "sysfs.h"
27
+#include "verity.h"
3328
3429 static int incfs_remount_fs(struct super_block *sb, int *flags, char *data);
3530
....@@ -52,23 +47,19 @@
5247 static int read_single_page(struct file *f, struct page *page);
5348 static long dispatch_ioctl(struct file *f, unsigned int req, unsigned long arg);
5449
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 *);
50
+#ifdef CONFIG_COMPAT
51
+static long incfs_compat_ioctl(struct file *file, unsigned int cmd,
52
+ unsigned long arg);
53
+#endif
6654
6755 static struct inode *alloc_inode(struct super_block *sb);
6856 static void free_inode(struct inode *inode);
6957 static void evict_inode(struct inode *inode);
7058
7159 static int incfs_setattr(struct dentry *dentry, struct iattr *ia);
60
+static int incfs_getattr(const struct path *path,
61
+ struct kstat *stat, u32 request_mask,
62
+ unsigned int query_flags);
7263 static ssize_t incfs_getxattr(struct dentry *d, const char *name,
7364 void *value, size_t size);
7465 static ssize_t incfs_setxattr(struct dentry *d, const char *name,
....@@ -109,8 +100,6 @@
109100 .iterate = iterate_incfs_dir,
110101 .open = file_open,
111102 .release = file_release,
112
- .unlocked_ioctl = dispatch_ioctl,
113
- .compat_ioctl = dispatch_ioctl
114103 };
115104
116105 static const struct dentry_operations incfs_dentry_ops = {
....@@ -123,51 +112,54 @@
123112 /* .readpages = readpages */
124113 };
125114
126
-static const struct file_operations incfs_file_ops = {
115
+static vm_fault_t incfs_fault(struct vm_fault *vmf)
116
+{
117
+ vmf->flags &= ~FAULT_FLAG_ALLOW_RETRY;
118
+ return filemap_fault(vmf);
119
+}
120
+
121
+static const struct vm_operations_struct incfs_file_vm_ops = {
122
+ .fault = incfs_fault,
123
+ .map_pages = filemap_map_pages,
124
+ .page_mkwrite = filemap_page_mkwrite,
125
+};
126
+
127
+/* This is used for a general mmap of a disk file */
128
+
129
+static int incfs_file_mmap(struct file *file, struct vm_area_struct *vma)
130
+{
131
+ struct address_space *mapping = file->f_mapping;
132
+
133
+ if (!mapping->a_ops->readpage)
134
+ return -ENOEXEC;
135
+ file_accessed(file);
136
+ vma->vm_ops = &incfs_file_vm_ops;
137
+ return 0;
138
+}
139
+
140
+const struct file_operations incfs_file_ops = {
127141 .open = file_open,
128142 .release = file_release,
129143 .read_iter = generic_file_read_iter,
130
- .mmap = generic_file_mmap,
144
+ .mmap = incfs_file_mmap,
131145 .splice_read = generic_file_splice_read,
132146 .llseek = generic_file_llseek,
133147 .unlocked_ioctl = dispatch_ioctl,
134
- .compat_ioctl = dispatch_ioctl
148
+#ifdef CONFIG_COMPAT
149
+ .compat_ioctl = incfs_compat_ioctl,
150
+#endif
135151 };
136152
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 = {
153
+const struct inode_operations incfs_file_inode_ops = {
163154 .setattr = incfs_setattr,
164
- .getattr = simple_getattr,
155
+ .getattr = incfs_getattr,
165156 .listxattr = incfs_listxattr
166157 };
167158
168159 static int incfs_handler_getxattr(const struct xattr_handler *xh,
169160 struct dentry *d, struct inode *inode,
170
- const char *name, void *buffer, size_t size)
161
+ const char *name, void *buffer, size_t size,
162
+ int flags)
171163 {
172164 return incfs_getxattr(d, name, buffer, size);
173165 }
....@@ -191,56 +183,41 @@
191183 NULL,
192184 };
193185
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
-
205186 struct inode_search {
206187 unsigned long ino;
207188
208189 struct dentry *backing_dentry;
209190
210191 size_t size;
192
+
193
+ bool verity;
211194 };
212195
213196 enum parse_parameter {
214197 Opt_read_timeout,
215198 Opt_readahead_pages,
216
- Opt_no_backing_file_cache,
217
- Opt_no_backing_file_readahead,
218199 Opt_rlog_pages,
219200 Opt_rlog_wakeup_cnt,
201
+ Opt_report_uid,
202
+ Opt_sysfs_name,
220203 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
233204 };
234205
235206 static const match_table_t option_tokens = {
236207 { Opt_read_timeout, "read_timeout_ms=%u" },
237208 { 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" },
240209 { Opt_rlog_pages, "rlog_pages=%u" },
241210 { Opt_rlog_wakeup_cnt, "rlog_wakeup_cnt=%u" },
211
+ { Opt_report_uid, "report_uid" },
212
+ { Opt_sysfs_name, "sysfs_name=%s" },
242213 { Opt_err, NULL }
243214 };
215
+
216
+static void free_options(struct mount_options *opts)
217
+{
218
+ kfree(opts->sysfs_name);
219
+ opts->sysfs_name = NULL;
220
+}
244221
245222 static int parse_options(struct mount_options *opts, char *str)
246223 {
....@@ -251,12 +228,13 @@
251228 if (opts == NULL)
252229 return -EFAULT;
253230
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;
231
+ *opts = (struct mount_options) {
232
+ .read_timeout_ms = 1000, /* Default: 1s */
233
+ .readahead_pages = 10,
234
+ .read_log_pages = 2,
235
+ .read_log_wakeup_count = 10,
236
+ };
237
+
260238 if (str == NULL || *str == 0)
261239 return 0;
262240
....@@ -272,22 +250,14 @@
272250 case Opt_read_timeout:
273251 if (match_int(&args[0], &value))
274252 return -EINVAL;
253
+ if (value > 3600000)
254
+ return -EINVAL;
275255 opts->read_timeout_ms = value;
276256 break;
277257 case Opt_readahead_pages:
278258 if (match_int(&args[0], &value))
279259 return -EINVAL;
280260 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);
291261 break;
292262 case Opt_rlog_pages:
293263 if (match_int(&args[0], &value))
....@@ -299,26 +269,19 @@
299269 return -EINVAL;
300270 opts->read_log_wakeup_count = value;
301271 break;
272
+ case Opt_report_uid:
273
+ opts->report_uid = true;
274
+ break;
275
+ case Opt_sysfs_name:
276
+ opts->sysfs_name = match_strdup(&args[0]);
277
+ break;
302278 default:
279
+ free_options(opts);
303280 return -EINVAL;
304281 }
305282 }
306283
307284 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;
322285 }
323286
324287 /* Read file size from the attribute. Quicker than reading the header */
....@@ -336,100 +299,66 @@
336299 return le64_to_cpu(attr_value);
337300 }
338301
302
+/* Read verity flag from the attribute. Quicker than reading the header */
303
+static bool read_verity_attr(struct dentry *backing_dentry)
304
+{
305
+ return vfs_getxattr(backing_dentry, INCFS_XATTR_VERITY_NAME, NULL, 0)
306
+ >= 0;
307
+}
308
+
339309 static int inode_test(struct inode *inode, void *opaque)
340310 {
341311 struct inode_search *search = opaque;
342312 struct inode_info *node = get_incfs_node(inode);
313
+ struct inode *backing_inode = d_inode(search->backing_dentry);
343314
344315 if (!node)
345316 return 0;
346317
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;
318
+ return node->n_backing_inode == backing_inode &&
319
+ inode->i_ino == search->ino;
354320 }
355321
356322 static int inode_set(struct inode *inode, void *opaque)
357323 {
358324 struct inode_search *search = opaque;
359325 struct inode_info *node = get_incfs_node(inode);
326
+ struct dentry *backing_dentry = search->backing_dentry;
327
+ struct inode *backing_inode = d_inode(backing_dentry);
360328
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);
329
+ fsstack_copy_attr_all(inode, backing_inode);
330
+ if (S_ISREG(inode->i_mode)) {
331
+ u64 size = search->size;
365332
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
-
333
+ inode->i_size = size;
334
+ inode->i_blocks = get_blocks_count_for_size(size);
335
+ inode->i_mapping->a_ops = &incfs_address_space_ops;
412336 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;
337
+ inode->i_fop = &incfs_file_ops;
338
+ inode->i_mode &= ~0222;
339
+ if (search->verity)
340
+ inode_set_flags(inode, S_VERITY, S_VERITY);
341
+ } else if (S_ISDIR(inode->i_mode)) {
421342 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
-
343
+ inode->i_blocks = 1;
344
+ inode->i_mapping->a_ops = &incfs_address_space_ops;
345
+ inode->i_op = &incfs_dir_inode_ops;
346
+ inode->i_fop = &incfs_dir_fops;
430347 } else {
431
- /* Unknown inode requested. */
432
- return -EINVAL;
348
+ pr_warn_once("incfs: Unexpected inode type\n");
349
+ return -EBADF;
350
+ }
351
+
352
+ ihold(backing_inode);
353
+ node->n_backing_inode = backing_inode;
354
+ node->n_mount_info = get_mount_info(inode->i_sb);
355
+ inode->i_ctime = backing_inode->i_ctime;
356
+ inode->i_mtime = backing_inode->i_mtime;
357
+ inode->i_atime = backing_inode->i_atime;
358
+ inode->i_ino = backing_inode->i_ino;
359
+ if (backing_inode->i_ino < INCFS_START_INO_RANGE) {
360
+ pr_warn("incfs: ino conflict with backing FS %ld\n",
361
+ backing_inode->i_ino);
433362 }
434363
435364 return 0;
....@@ -443,222 +372,7 @@
443372 .ino = backing_inode->i_ino,
444373 .backing_dentry = backing_dentry,
445374 .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
375
+ .verity = read_verity_attr(backing_dentry),
662376 };
663377 struct inode *inode = iget5_locked(sb, search.ino, inode_test,
664378 inode_set, &search);
....@@ -679,7 +393,7 @@
679393 struct mount_info *mi = get_mount_info(file_superblock(file));
680394 bool root;
681395
682
- if (!dir || !mi) {
396
+ if (!dir) {
683397 error = -EBADF;
684398 goto out;
685399 }
....@@ -687,29 +401,15 @@
687401 root = dir->backing_dir->f_inode
688402 == d_inode(mi->mi_backing_dir_path.dentry);
689403
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;
404
+ if (root) {
405
+ error = emit_pseudo_files(ctx);
406
+ if (error)
695407 goto out;
696
- }
697
- ctx->pos++;
698408 }
699409
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;
410
+ ctx->pos -= PSEUDO_FILE_COUNT;
711411 error = iterate_dir(dir->backing_dir, ctx);
712
- ctx->pos += 2;
412
+ ctx->pos += PSEUDO_FILE_COUNT;
713413 file->f_pos = dir->backing_dir->f_pos;
714414 out:
715415 if (error)
....@@ -736,29 +436,10 @@
736436 return 0;
737437 }
738438
739
-static struct dentry *incfs_lookup_dentry(struct dentry *parent,
740
- const char *name)
439
+static struct dentry *open_or_create_special_dir(struct dentry *backing_dir,
440
+ const char *name,
441
+ bool *created)
741442 {
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";
762443 struct dentry *index_dentry;
763444 struct inode *backing_inode = d_inode(backing_dir);
764445 int err = 0;
....@@ -770,6 +451,7 @@
770451 return index_dentry;
771452 } else if (d_really_is_positive(index_dentry)) {
772453 /* Index already exists. */
454
+ *created = false;
773455 return index_dentry;
774456 }
775457
....@@ -783,12 +465,51 @@
783465 return ERR_PTR(err);
784466 }
785467
786
- if (!d_really_is_positive(index_dentry)) {
468
+ if (!d_really_is_positive(index_dentry) ||
469
+ unlikely(d_unhashed(index_dentry))) {
787470 dput(index_dentry);
788471 return ERR_PTR(-EINVAL);
789472 }
790473
474
+ *created = true;
791475 return index_dentry;
476
+}
477
+
478
+static int read_single_page_timeouts(struct data_file *df, struct file *f,
479
+ int block_index, struct mem_range range,
480
+ struct mem_range tmp)
481
+{
482
+ struct mount_info *mi = df->df_mount_info;
483
+ struct incfs_read_data_file_timeouts timeouts = {
484
+ .max_pending_time_us = U32_MAX,
485
+ };
486
+ int uid = current_uid().val;
487
+ int i;
488
+
489
+ spin_lock(&mi->mi_per_uid_read_timeouts_lock);
490
+ for (i = 0; i < mi->mi_per_uid_read_timeouts_size /
491
+ sizeof(*mi->mi_per_uid_read_timeouts); ++i) {
492
+ struct incfs_per_uid_read_timeouts *t =
493
+ &mi->mi_per_uid_read_timeouts[i];
494
+
495
+ if(t->uid == uid) {
496
+ timeouts.min_time_us = t->min_time_us;
497
+ timeouts.min_pending_time_us = t->min_pending_time_us;
498
+ timeouts.max_pending_time_us = t->max_pending_time_us;
499
+ break;
500
+ }
501
+ }
502
+ spin_unlock(&mi->mi_per_uid_read_timeouts_lock);
503
+ if (timeouts.max_pending_time_us == U32_MAX) {
504
+ u64 read_timeout_us = (u64)mi->mi_options.read_timeout_ms *
505
+ 1000;
506
+
507
+ timeouts.max_pending_time_us = read_timeout_us <= U32_MAX ?
508
+ read_timeout_us : U32_MAX;
509
+ }
510
+
511
+ return incfs_read_data_file_block(range, f, block_index, tmp,
512
+ &timeouts);
792513 }
793514
794515 static int read_single_page(struct file *f, struct page *page)
....@@ -799,28 +520,34 @@
799520 ssize_t read_result = 0;
800521 struct data_file *df = get_incfs_data_file(f);
801522 int result = 0;
802
- void *page_start = kmap(page);
523
+ void *page_start;
803524 int block_index;
804
- int timeout_ms;
805525
806
- if (!df)
526
+ if (!df) {
527
+ SetPageError(page);
528
+ unlock_page(page);
807529 return -EBADF;
530
+ }
808531
532
+ page_start = kmap(page);
809533 offset = page_offset(page);
810
- block_index = offset / INCFS_DATA_FILE_BLOCK_SIZE;
534
+ block_index = (offset + df->df_mapped_offset) /
535
+ INCFS_DATA_FILE_BLOCK_SIZE;
811536 size = df->df_size;
812
- timeout_ms = df->df_mount_info->mi_options.read_timeout_ms;
813537
814538 if (offset < size) {
815539 struct mem_range tmp = {
816540 .len = 2 * INCFS_DATA_FILE_BLOCK_SIZE
817541 };
818
-
819542 tmp.data = (u8 *)__get_free_pages(GFP_NOFS, get_order(tmp.len));
543
+ if (!tmp.data) {
544
+ read_result = -ENOMEM;
545
+ goto err;
546
+ }
820547 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);
548
+
549
+ read_result = read_single_page_timeouts(df, f, block_index,
550
+ range(page_start, bytes_to_read), tmp);
824551
825552 free_pages((unsigned long)tmp.data, get_order(tmp.len));
826553 } else {
....@@ -828,6 +555,7 @@
828555 read_result = 0;
829556 }
830557
558
+err:
831559 if (read_result < 0)
832560 result = read_result;
833561 else if (read_result < PAGE_SIZE)
....@@ -844,137 +572,7 @@
844572 return result;
845573 }
846574
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)
575
+int incfs_link(struct dentry *what, struct dentry *where)
978576 {
979577 struct dentry *parent_dentry = dget_parent(where);
980578 struct inode *pinode = d_inode(parent_dentry);
....@@ -988,7 +586,7 @@
988586 return error;
989587 }
990588
991
-static int incfs_unlink(struct dentry *dentry)
589
+int incfs_unlink(struct dentry *dentry)
992590 {
993591 struct dentry *parent_dentry = dget_parent(dentry);
994592 struct inode *pinode = d_inode(parent_dentry);
....@@ -1016,274 +614,112 @@
1016614 return error;
1017615 }
1018616
1019
-static int dir_relative_path_resolve(
1020
- struct mount_info *mi,
1021
- const char __user *relative_path,
1022
- struct path *result_path)
617
+static void notify_unlink(struct dentry *dentry, const char *file_id_str,
618
+ const char *special_directory)
1023619 {
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;
620
+ struct dentry *root = dentry;
621
+ struct dentry *file = NULL;
622
+ struct dentry *dir = NULL;
1027623 int error = 0;
624
+ bool take_lock = root->d_parent != root->d_parent->d_parent;
1028625
1029
- if (dir_fd < 0)
1030
- return dir_fd;
626
+ while (root != root->d_parent)
627
+ root = root->d_parent;
1031628
1032
- dir_f = dentry_open(base_path, O_RDONLY | O_NOATIME, current_cred());
629
+ if (take_lock)
630
+ dir = incfs_lookup_dentry(root, special_directory);
631
+ else
632
+ dir = lookup_one_len(special_directory, root,
633
+ strlen(special_directory));
1033634
1034
- if (IS_ERR(dir_f)) {
1035
- error = PTR_ERR(dir_f);
635
+ if (IS_ERR(dir)) {
636
+ error = PTR_ERR(dir);
1036637 goto out;
1037638 }
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);
639
+ if (d_is_negative(dir)) {
640
+ error = -ENOENT;
1044641 goto out;
1045642 }
1046643
1047
- error = user_path_at_empty(dir_fd, relative_path,
1048
- LOOKUP_FOLLOW | LOOKUP_DIRECTORY, result_path, NULL);
644
+ file = incfs_lookup_dentry(dir, file_id_str);
645
+ if (IS_ERR(file)) {
646
+ error = PTR_ERR(file);
647
+ goto out;
648
+ }
649
+ if (d_is_negative(file)) {
650
+ error = -ENOENT;
651
+ goto out;
652
+ }
653
+
654
+ fsnotify_unlink(d_inode(dir), file);
655
+ d_delete(file);
1049656
1050657 out:
1051
- ksys_close(dir_fd);
1052658 if (error)
1053
- pr_debug("incfs: %s %d\n", __func__, error);
1054
- return error;
659
+ pr_warn("%s failed with error %d\n", __func__, error);
660
+
661
+ dput(dir);
662
+ dput(file);
1055663 }
1056664
1057
-static int validate_name(char *file_name)
665
+static void maybe_delete_incomplete_file(struct file *f,
666
+ struct data_file *df)
1058667 {
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;
668
+ struct backing_file_context *bfc;
669
+ struct mount_info *mi = df->df_mount_info;
670
+ char *file_id_str = NULL;
671
+ struct dentry *incomplete_file_dentry = NULL;
672
+ const struct cred *old_cred = override_creds(mi->mi_owner);
1080673 int error;
1081674
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
-}
1095
-
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;
1110
-
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)
675
+ if (atomic_read(&df->df_data_blocks_written) < df->df_data_block_count)
1130676 goto out;
1131677
1132
- file_id_str = file_id_to_str(args.file_id);
1133
- if (!file_id_str) {
1134
- error = -ENOMEM;
1135
- goto out;
1136
- }
678
+ /* Truncate file to remove any preallocated space */
679
+ bfc = df->df_backing_file_context;
680
+ if (bfc) {
681
+ struct file *f = bfc->bc_file;
1137682
1138
- error = mutex_lock_interruptible(&mi->mi_dir_struct_mutex);
1139
- if (error)
1140
- goto out;
1141
- locked = true;
683
+ if (f) {
684
+ loff_t size = i_size_read(file_inode(f));
1142685
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;
686
+ error = vfs_truncate(&f->f_path, size);
687
+ if (error)
688
+ /* No useful action on failure */
689
+ pr_warn("incfs: Failed to truncate complete file: %d\n",
690
+ error);
1233691 }
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;
1255692 }
1256693
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)
694
+ /* This is best effort - there is no useful action to take on failure */
695
+ file_id_str = file_id_to_str(df->df_id);
696
+ if (!file_id_str)
1269697 goto out;
1270698
1271
-delete_index_file:
1272
- incfs_unlink(index_file_dentry);
699
+ incomplete_file_dentry = incfs_lookup_dentry(
700
+ df->df_mount_info->mi_incomplete_dir,
701
+ file_id_str);
702
+ if (!incomplete_file_dentry || IS_ERR(incomplete_file_dentry)) {
703
+ incomplete_file_dentry = NULL;
704
+ goto out;
705
+ }
706
+
707
+ if (!d_really_is_positive(incomplete_file_dentry))
708
+ goto out;
709
+
710
+ vfs_fsync(df->df_backing_file_context->bc_file, 0);
711
+ error = incfs_unlink(incomplete_file_dentry);
712
+ if (error) {
713
+ pr_warn("incfs: Deleting incomplete file failed: %d\n", error);
714
+ goto out;
715
+ }
716
+
717
+ notify_unlink(f->f_path.dentry, file_id_str, INCFS_INCOMPLETE_NAME);
1273718
1274719 out:
1275
- if (error)
1276
- pr_debug("incfs: %s err:%d\n", __func__, error);
1277
-
720
+ dput(incomplete_file_dentry);
1278721 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;
722
+ revert_creds(old_cred);
1287723 }
1288724
1289725 static long ioctl_fill_blocks(struct file *f, void __user *arg)
....@@ -1292,6 +728,7 @@
1292728 struct incfs_fill_blocks fill_blocks;
1293729 struct incfs_fill_block __user *usr_fill_block_array;
1294730 struct data_file *df = get_incfs_data_file(f);
731
+ struct incfs_file_data *fd = f->private_data;
1295732 const ssize_t data_buf_size = 2 * INCFS_DATA_FILE_BLOCK_SIZE;
1296733 u8 *data_buf = NULL;
1297734 ssize_t error = 0;
....@@ -1300,7 +737,7 @@
1300737 if (!df)
1301738 return -EBADF;
1302739
1303
- if ((uintptr_t)f->private_data != CAN_FILL)
740
+ if (!fd || fd->fd_fill_permission != CAN_FILL)
1304741 return -EPERM;
1305742
1306743 if (copy_from_user(&fill_blocks, usr_fill_blocks, sizeof(fill_blocks)))
....@@ -1346,6 +783,8 @@
1346783 if (data_buf)
1347784 free_pages((unsigned long)data_buf, get_order(data_buf_size));
1348785
786
+ maybe_delete_incomplete_file(f, df);
787
+
1349788 /*
1350789 * Only report the error if no records were processed, otherwise
1351790 * just return how many were processed successfully.
....@@ -1354,53 +793,6 @@
1354793 return error;
1355794
1356795 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;
1404796 }
1405797
1406798 static long ioctl_read_file_signature(struct file *f, void __user *arg)
....@@ -1456,18 +848,19 @@
1456848 struct incfs_get_filled_blocks_args __user *args_usr_ptr = arg;
1457849 struct incfs_get_filled_blocks_args args = {};
1458850 struct data_file *df = get_incfs_data_file(f);
851
+ struct incfs_file_data *fd = f->private_data;
1459852 int error;
1460853
1461
- if (!df)
854
+ if (!df || !fd)
1462855 return -EINVAL;
1463856
1464
- if ((uintptr_t)f->private_data != CAN_FILL)
857
+ if (fd->fd_fill_permission != CAN_FILL)
1465858 return -EPERM;
1466859
1467860 if (copy_from_user(&args, args_usr_ptr, sizeof(args)) > 0)
1468861 return -EINVAL;
1469862
1470
- error = incfs_get_filled_blocks(df, &args);
863
+ error = incfs_get_filled_blocks(df, fd, &args);
1471864
1472865 if (copy_to_user(args_usr_ptr, &args, sizeof(args)))
1473866 return -EFAULT;
....@@ -1475,25 +868,80 @@
1475868 return error;
1476869 }
1477870
871
+static long ioctl_get_block_count(struct file *f, void __user *arg)
872
+{
873
+ struct incfs_get_block_count_args __user *args_usr_ptr = arg;
874
+ struct incfs_get_block_count_args args = {};
875
+ struct data_file *df = get_incfs_data_file(f);
876
+
877
+ if (!df)
878
+ return -EINVAL;
879
+
880
+ args.total_data_blocks_out = df->df_data_block_count;
881
+ args.filled_data_blocks_out = atomic_read(&df->df_data_blocks_written);
882
+ args.total_hash_blocks_out = df->df_total_block_count -
883
+ df->df_data_block_count;
884
+ args.filled_hash_blocks_out = atomic_read(&df->df_hash_blocks_written);
885
+
886
+ if (copy_to_user(args_usr_ptr, &args, sizeof(args)))
887
+ return -EFAULT;
888
+
889
+ return 0;
890
+}
891
+
892
+static int incfs_ioctl_get_flags(struct file *f, void __user *arg)
893
+{
894
+ u32 flags = IS_VERITY(file_inode(f)) ? FS_VERITY_FL : 0;
895
+
896
+ return put_user(flags, (int __user *) arg);
897
+}
898
+
1478899 static long dispatch_ioctl(struct file *f, unsigned int req, unsigned long arg)
1479900 {
1480
- struct mount_info *mi = get_mount_info(file_superblock(f));
1481
-
1482901 switch (req) {
1483
- case INCFS_IOC_CREATE_FILE:
1484
- return ioctl_create_file(mi, (void __user *)arg);
1485902 case INCFS_IOC_FILL_BLOCKS:
1486903 return ioctl_fill_blocks(f, (void __user *)arg);
1487
- case INCFS_IOC_PERMIT_FILL:
1488
- return ioctl_permit_fill(f, (void __user *)arg);
1489904 case INCFS_IOC_READ_FILE_SIGNATURE:
1490905 return ioctl_read_file_signature(f, (void __user *)arg);
1491906 case INCFS_IOC_GET_FILLED_BLOCKS:
1492907 return ioctl_get_filled_blocks(f, (void __user *)arg);
908
+ case INCFS_IOC_GET_BLOCK_COUNT:
909
+ return ioctl_get_block_count(f, (void __user *)arg);
910
+ case FS_IOC_ENABLE_VERITY:
911
+ return incfs_ioctl_enable_verity(f, (const void __user *)arg);
912
+ case FS_IOC_GETFLAGS:
913
+ return incfs_ioctl_get_flags(f, (void __user *) arg);
914
+ case FS_IOC_MEASURE_VERITY:
915
+ return incfs_ioctl_measure_verity(f, (void __user *)arg);
916
+ case FS_IOC_READ_VERITY_METADATA:
917
+ return incfs_ioctl_read_verity_metadata(f, (void __user *)arg);
1493918 default:
1494919 return -EINVAL;
1495920 }
1496921 }
922
+
923
+#ifdef CONFIG_COMPAT
924
+static long incfs_compat_ioctl(struct file *file, unsigned int cmd,
925
+ unsigned long arg)
926
+{
927
+ switch (cmd) {
928
+ case FS_IOC32_GETFLAGS:
929
+ cmd = FS_IOC_GETFLAGS;
930
+ break;
931
+ case INCFS_IOC_FILL_BLOCKS:
932
+ case INCFS_IOC_READ_FILE_SIGNATURE:
933
+ case INCFS_IOC_GET_FILLED_BLOCKS:
934
+ case INCFS_IOC_GET_BLOCK_COUNT:
935
+ case FS_IOC_ENABLE_VERITY:
936
+ case FS_IOC_MEASURE_VERITY:
937
+ case FS_IOC_READ_VERITY_METADATA:
938
+ break;
939
+ default:
940
+ return -ENOIOCTLCMD;
941
+ }
942
+ return dispatch_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
943
+}
944
+#endif
1497945
1498946 static struct dentry *dir_lookup(struct inode *dir_inode, struct dentry *dentry,
1499947 unsigned int flags)
....@@ -1503,40 +951,18 @@
1503951 struct dentry *backing_dentry = NULL;
1504952 struct path dir_backing_path = {};
1505953 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);
1508954 int err = 0;
955
+
956
+ if (!mi || !dir_info || !dir_info->n_backing_inode)
957
+ return ERR_PTR(-EBADF);
1509958
1510959 if (d_inode(mi->mi_backing_dir_path.dentry) ==
1511960 dir_info->n_backing_inode) {
1512961 /* 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);
962
+ err = dir_lookup_pseudo_files(dir_inode->i_sb, dentry);
963
+ if (err != -ENOENT)
1525964 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
- }
965
+ err = 0;
1540966 }
1541967
1542968 dir_dentry = dget_parent(dentry);
....@@ -1622,7 +1048,7 @@
16221048
16231049 if (!backing_dentry) {
16241050 err = -EBADF;
1625
- goto out;
1051
+ goto path_err;
16261052 }
16271053
16281054 if (backing_dentry->d_parent == mi->mi_index_dir) {
....@@ -1631,13 +1057,19 @@
16311057 goto out;
16321058 }
16331059
1060
+ if (backing_dentry->d_parent == mi->mi_incomplete_dir) {
1061
+ /* Can't create a subdir inside .incomplete */
1062
+ err = -EBUSY;
1063
+ goto out;
1064
+ }
16341065 inode_lock_nested(dir_node->n_backing_inode, I_MUTEX_PARENT);
16351066 err = vfs_mkdir(dir_node->n_backing_inode, backing_dentry, mode | 0222);
16361067 inode_unlock(dir_node->n_backing_inode);
16371068 if (!err) {
16381069 struct inode *inode = NULL;
16391070
1640
- if (d_really_is_negative(backing_dentry)) {
1071
+ if (d_really_is_negative(backing_dentry) ||
1072
+ unlikely(d_unhashed(backing_dentry))) {
16411073 err = -EINVAL;
16421074 goto out;
16431075 }
....@@ -1654,23 +1086,33 @@
16541086 if (d_really_is_negative(dentry))
16551087 d_drop(dentry);
16561088 path_put(&backing_path);
1089
+
1090
+path_err:
16571091 mutex_unlock(&mi->mi_dir_struct_mutex);
16581092 if (err)
16591093 pr_debug("incfs: %s err:%d\n", __func__, err);
16601094 return err;
16611095 }
16621096
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)
1097
+/*
1098
+ * Delete file referenced by backing_dentry and if appropriate its hardlink
1099
+ * from .index and .incomplete
1100
+ */
1101
+static int file_delete(struct mount_info *mi, struct dentry *dentry,
1102
+ struct dentry *backing_dentry, int nlink)
16661103 {
16671104 struct dentry *index_file_dentry = NULL;
1105
+ struct dentry *incomplete_file_dentry = NULL;
16681106 /* 2 chars per byte of file ID + 1 char for \0 */
16691107 char file_id_str[2 * sizeof(incfs_uuid_t) + 1] = {0};
16701108 ssize_t uuid_size = 0;
16711109 int error = 0;
16721110
16731111 WARN_ON(!mutex_is_locked(&mi->mi_dir_struct_mutex));
1112
+
1113
+ if (nlink > 3)
1114
+ goto just_unlink;
1115
+
16741116 uuid_size = vfs_getxattr(backing_dentry, INCFS_XATTR_ID_NAME,
16751117 file_id_str, 2 * sizeof(incfs_uuid_t));
16761118 if (uuid_size < 0) {
....@@ -1686,17 +1128,50 @@
16861128 index_file_dentry = incfs_lookup_dentry(mi->mi_index_dir, file_id_str);
16871129 if (IS_ERR(index_file_dentry)) {
16881130 error = PTR_ERR(index_file_dentry);
1131
+ index_file_dentry = NULL;
16891132 goto out;
16901133 }
16911134
1692
- error = incfs_unlink(backing_dentry);
1693
- if (error)
1694
- goto out;
1135
+ if (d_really_is_positive(index_file_dentry) && nlink > 0)
1136
+ nlink--;
16951137
1696
- if (d_really_is_positive(index_file_dentry))
1138
+ if (nlink > 2)
1139
+ goto just_unlink;
1140
+
1141
+ incomplete_file_dentry = incfs_lookup_dentry(mi->mi_incomplete_dir,
1142
+ file_id_str);
1143
+ if (IS_ERR(incomplete_file_dentry)) {
1144
+ error = PTR_ERR(incomplete_file_dentry);
1145
+ incomplete_file_dentry = NULL;
1146
+ goto out;
1147
+ }
1148
+
1149
+ if (d_really_is_positive(incomplete_file_dentry) && nlink > 0)
1150
+ nlink--;
1151
+
1152
+ if (nlink > 1)
1153
+ goto just_unlink;
1154
+
1155
+ if (d_really_is_positive(index_file_dentry)) {
16971156 error = incfs_unlink(index_file_dentry);
1157
+ if (error)
1158
+ goto out;
1159
+ notify_unlink(dentry, file_id_str, INCFS_INDEX_NAME);
1160
+ }
1161
+
1162
+ if (d_really_is_positive(incomplete_file_dentry)) {
1163
+ error = incfs_unlink(incomplete_file_dentry);
1164
+ if (error)
1165
+ goto out;
1166
+ notify_unlink(dentry, file_id_str, INCFS_INCOMPLETE_NAME);
1167
+ }
1168
+
1169
+just_unlink:
1170
+ error = incfs_unlink(backing_dentry);
1171
+
16981172 out:
16991173 dput(index_file_dentry);
1174
+ dput(incomplete_file_dentry);
17001175 if (error)
17011176 pr_debug("incfs: delete_file_from_index err:%d\n", error);
17021177 return error;
....@@ -1709,6 +1184,9 @@
17091184 struct kstat stat;
17101185 int err = 0;
17111186
1187
+ if (!mi)
1188
+ return -EBADF;
1189
+
17121190 err = mutex_lock_interruptible(&mi->mi_dir_struct_mutex);
17131191 if (err)
17141192 return err;
....@@ -1716,11 +1194,17 @@
17161194 get_incfs_backing_path(dentry, &backing_path);
17171195 if (!backing_path.dentry) {
17181196 err = -EBADF;
1719
- goto out;
1197
+ goto path_err;
17201198 }
17211199
17221200 if (backing_path.dentry->d_parent == mi->mi_index_dir) {
17231201 /* Direct unlink from .index are not allowed. */
1202
+ err = -EBUSY;
1203
+ goto out;
1204
+ }
1205
+
1206
+ if (backing_path.dentry->d_parent == mi->mi_incomplete_dir) {
1207
+ /* Direct unlink from .incomplete are not allowed. */
17241208 err = -EBUSY;
17251209 goto out;
17261210 }
....@@ -1730,20 +1214,12 @@
17301214 if (err)
17311215 goto out;
17321216
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
- }
1217
+ err = file_delete(mi, dentry, backing_path.dentry, stat.nlink);
17431218
17441219 d_drop(dentry);
17451220 out:
17461221 path_put(&backing_path);
1222
+path_err:
17471223 if (err)
17481224 pr_debug("incfs: %s err:%d\n", __func__, err);
17491225 mutex_unlock(&mi->mi_dir_struct_mutex);
....@@ -1758,6 +1234,9 @@
17581234 struct path backing_new_path = {};
17591235 int error = 0;
17601236
1237
+ if (!mi)
1238
+ return -EBADF;
1239
+
17611240 error = mutex_lock_interruptible(&mi->mi_dir_struct_mutex);
17621241 if (error)
17631242 return error;
....@@ -1767,6 +1246,12 @@
17671246
17681247 if (backing_new_path.dentry->d_parent == mi->mi_index_dir) {
17691248 /* Can't link to .index */
1249
+ error = -EBUSY;
1250
+ goto out;
1251
+ }
1252
+
1253
+ if (backing_new_path.dentry->d_parent == mi->mi_incomplete_dir) {
1254
+ /* Can't link to .incomplete */
17701255 error = -EBUSY;
17711256 goto out;
17721257 }
....@@ -1804,6 +1289,9 @@
18041289 struct path backing_path = {};
18051290 int err = 0;
18061291
1292
+ if (!mi)
1293
+ return -EBADF;
1294
+
18071295 err = mutex_lock_interruptible(&mi->mi_dir_struct_mutex);
18081296 if (err)
18091297 return err;
....@@ -1811,11 +1299,17 @@
18111299 get_incfs_backing_path(dentry, &backing_path);
18121300 if (!backing_path.dentry) {
18131301 err = -EBADF;
1814
- goto out;
1302
+ goto path_err;
18151303 }
18161304
18171305 if (backing_path.dentry == mi->mi_index_dir) {
18181306 /* Can't delete .index */
1307
+ err = -EBUSY;
1308
+ goto out;
1309
+ }
1310
+
1311
+ if (backing_path.dentry == mi->mi_incomplete_dir) {
1312
+ /* Can't delete .incomplete */
18191313 err = -EBUSY;
18201314 goto out;
18211315 }
....@@ -1825,6 +1319,8 @@
18251319 d_drop(dentry);
18261320 out:
18271321 path_put(&backing_path);
1322
+
1323
+path_err:
18281324 if (err)
18291325 pr_debug("incfs: %s err:%d\n", __func__, err);
18301326 mutex_unlock(&mi->mi_dir_struct_mutex);
....@@ -1843,14 +1339,19 @@
18431339 struct dentry *trap;
18441340 int error = 0;
18451341
1846
- if (!mi)
1847
- return -EBADF;
1848
-
18491342 error = mutex_lock_interruptible(&mi->mi_dir_struct_mutex);
18501343 if (error)
18511344 return error;
18521345
18531346 backing_old_dentry = get_incfs_dentry(old_dentry)->backing_path.dentry;
1347
+
1348
+ if (!backing_old_dentry || backing_old_dentry == mi->mi_index_dir ||
1349
+ backing_old_dentry == mi->mi_incomplete_dir) {
1350
+ /* Renaming .index or .incomplete not allowed */
1351
+ error = -EBUSY;
1352
+ goto exit;
1353
+ }
1354
+
18541355 backing_new_dentry = get_incfs_dentry(new_dentry)->backing_path.dentry;
18551356 dget(backing_old_dentry);
18561357 dget(backing_new_dentry);
....@@ -1859,8 +1360,9 @@
18591360 backing_new_dir_dentry = dget_parent(backing_new_dentry);
18601361 target_inode = d_inode(new_dentry);
18611362
1862
- if (backing_old_dir_dentry == mi->mi_index_dir) {
1863
- /* Direct moves from .index are not allowed. */
1363
+ if (backing_old_dir_dentry == mi->mi_index_dir ||
1364
+ backing_old_dir_dentry == mi->mi_incomplete_dir) {
1365
+ /* Direct moves from .index or .incomplete are not allowed. */
18641366 error = -EBUSY;
18651367 goto out;
18661368 }
....@@ -1897,6 +1399,7 @@
18971399 dput(backing_new_dentry);
18981400 dput(backing_old_dentry);
18991401
1402
+exit:
19001403 mutex_unlock(&mi->mi_dir_struct_mutex);
19011404 if (error)
19021405 pr_debug("incfs: %s err:%d\n", __func__, error);
....@@ -1910,12 +1413,21 @@
19101413 struct file *backing_file = NULL;
19111414 struct path backing_path = {};
19121415 int err = 0;
1416
+ int flags = O_NOATIME | O_LARGEFILE |
1417
+ (S_ISDIR(inode->i_mode) ? O_RDONLY : O_RDWR);
19131418 const struct cred *old_cred;
19141419
1420
+ WARN_ON(file->private_data);
1421
+
1422
+ if (!mi)
1423
+ return -EBADF;
1424
+
19151425 get_incfs_backing_path(file->f_path.dentry, &backing_path);
1426
+ if (!backing_path.dentry)
1427
+ return -EBADF;
1428
+
19161429 old_cred = override_creds(mi->mi_owner);
1917
- backing_file = dentry_open(&backing_path,
1918
- O_RDWR | O_NOATIME | O_LARGEFILE, current_cred());
1430
+ backing_file = dentry_open(&backing_path, flags, current_cred());
19191431 revert_creds(old_cred);
19201432 path_put(&backing_path);
19211433
....@@ -1926,8 +1438,25 @@
19261438 }
19271439
19281440 if (S_ISREG(inode->i_mode)) {
1441
+ struct incfs_file_data *fd = kzalloc(sizeof(*fd), GFP_NOFS);
1442
+
1443
+ if (!fd) {
1444
+ err = -ENOMEM;
1445
+ goto out;
1446
+ }
1447
+
1448
+ *fd = (struct incfs_file_data) {
1449
+ .fd_fill_permission = CANT_FILL,
1450
+ };
1451
+ file->private_data = fd;
1452
+
19291453 err = make_inode_ready_for_data_ops(mi, inode, backing_file);
1930
- file->private_data = (void *)CANT_FILL;
1454
+ if (err)
1455
+ goto out;
1456
+
1457
+ err = incfs_fsverity_file_open(inode, file);
1458
+ if (err)
1459
+ goto out;
19311460 } else if (S_ISDIR(inode->i_mode)) {
19321461 struct dir_file *dir = NULL;
19331462
....@@ -1940,9 +1469,17 @@
19401469 err = -EBADF;
19411470
19421471 out:
1943
- if (err)
1944
- pr_debug("incfs: %s name:%s err: %d\n", __func__,
1945
- file->f_path.dentry->d_name.name, err);
1472
+ if (err) {
1473
+ pr_debug("name:%s err: %d\n",
1474
+ file->f_path.dentry->d_name.name, err);
1475
+ if (S_ISREG(inode->i_mode))
1476
+ kfree(file->private_data);
1477
+ else if (S_ISDIR(inode->i_mode))
1478
+ incfs_free_dir_file(file->private_data);
1479
+
1480
+ file->private_data = NULL;
1481
+ }
1482
+
19461483 if (backing_file)
19471484 fput(backing_file);
19481485 return err;
....@@ -1951,9 +1488,8 @@
19511488 static int file_release(struct inode *inode, struct file *file)
19521489 {
19531490 if (S_ISREG(inode->i_mode)) {
1954
- /* Do nothing.
1955
- * data_file is released only by inode eviction.
1956
- */
1491
+ kfree(file->private_data);
1492
+ file->private_data = NULL;
19571493 } else if (S_ISDIR(inode->i_mode)) {
19581494 struct dir_file *dir = get_incfs_dir_file(file);
19591495
....@@ -2056,6 +1592,10 @@
20561592 if (ia->ia_valid & ATTR_SIZE)
20571593 return -EINVAL;
20581594
1595
+ if ((ia->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
1596
+ (ia->ia_valid & ATTR_MODE))
1597
+ return -EINVAL;
1598
+
20591599 if (!di)
20601600 return -EINVAL;
20611601 backing_dentry = di->backing_path.dentry;
....@@ -2085,6 +1625,43 @@
20851625 return simple_setattr(dentry, ia);
20861626 }
20871627
1628
+
1629
+static int incfs_getattr(const struct path *path,
1630
+ struct kstat *stat, u32 request_mask,
1631
+ unsigned int query_flags)
1632
+{
1633
+ struct inode *inode = d_inode(path->dentry);
1634
+
1635
+ generic_fillattr(inode, stat);
1636
+
1637
+ if (inode->i_ino < INCFS_START_INO_RANGE)
1638
+ return 0;
1639
+
1640
+ stat->attributes &= ~STATX_ATTR_VERITY;
1641
+ if (IS_VERITY(inode))
1642
+ stat->attributes |= STATX_ATTR_VERITY;
1643
+ stat->attributes_mask |= STATX_ATTR_VERITY;
1644
+
1645
+ if (request_mask & STATX_BLOCKS) {
1646
+ struct kstat backing_kstat;
1647
+ struct dentry_info *di = get_incfs_dentry(path->dentry);
1648
+ int error = 0;
1649
+ struct path *backing_path;
1650
+
1651
+ if (!di)
1652
+ return -EFSCORRUPTED;
1653
+ backing_path = &di->backing_path;
1654
+ error = vfs_getattr(backing_path, &backing_kstat, STATX_BLOCKS,
1655
+ AT_STATX_SYNC_AS_STAT);
1656
+ if (error)
1657
+ return error;
1658
+
1659
+ stat->blocks = backing_kstat.blocks;
1660
+ }
1661
+
1662
+ return 0;
1663
+}
1664
+
20881665 static ssize_t incfs_getxattr(struct dentry *d, const char *name,
20891666 void *value, size_t size)
20901667 {
....@@ -2092,9 +1669,7 @@
20921669 struct mount_info *mi = get_mount_info(d->d_sb);
20931670 char *stored_value;
20941671 size_t stored_size;
2095
-
2096
- if (!mi)
2097
- return -EBADF;
1672
+ int i;
20981673
20991674 if (di && di->backing_path.dentry)
21001675 return vfs_getxattr(di->backing_path.dentry, name, value, size);
....@@ -2102,16 +1677,14 @@
21021677 if (strcmp(name, "security.selinux"))
21031678 return -ENODATA;
21041679
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 {
1680
+ for (i = 0; i < PSEUDO_FILE_COUNT; ++i)
1681
+ if (!strcmp(d->d_iname, incfs_pseudo_file_names[i].data))
1682
+ break;
1683
+ if (i == PSEUDO_FILE_COUNT)
21121684 return -ENODATA;
2113
- }
21141685
1686
+ stored_value = mi->pseudo_file_xattr[i].data;
1687
+ stored_size = mi->pseudo_file_xattr[i].len;
21151688 if (!stored_value)
21161689 return -ENODATA;
21171690
....@@ -2120,7 +1693,6 @@
21201693
21211694 memcpy(value, stored_value, stored_size);
21221695 return stored_size;
2123
-
21241696 }
21251697
21261698
....@@ -2129,11 +1701,9 @@
21291701 {
21301702 struct dentry_info *di = get_incfs_dentry(d);
21311703 struct mount_info *mi = get_mount_info(d->d_sb);
2132
- void **stored_value;
1704
+ u8 **stored_value;
21331705 size_t *stored_size;
2134
-
2135
- if (!mi)
2136
- return -EBADF;
1706
+ int i;
21371707
21381708 if (di && di->backing_path.dentry)
21391709 return vfs_setxattr(di->backing_path.dentry, name, value, size,
....@@ -2145,16 +1715,14 @@
21451715 if (size > INCFS_MAX_FILE_ATTR_SIZE)
21461716 return -E2BIG;
21471717
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 {
1718
+ for (i = 0; i < PSEUDO_FILE_COUNT; ++i)
1719
+ if (!strcmp(d->d_iname, incfs_pseudo_file_names[i].data))
1720
+ break;
1721
+ if (i == PSEUDO_FILE_COUNT)
21551722 return -ENODATA;
2156
- }
21571723
1724
+ stored_value = &mi->pseudo_file_xattr[i].data;
1725
+ stored_size = &mi->pseudo_file_xattr[i].len;
21581726 kfree (*stored_value);
21591727 *stored_value = kzalloc(size, GFP_NOFS);
21601728 if (!*stored_value)
....@@ -2175,22 +1743,18 @@
21751743 return vfs_listxattr(di->backing_path.dentry, list, size);
21761744 }
21771745
2178
-static int incfs_test_super(struct super_block *s, void *p)
2179
-{
2180
- return s->s_fs_info != NULL;
2181
-}
2182
-
21831746 struct dentry *incfs_mount_fs(struct file_system_type *type, int flags,
21841747 const char *dev_name, void *data)
21851748 {
21861749 struct mount_options options = {};
21871750 struct mount_info *mi = NULL;
21881751 struct path backing_dir_path = {};
2189
- struct dentry *index_dir;
1752
+ struct dentry *index_dir = NULL;
1753
+ struct dentry *incomplete_dir = NULL;
21901754 struct super_block *src_fs_sb = NULL;
21911755 struct inode *root_inode = NULL;
2192
- struct super_block *sb = sget(type, incfs_test_super, set_anon_super,
2193
- flags, NULL);
1756
+ struct super_block *sb = sget(type, NULL, set_anon_super, flags, NULL);
1757
+ bool dir_created = false;
21941758 int error = 0;
21951759
21961760 if (IS_ERR(sb))
....@@ -2199,7 +1763,7 @@
21991763 sb->s_op = &incfs_super_ops;
22001764 sb->s_d_op = &incfs_dentry_ops;
22011765 sb->s_flags |= S_NOATIME;
2202
- sb->s_magic = (long)INCFS_MAGIC_NUMBER;
1766
+ sb->s_magic = INCFS_MAGIC_NUMBER;
22031767 sb->s_time_gran = 1;
22041768 sb->s_blocksize = INCFS_DATA_FILE_BLOCK_SIZE;
22051769 sb->s_blocksize_bits = blksize_bits(sb->s_blocksize);
....@@ -2207,17 +1771,23 @@
22071771
22081772 BUILD_BUG_ON(PAGE_SIZE != INCFS_DATA_FILE_BLOCK_SIZE);
22091773
1774
+ if (!dev_name) {
1775
+ pr_err("incfs: Backing dir is not set, filesystem can't be mounted.\n");
1776
+ error = -ENOENT;
1777
+ goto err_deactivate;
1778
+ }
1779
+
22101780 error = parse_options(&options, (char *)data);
22111781 if (error != 0) {
22121782 pr_err("incfs: Options parsing error. %d\n", error);
2213
- goto err;
1783
+ goto err_deactivate;
22141784 }
22151785
22161786 sb->s_bdi->ra_pages = options.readahead_pages;
22171787 if (!dev_name) {
22181788 pr_err("incfs: Backing dir is not set, filesystem can't be mounted.\n");
22191789 error = -ENOENT;
2220
- goto err;
1790
+ goto err_free_opts;
22211791 }
22221792
22231793 error = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
....@@ -2226,62 +1796,80 @@
22261796 !d_really_is_positive(backing_dir_path.dentry)) {
22271797 pr_err("incfs: Error accessing: %s.\n",
22281798 dev_name);
2229
- goto err;
1799
+ goto err_free_opts;
22301800 }
22311801 src_fs_sb = backing_dir_path.dentry->d_sb;
22321802 sb->s_maxbytes = src_fs_sb->s_maxbytes;
1803
+ sb->s_stack_depth = src_fs_sb->s_stack_depth + 1;
22331804
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;
1805
+ if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1806
+ error = -EINVAL;
1807
+ goto err_put_path;
22471808 }
22481809
2249
- index_dir = open_or_create_index_dir(backing_dir_path.dentry);
1810
+ mi = incfs_alloc_mount_info(sb, &options, &backing_dir_path);
1811
+ if (IS_ERR_OR_NULL(mi)) {
1812
+ error = PTR_ERR(mi);
1813
+ pr_err("incfs: Error allocating mount info. %d\n", error);
1814
+ goto err_put_path;
1815
+ }
1816
+
1817
+ sb->s_fs_info = mi;
1818
+ mi->mi_backing_dir_path = backing_dir_path;
1819
+ index_dir = open_or_create_special_dir(backing_dir_path.dentry,
1820
+ INCFS_INDEX_NAME, &dir_created);
22501821 if (IS_ERR_OR_NULL(index_dir)) {
22511822 error = PTR_ERR(index_dir);
22521823 pr_err("incfs: Can't find or create .index dir in %s\n",
22531824 dev_name);
2254
- goto err;
1825
+ /* No need to null index_dir since we don't put it */
1826
+ goto err_put_path;
22551827 }
1828
+
22561829 mi->mi_index_dir = index_dir;
1830
+ mi->mi_index_free = dir_created;
1831
+
1832
+ incomplete_dir = open_or_create_special_dir(backing_dir_path.dentry,
1833
+ INCFS_INCOMPLETE_NAME,
1834
+ &dir_created);
1835
+ if (IS_ERR_OR_NULL(incomplete_dir)) {
1836
+ error = PTR_ERR(incomplete_dir);
1837
+ pr_err("incfs: Can't find or create .incomplete dir in %s\n",
1838
+ dev_name);
1839
+ /* No need to null incomplete_dir since we don't put it */
1840
+ goto err_put_path;
1841
+ }
1842
+ mi->mi_incomplete_dir = incomplete_dir;
1843
+ mi->mi_incomplete_free = dir_created;
22571844
22581845 root_inode = fetch_regular_inode(sb, backing_dir_path.dentry);
22591846 if (IS_ERR(root_inode)) {
22601847 error = PTR_ERR(root_inode);
2261
- goto err;
1848
+ goto err_put_path;
22621849 }
22631850
1851
+ sb->s_root = d_make_root(root_inode);
22641852 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;
1853
+ error = -ENOMEM;
1854
+ goto err_put_path;
22731855 }
1856
+ error = incfs_init_dentry(sb->s_root, &backing_dir_path);
1857
+ if (error)
1858
+ goto err_put_path;
22741859
2275
- mi->mi_backing_dir_path = backing_dir_path;
1860
+ path_put(&backing_dir_path);
22761861 sb->s_flags |= SB_ACTIVE;
22771862
22781863 pr_debug("incfs: mount\n");
22791864 return dget(sb->s_root);
2280
-err:
2281
- sb->s_fs_info = NULL;
1865
+
1866
+err_put_path:
22821867 path_put(&backing_dir_path);
2283
- incfs_free_mount_info(mi);
1868
+err_free_opts:
1869
+ free_options(&options);
1870
+err_deactivate:
22841871 deactivate_locked_super(sb);
1872
+ pr_err("incfs: mount failed %d\n", error);
22851873 return ERR_PTR(error);
22861874 }
22871875
....@@ -2291,39 +1879,56 @@
22911879 struct mount_info *mi = get_mount_info(sb);
22921880 int err = 0;
22931881
2294
- if (!mi)
2295
- return err;
2296
-
22971882 sync_filesystem(sb);
22981883 err = parse_options(&options, (char *)data);
22991884 if (err)
23001885 return err;
23011886
1887
+ if (options.report_uid != mi->mi_options.report_uid) {
1888
+ pr_err("incfs: Can't change report_uid mount option on remount\n");
1889
+ err = -EOPNOTSUPP;
1890
+ goto out;
1891
+ }
1892
+
23021893 err = incfs_realloc_mount_info(mi, &options);
23031894 if (err)
2304
- return err;
1895
+ goto out;
23051896
23061897 pr_debug("incfs: remount\n");
2307
- return 0;
1898
+
1899
+out:
1900
+ free_options(&options);
1901
+ return err;
23081902 }
23091903
23101904 void incfs_kill_sb(struct super_block *sb)
23111905 {
23121906 struct mount_info *mi = sb->s_fs_info;
1907
+ struct inode *dinode = NULL;
23131908
23141909 pr_debug("incfs: unmount\n");
2315
- vfs_rmdir(d_inode(mi->mi_backing_dir_path.dentry), mi->mi_index_dir);
1910
+
1911
+ if (mi) {
1912
+ if (mi->mi_backing_dir_path.dentry)
1913
+ dinode = d_inode(mi->mi_backing_dir_path.dentry);
1914
+
1915
+ if (dinode) {
1916
+ if (mi->mi_index_dir && mi->mi_index_free)
1917
+ vfs_rmdir(dinode, mi->mi_index_dir);
1918
+
1919
+ if (mi->mi_incomplete_dir && mi->mi_incomplete_free)
1920
+ vfs_rmdir(dinode, mi->mi_incomplete_dir);
1921
+ }
1922
+
1923
+ incfs_free_mount_info(mi);
1924
+ sb->s_fs_info = NULL;
1925
+ }
23161926 kill_anon_super(sb);
2317
- incfs_free_mount_info(mi);
2318
- sb->s_fs_info = NULL;
23191927 }
23201928
23211929 static int show_options(struct seq_file *m, struct dentry *root)
23221930 {
23231931 struct mount_info *mi = get_mount_info(root->d_sb);
2324
-
2325
- if (!mi)
2326
- return -EBADF;
23271932
23281933 seq_printf(m, ",read_timeout_ms=%u", mi->mi_options.read_timeout_ms);
23291934 seq_printf(m, ",readahead=%u", mi->mi_options.readahead_pages);
....@@ -2332,9 +1937,11 @@
23321937 seq_printf(m, ",rlog_wakeup_cnt=%u",
23331938 mi->mi_options.read_log_wakeup_count);
23341939 }
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");
1940
+ if (mi->mi_options.report_uid)
1941
+ seq_puts(m, ",report_uid");
1942
+
1943
+ if (mi->mi_sysfs_node)
1944
+ seq_printf(m, ",sysfs_name=%s",
1945
+ kobject_name(&mi->mi_sysfs_node->isn_sysfs_node));
23391946 return 0;
23401947 }