1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright 2018 Google LLC
4 */
5
6 #include <linux/blkdev.h>
7 #include <linux/cred.h>
8 #include <linux/eventpoll.h>
9 #include <linux/file.h>
10 #include <linux/fs.h>
11 #include <linux/fs_stack.h>
12 #include <linux/namei.h>
13 #include <linux/parser.h>
14 #include <linux/poll.h>
15 #include <linux/seq_file.h>
16 #include <linux/syscalls.h>
17 #include <linux/xattr.h>
18
19 #include <uapi/linux/incrementalfs.h>
20
21 #include "vfs.h"
22 #include "data_mgmt.h"
23 #include "format.h"
24 #include "integrity.h"
25 #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
33
34 static int incfs_remount_fs(struct super_block *sb, int *flags, char *data);
35
36 static int dentry_revalidate(struct dentry *dentry, unsigned int flags);
37 static void dentry_release(struct dentry *d);
38
39 static int iterate_incfs_dir(struct file *file, struct dir_context *ctx);
40 static struct dentry *dir_lookup(struct inode *dir_inode,
41 struct dentry *dentry, unsigned int flags);
42 static int dir_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
43 static int dir_unlink(struct inode *dir, struct dentry *dentry);
44 static int dir_link(struct dentry *old_dentry, struct inode *dir,
45 struct dentry *new_dentry);
46 static int dir_rmdir(struct inode *dir, struct dentry *dentry);
47 static int dir_rename(struct inode *old_dir, struct dentry *old_dentry,
48 struct inode *new_dir, struct dentry *new_dentry);
49
50 static int file_open(struct inode *inode, struct file *file);
51 static int file_release(struct inode *inode, struct file *file);
52 static ssize_t file_write(struct file *f, const char __user *buf,
53 size_t size, loff_t *offset);
54 static int read_single_page(struct file *f, struct page *page);
55 static long dispatch_ioctl(struct file *f, unsigned int req, unsigned long arg);
56
57 static ssize_t pending_reads_read(struct file *f, char __user *buf, size_t len,
58 loff_t *ppos);
59 static __poll_t pending_reads_poll(struct file *file, poll_table *wait);
60 static int pending_reads_open(struct inode *inode, struct file *file);
61 static int pending_reads_release(struct inode *, struct file *);
62
63 static ssize_t log_read(struct file *f, char __user *buf, size_t len,
64 loff_t *ppos);
65 static __poll_t log_poll(struct file *file, poll_table *wait);
66 static int log_open(struct inode *inode, struct file *file);
67 static int log_release(struct inode *, struct file *);
68
69 static struct inode *alloc_inode(struct super_block *sb);
70 static void free_inode(struct inode *inode);
71 static void evict_inode(struct inode *inode);
72
73 static ssize_t incfs_getxattr(struct dentry *d, const char *name,
74 void *value, size_t size);
75 static ssize_t incfs_listxattr(struct dentry *d, char *list, size_t size);
76
77 static int show_options(struct seq_file *, struct dentry *);
78
79 static const struct super_operations incfs_super_ops = {
80 .statfs = simple_statfs,
81 .remount_fs = incfs_remount_fs,
82 .alloc_inode = alloc_inode,
83 .destroy_inode = free_inode,
84 .evict_inode = evict_inode,
85 .show_options = show_options
86 };
87
dir_rename_wrap(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)88 static int dir_rename_wrap(struct inode *old_dir, struct dentry *old_dentry,
89 struct inode *new_dir, struct dentry *new_dentry,
90 unsigned int flags)
91 {
92 return dir_rename(old_dir, old_dentry, new_dir, new_dentry);
93 }
94
95 static const struct inode_operations incfs_dir_inode_ops = {
96 .lookup = dir_lookup,
97 .mkdir = dir_mkdir,
98 .rename = dir_rename_wrap,
99 .unlink = dir_unlink,
100 .link = dir_link,
101 .rmdir = dir_rmdir
102 };
103
104 static const struct file_operations incfs_dir_fops = {
105 .llseek = generic_file_llseek,
106 .read = generic_read_dir,
107 .iterate = iterate_incfs_dir,
108 .open = file_open,
109 .release = file_release,
110 .unlocked_ioctl = dispatch_ioctl,
111 .compat_ioctl = dispatch_ioctl
112 };
113
114 static const struct dentry_operations incfs_dentry_ops = {
115 .d_revalidate = dentry_revalidate,
116 .d_release = dentry_release
117 };
118
119 static const struct address_space_operations incfs_address_space_ops = {
120 .readpage = read_single_page,
121 /* .readpages = readpages */
122 };
123
124 static const struct file_operations incfs_file_ops = {
125 .open = file_open,
126 .release = file_release,
127 .write = file_write,
128 .read_iter = generic_file_read_iter,
129 .mmap = generic_file_mmap,
130 .splice_read = generic_file_splice_read,
131 .llseek = generic_file_llseek,
132 .unlocked_ioctl = dispatch_ioctl,
133 .compat_ioctl = dispatch_ioctl
134 };
135
136 static const struct file_operations incfs_pending_read_file_ops = {
137 .read = pending_reads_read,
138 .poll = pending_reads_poll,
139 .open = pending_reads_open,
140 .release = pending_reads_release,
141 .llseek = noop_llseek,
142 .unlocked_ioctl = dispatch_ioctl,
143 .compat_ioctl = dispatch_ioctl
144 };
145
146 static const struct file_operations incfs_log_file_ops = {
147 .read = log_read,
148 .poll = log_poll,
149 .open = log_open,
150 .release = log_release,
151 .llseek = noop_llseek,
152 .unlocked_ioctl = dispatch_ioctl,
153 .compat_ioctl = dispatch_ioctl
154 };
155
156 static const struct inode_operations incfs_file_inode_ops = {
157 .setattr = simple_setattr,
158 .getattr = simple_getattr,
159 .listxattr = incfs_listxattr
160 };
161
incfs_handler_getxattr(const struct xattr_handler * xh,struct dentry * d,struct inode * inode,const char * name,void * buffer,size_t size,int flags)162 static int incfs_handler_getxattr(const struct xattr_handler *xh,
163 struct dentry *d, struct inode *inode,
164 const char *name, void *buffer, size_t size,
165 int flags)
166 {
167 return incfs_getxattr(d, name, buffer, size);
168 }
169
170 static const struct xattr_handler incfs_xattr_handler = {
171 .prefix = "", /* AKA all attributes */
172 .get = incfs_handler_getxattr,
173 };
174
175 static const struct xattr_handler *incfs_xattr_ops[] = {
176 &incfs_xattr_handler,
177 NULL,
178 };
179
180 /* State of an open .pending_reads file, unique for each file descriptor. */
181 struct pending_reads_state {
182 /* A serial number of the last pending read obtained from this file. */
183 int last_pending_read_sn;
184 };
185
186 /* State of an open .log file, unique for each file descriptor. */
187 struct log_file_state {
188 struct read_log_state state;
189 };
190
191 struct inode_search {
192 unsigned long ino;
193
194 struct dentry *backing_dentry;
195 };
196
197 enum parse_parameter {
198 Opt_read_timeout,
199 Opt_readahead_pages,
200 Opt_no_backing_file_cache,
201 Opt_no_backing_file_readahead,
202 Opt_rlog_pages,
203 Opt_rlog_wakeup_cnt,
204 Opt_err
205 };
206
207 static const char pending_reads_file_name[] = INCFS_PENDING_READS_FILENAME;
208 static struct mem_range pending_reads_file_name_range = {
209 .data = (u8 *)pending_reads_file_name,
210 .len = ARRAY_SIZE(pending_reads_file_name) - 1
211 };
212
213 static const char log_file_name[] = INCFS_LOG_FILENAME;
214 static struct mem_range log_file_name_range = {
215 .data = (u8 *)log_file_name,
216 .len = ARRAY_SIZE(log_file_name) - 1
217 };
218
219 static const match_table_t option_tokens = {
220 { Opt_read_timeout, "read_timeout_ms=%u" },
221 { Opt_readahead_pages, "readahead=%u" },
222 { Opt_no_backing_file_cache, "no_bf_cache=%u" },
223 { Opt_no_backing_file_readahead, "no_bf_readahead=%u" },
224 { Opt_rlog_pages, "rlog_pages=%u" },
225 { Opt_rlog_wakeup_cnt, "rlog_wakeup_cnt=%u" },
226 { Opt_err, NULL }
227 };
228
parse_options(struct mount_options * opts,char * str)229 static int parse_options(struct mount_options *opts, char *str)
230 {
231 substring_t args[MAX_OPT_ARGS];
232 int value;
233 char *position;
234
235 if (opts == NULL)
236 return -EFAULT;
237
238 opts->read_timeout_ms = 1000; /* Default: 1s */
239 opts->readahead_pages = 10;
240 opts->read_log_pages = 2;
241 opts->read_log_wakeup_count = 10;
242 opts->no_backing_file_cache = false;
243 opts->no_backing_file_readahead = false;
244 if (str == NULL || *str == 0)
245 return 0;
246
247 while ((position = strsep(&str, ",")) != NULL) {
248 int token;
249
250 if (!*position)
251 continue;
252
253 token = match_token(position, option_tokens, args);
254
255 switch (token) {
256 case Opt_read_timeout:
257 if (match_int(&args[0], &value))
258 return -EINVAL;
259 opts->read_timeout_ms = value;
260 break;
261 case Opt_readahead_pages:
262 if (match_int(&args[0], &value))
263 return -EINVAL;
264 opts->readahead_pages = value;
265 break;
266 case Opt_no_backing_file_cache:
267 if (match_int(&args[0], &value))
268 return -EINVAL;
269 opts->no_backing_file_cache = (value != 0);
270 break;
271 case Opt_no_backing_file_readahead:
272 if (match_int(&args[0], &value))
273 return -EINVAL;
274 opts->no_backing_file_readahead = (value != 0);
275 break;
276 case Opt_rlog_pages:
277 if (match_int(&args[0], &value))
278 return -EINVAL;
279 opts->read_log_pages = value;
280 break;
281 case Opt_rlog_wakeup_cnt:
282 if (match_int(&args[0], &value))
283 return -EINVAL;
284 opts->read_log_wakeup_count = value;
285 break;
286 default:
287 return -EINVAL;
288 }
289 }
290
291 return 0;
292 }
293
file_superblock(struct file * f)294 static struct super_block *file_superblock(struct file *f)
295 {
296 struct inode *inode = file_inode(f);
297
298 return inode->i_sb;
299 }
300
get_mount_info(struct super_block * sb)301 static struct mount_info *get_mount_info(struct super_block *sb)
302 {
303 struct mount_info *result = sb->s_fs_info;
304
305 WARN_ON(!result);
306 return result;
307 }
308
309 /* Read file size from the attribute. Quicker than reading the header */
read_size_attr(struct dentry * backing_dentry)310 static u64 read_size_attr(struct dentry *backing_dentry)
311 {
312 __le64 attr_value;
313 ssize_t bytes_read;
314
315 bytes_read = vfs_getxattr(backing_dentry, INCFS_XATTR_SIZE_NAME,
316 (char *)&attr_value, sizeof(attr_value));
317
318 if (bytes_read != sizeof(attr_value))
319 return 0;
320
321 return le64_to_cpu(attr_value);
322 }
323
inode_test(struct inode * inode,void * opaque)324 static int inode_test(struct inode *inode, void *opaque)
325 {
326 struct inode_search *search = opaque;
327 struct inode_info *node = get_incfs_node(inode);
328
329 if (!node)
330 return 0;
331
332 if (search->backing_dentry) {
333 struct inode *backing_inode = d_inode(search->backing_dentry);
334
335 return (node->n_backing_inode == backing_inode) &&
336 inode->i_ino == search->ino;
337 }
338 return 1;
339 }
340
inode_set(struct inode * inode,void * opaque)341 static int inode_set(struct inode *inode, void *opaque)
342 {
343 struct inode_search *search = opaque;
344 struct inode_info *node = get_incfs_node(inode);
345
346 if (search->backing_dentry) {
347 /* It's a regular inode that has corresponding backing inode */
348 struct dentry *backing_dentry = search->backing_dentry;
349 struct inode *backing_inode = d_inode(backing_dentry);
350
351 inode_init_owner(inode, NULL, backing_inode->i_mode);
352 fsstack_copy_attr_all(inode, backing_inode);
353 if (S_ISREG(inode->i_mode)) {
354 u64 size = read_size_attr(backing_dentry);
355
356 inode->i_size = size;
357 inode->i_blocks = get_blocks_count_for_size(size);
358 inode->i_mapping->a_ops = &incfs_address_space_ops;
359 inode->i_op = &incfs_file_inode_ops;
360 inode->i_fop = &incfs_file_ops;
361 } else if (S_ISDIR(inode->i_mode)) {
362 inode->i_size = 0;
363 inode->i_blocks = 1;
364 inode->i_mapping->a_ops = &incfs_address_space_ops;
365 inode->i_op = &incfs_dir_inode_ops;
366 inode->i_fop = &incfs_dir_fops;
367 } else {
368 pr_warn_once("incfs: Unexpected inode type\n");
369 return -EBADF;
370 }
371
372 ihold(backing_inode);
373 node->n_backing_inode = backing_inode;
374 node->n_mount_info = get_mount_info(inode->i_sb);
375 inode->i_ctime = backing_inode->i_ctime;
376 inode->i_mtime = backing_inode->i_mtime;
377 inode->i_atime = backing_inode->i_atime;
378 inode->i_ino = backing_inode->i_ino;
379 if (backing_inode->i_ino < INCFS_START_INO_RANGE) {
380 pr_warn("incfs: ino conflict with backing FS %ld\n",
381 backing_inode->i_ino);
382 }
383 return 0;
384 } else if (search->ino == INCFS_PENDING_READS_INODE) {
385 /* It's an inode for .pending_reads pseudo file. */
386
387 inode->i_ctime = (struct timespec64){};
388 inode->i_mtime = inode->i_ctime;
389 inode->i_atime = inode->i_ctime;
390 inode->i_size = 0;
391 inode->i_ino = INCFS_PENDING_READS_INODE;
392 inode->i_private = NULL;
393
394 inode_init_owner(inode, NULL, S_IFREG | READ_WRITE_FILE_MODE);
395
396 inode->i_op = &incfs_file_inode_ops;
397 inode->i_fop = &incfs_pending_read_file_ops;
398
399 } else if (search->ino == INCFS_LOG_INODE) {
400 /* It's an inode for .log pseudo file. */
401
402 inode->i_ctime = (struct timespec64){};
403 inode->i_mtime = inode->i_ctime;
404 inode->i_atime = inode->i_ctime;
405 inode->i_size = 0;
406 inode->i_ino = INCFS_LOG_INODE;
407 inode->i_private = NULL;
408
409 inode_init_owner(inode, NULL, S_IFREG | READ_WRITE_FILE_MODE);
410
411 inode->i_op = &incfs_file_inode_ops;
412 inode->i_fop = &incfs_log_file_ops;
413
414 } else {
415 /* Unknown inode requested. */
416 return -EINVAL;
417 }
418
419 return 0;
420 }
421
fetch_regular_inode(struct super_block * sb,struct dentry * backing_dentry)422 static struct inode *fetch_regular_inode(struct super_block *sb,
423 struct dentry *backing_dentry)
424 {
425 struct inode *backing_inode = d_inode(backing_dentry);
426 struct inode_search search = {
427 .ino = backing_inode->i_ino,
428 .backing_dentry = backing_dentry
429 };
430 struct inode *inode = iget5_locked(sb, search.ino, inode_test,
431 inode_set, &search);
432
433 if (!inode)
434 return ERR_PTR(-ENOMEM);
435
436 if (inode->i_state & I_NEW)
437 unlock_new_inode(inode);
438
439 return inode;
440 }
441
pending_reads_read(struct file * f,char __user * buf,size_t len,loff_t * ppos)442 static ssize_t pending_reads_read(struct file *f, char __user *buf, size_t len,
443 loff_t *ppos)
444 {
445 struct pending_reads_state *pr_state = f->private_data;
446 struct mount_info *mi = get_mount_info(file_superblock(f));
447 struct incfs_pending_read_info *reads_buf = NULL;
448 size_t reads_to_collect = len / sizeof(*reads_buf);
449 int last_known_read_sn = READ_ONCE(pr_state->last_pending_read_sn);
450 int new_max_sn = last_known_read_sn;
451 int reads_collected = 0;
452 ssize_t result = 0;
453 int i = 0;
454
455 if (!access_ok(buf, len))
456 return -EFAULT;
457
458 if (!incfs_fresh_pending_reads_exist(mi, last_known_read_sn))
459 return 0;
460
461 reads_buf = (struct incfs_pending_read_info *)get_zeroed_page(GFP_NOFS);
462 if (!reads_buf)
463 return -ENOMEM;
464
465 reads_to_collect =
466 min_t(size_t, PAGE_SIZE / sizeof(*reads_buf), reads_to_collect);
467
468 reads_collected = incfs_collect_pending_reads(
469 mi, last_known_read_sn, reads_buf, reads_to_collect);
470 if (reads_collected < 0) {
471 result = reads_collected;
472 goto out;
473 }
474
475 for (i = 0; i < reads_collected; i++)
476 if (reads_buf[i].serial_number > new_max_sn)
477 new_max_sn = reads_buf[i].serial_number;
478
479 /*
480 * Just to make sure that we don't accidentally copy more data
481 * to reads buffer than userspace can handle.
482 */
483 reads_collected = min_t(size_t, reads_collected, reads_to_collect);
484 result = reads_collected * sizeof(*reads_buf);
485
486 /* Copy reads info to the userspace buffer */
487 if (copy_to_user(buf, reads_buf, result)) {
488 result = -EFAULT;
489 goto out;
490 }
491
492 WRITE_ONCE(pr_state->last_pending_read_sn, new_max_sn);
493 *ppos = 0;
494 out:
495 if (reads_buf)
496 free_page((unsigned long)reads_buf);
497 return result;
498 }
499
500
pending_reads_poll(struct file * file,poll_table * wait)501 static __poll_t pending_reads_poll(struct file *file, poll_table *wait)
502 {
503 struct pending_reads_state *state = file->private_data;
504 struct mount_info *mi = get_mount_info(file_superblock(file));
505 __poll_t ret = 0;
506
507 poll_wait(file, &mi->mi_pending_reads_notif_wq, wait);
508 if (incfs_fresh_pending_reads_exist(mi,
509 state->last_pending_read_sn))
510 ret = EPOLLIN | EPOLLRDNORM;
511
512 return ret;
513 }
514
pending_reads_open(struct inode * inode,struct file * file)515 static int pending_reads_open(struct inode *inode, struct file *file)
516 {
517 struct pending_reads_state *state = NULL;
518
519 state = kzalloc(sizeof(*state), GFP_NOFS);
520 if (!state)
521 return -ENOMEM;
522
523 file->private_data = state;
524 return 0;
525 }
526
pending_reads_release(struct inode * inode,struct file * file)527 static int pending_reads_release(struct inode *inode, struct file *file)
528 {
529 kfree(file->private_data);
530 return 0;
531 }
532
fetch_pending_reads_inode(struct super_block * sb)533 static struct inode *fetch_pending_reads_inode(struct super_block *sb)
534 {
535 struct inode_search search = {
536 .ino = INCFS_PENDING_READS_INODE
537 };
538 struct inode *inode = iget5_locked(sb, search.ino, inode_test,
539 inode_set, &search);
540
541 if (!inode)
542 return ERR_PTR(-ENOMEM);
543
544 if (inode->i_state & I_NEW)
545 unlock_new_inode(inode);
546
547 return inode;
548 }
549
log_open(struct inode * inode,struct file * file)550 static int log_open(struct inode *inode, struct file *file)
551 {
552 struct log_file_state *log_state = NULL;
553 struct mount_info *mi = get_mount_info(file_superblock(file));
554
555 log_state = kzalloc(sizeof(*log_state), GFP_NOFS);
556 if (!log_state)
557 return -ENOMEM;
558
559 log_state->state = incfs_get_log_state(mi);
560 file->private_data = log_state;
561 return 0;
562 }
563
log_release(struct inode * inode,struct file * file)564 static int log_release(struct inode *inode, struct file *file)
565 {
566 kfree(file->private_data);
567 return 0;
568 }
569
log_read(struct file * f,char __user * buf,size_t len,loff_t * ppos)570 static ssize_t log_read(struct file *f, char __user *buf, size_t len,
571 loff_t *ppos)
572 {
573 struct log_file_state *log_state = f->private_data;
574 struct mount_info *mi = get_mount_info(file_superblock(f));
575 struct incfs_pending_read_info *reads_buf =
576 (struct incfs_pending_read_info *)__get_free_page(GFP_NOFS);
577 size_t reads_to_collect = len / sizeof(*reads_buf);
578 size_t reads_per_page = PAGE_SIZE / sizeof(*reads_buf);
579 int total_reads_collected = 0;
580 ssize_t result = 0;
581
582 if (!reads_buf)
583 return -ENOMEM;
584
585 reads_to_collect = min_t(size_t, mi->mi_log.rl_size, reads_to_collect);
586 while (reads_to_collect > 0) {
587 struct read_log_state next_state = READ_ONCE(log_state->state);
588 int reads_collected = incfs_collect_logged_reads(
589 mi, &next_state, reads_buf,
590 min_t(size_t, reads_to_collect, reads_per_page));
591 if (reads_collected <= 0) {
592 result = total_reads_collected ?
593 total_reads_collected *
594 sizeof(*reads_buf) :
595 reads_collected;
596 goto out;
597 }
598 if (copy_to_user(buf, reads_buf,
599 reads_collected * sizeof(*reads_buf))) {
600 result = total_reads_collected ?
601 total_reads_collected *
602 sizeof(*reads_buf) :
603 -EFAULT;
604 goto out;
605 }
606
607 WRITE_ONCE(log_state->state, next_state);
608 total_reads_collected += reads_collected;
609 buf += reads_collected * sizeof(*reads_buf);
610 reads_to_collect -= reads_collected;
611 }
612
613 result = total_reads_collected * sizeof(*reads_buf);
614 *ppos = 0;
615 out:
616 if (reads_buf)
617 free_page((unsigned long)reads_buf);
618 return result;
619 }
620
log_poll(struct file * file,poll_table * wait)621 static __poll_t log_poll(struct file *file, poll_table *wait)
622 {
623 struct log_file_state *log_state = file->private_data;
624 struct mount_info *mi = get_mount_info(file_superblock(file));
625 int count;
626 __poll_t ret = 0;
627
628 poll_wait(file, &mi->mi_log.ml_notif_wq, wait);
629 count = incfs_get_uncollected_logs_count(mi, log_state->state);
630 if (count >= mi->mi_options.read_log_wakeup_count)
631 ret = EPOLLIN | EPOLLRDNORM;
632
633 return ret;
634 }
635
fetch_log_inode(struct super_block * sb)636 static struct inode *fetch_log_inode(struct super_block *sb)
637 {
638 struct inode_search search = {
639 .ino = INCFS_LOG_INODE
640 };
641 struct inode *inode = iget5_locked(sb, search.ino, inode_test,
642 inode_set, &search);
643
644 if (!inode)
645 return ERR_PTR(-ENOMEM);
646
647 if (inode->i_state & I_NEW)
648 unlock_new_inode(inode);
649
650 return inode;
651 }
652
iterate_incfs_dir(struct file * file,struct dir_context * ctx)653 static int iterate_incfs_dir(struct file *file, struct dir_context *ctx)
654 {
655 struct dir_file *dir = get_incfs_dir_file(file);
656 int error = 0;
657 struct mount_info *mi = get_mount_info(file_superblock(file));
658 bool root;
659
660 if (!dir) {
661 error = -EBADF;
662 goto out;
663 }
664
665 root = dir->backing_dir->f_inode
666 == d_inode(mi->mi_backing_dir_path.dentry);
667
668 if (root && ctx->pos == 0) {
669 if (!dir_emit(ctx, pending_reads_file_name,
670 ARRAY_SIZE(pending_reads_file_name) - 1,
671 INCFS_PENDING_READS_INODE, DT_REG)) {
672 error = -EINVAL;
673 goto out;
674 }
675 ctx->pos++;
676 }
677
678 if (root && ctx->pos == 1) {
679 if (!dir_emit(ctx, log_file_name,
680 ARRAY_SIZE(log_file_name) - 1,
681 INCFS_LOG_INODE, DT_REG)) {
682 error = -EINVAL;
683 goto out;
684 }
685 ctx->pos++;
686 }
687
688 ctx->pos -= 2;
689 error = iterate_dir(dir->backing_dir, ctx);
690 ctx->pos += 2;
691 file->f_pos = dir->backing_dir->f_pos;
692 out:
693 if (error)
694 pr_warn("incfs: %s %s %d\n", __func__,
695 file->f_path.dentry->d_name.name, error);
696 return error;
697 }
698
incfs_init_dentry(struct dentry * dentry,struct path * path)699 static int incfs_init_dentry(struct dentry *dentry, struct path *path)
700 {
701 struct dentry_info *d_info = NULL;
702
703 if (!dentry || !path)
704 return -EFAULT;
705
706 d_info = kzalloc(sizeof(*d_info), GFP_NOFS);
707 if (!d_info)
708 return -ENOMEM;
709
710 d_info->backing_path = *path;
711 path_get(path);
712
713 dentry->d_fsdata = d_info;
714 return 0;
715 }
716
incfs_lookup_dentry(struct dentry * parent,const char * name)717 static struct dentry *incfs_lookup_dentry(struct dentry *parent,
718 const char *name)
719 {
720 struct inode *inode;
721 struct dentry *result = NULL;
722
723 if (!parent)
724 return ERR_PTR(-EFAULT);
725
726 inode = d_inode(parent);
727 inode_lock_nested(inode, I_MUTEX_PARENT);
728 result = lookup_one_len(name, parent, strlen(name));
729 inode_unlock(inode);
730
731 if (IS_ERR(result))
732 pr_warn("%s err:%ld\n", __func__, PTR_ERR(result));
733
734 return result;
735 }
736
open_or_create_index_dir(struct dentry * backing_dir)737 static struct dentry *open_or_create_index_dir(struct dentry *backing_dir)
738 {
739 static const char name[] = ".index";
740 struct dentry *index_dentry;
741 struct inode *backing_inode = d_inode(backing_dir);
742 int err = 0;
743
744 index_dentry = incfs_lookup_dentry(backing_dir, name);
745 if (!index_dentry) {
746 return ERR_PTR(-EINVAL);
747 } else if (IS_ERR(index_dentry)) {
748 return index_dentry;
749 } else if (d_really_is_positive(index_dentry)) {
750 /* Index already exists. */
751 return index_dentry;
752 }
753
754 /* Index needs to be created. */
755 inode_lock_nested(backing_inode, I_MUTEX_PARENT);
756 err = vfs_mkdir(backing_inode, index_dentry, 0777);
757 inode_unlock(backing_inode);
758
759 if (err)
760 return ERR_PTR(err);
761
762 if (!d_really_is_positive(index_dentry)) {
763 dput(index_dentry);
764 return ERR_PTR(-EINVAL);
765 }
766
767 return index_dentry;
768 }
769
read_single_page(struct file * f,struct page * page)770 static int read_single_page(struct file *f, struct page *page)
771 {
772 loff_t offset = 0;
773 loff_t size = 0;
774 ssize_t bytes_to_read = 0;
775 ssize_t read_result = 0;
776 struct data_file *df = get_incfs_data_file(f);
777 int result = 0;
778 void *page_start = kmap(page);
779 int block_index;
780 int timeout_ms;
781
782 if (!df)
783 return -EBADF;
784
785 offset = page_offset(page);
786 block_index = offset / INCFS_DATA_FILE_BLOCK_SIZE;
787 size = df->df_size;
788 timeout_ms = df->df_mount_info->mi_options.read_timeout_ms;
789
790 pr_debug("incfs: %s %s %lld\n", __func__,
791 f->f_path.dentry->d_name.name, offset);
792
793 if (offset < size) {
794 struct mem_range tmp = {
795 .len = 2 * INCFS_DATA_FILE_BLOCK_SIZE
796 };
797
798 tmp.data = (u8 *)__get_free_pages(GFP_NOFS, get_order(tmp.len));
799 bytes_to_read = min_t(loff_t, size - offset, PAGE_SIZE);
800 read_result = incfs_read_data_file_block(
801 range(page_start, bytes_to_read), df, block_index,
802 timeout_ms, tmp);
803
804 free_pages((unsigned long)tmp.data, get_order(tmp.len));
805 } else {
806 bytes_to_read = 0;
807 read_result = 0;
808 }
809
810 if (read_result < 0)
811 result = read_result;
812 else if (read_result < PAGE_SIZE)
813 zero_user(page, read_result, PAGE_SIZE - read_result);
814
815 if (result == 0)
816 SetPageUptodate(page);
817 else
818 SetPageError(page);
819
820 flush_dcache_page(page);
821 kunmap(page);
822 unlock_page(page);
823 return result;
824 }
825
file_id_to_str(incfs_uuid_t id)826 static char *file_id_to_str(incfs_uuid_t id)
827 {
828 char *result = kmalloc(1 + sizeof(id.bytes) * 2, GFP_NOFS);
829 char *end;
830
831 if (!result)
832 return NULL;
833
834 end = bin2hex(result, id.bytes, sizeof(id.bytes));
835 *end = 0;
836 return result;
837 }
838
incfs_copy_signature_info_from_user(struct incfs_file_signature_info __user * original)839 static struct signature_info *incfs_copy_signature_info_from_user(
840 struct incfs_file_signature_info __user *original)
841 {
842 struct incfs_file_signature_info usr_si;
843 struct signature_info *result;
844 int error;
845
846 if (!original)
847 return NULL;
848
849 if (!access_ok(original, sizeof(usr_si)))
850 return ERR_PTR(-EFAULT);
851
852 if (copy_from_user(&usr_si, original, sizeof(usr_si)) > 0)
853 return ERR_PTR(-EFAULT);
854
855 result = kzalloc(sizeof(*result), GFP_NOFS);
856 if (!result)
857 return ERR_PTR(-ENOMEM);
858
859 result->hash_alg = usr_si.hash_tree_alg;
860
861 if (result->hash_alg) {
862 void *p = kzalloc(INCFS_MAX_HASH_SIZE, GFP_NOFS);
863
864 if (!p) {
865 error = -ENOMEM;
866 goto err;
867 }
868
869 // TODO this sets the root_hash length to MAX_HASH_SIZE not
870 // the actual size. Fix, then set INCFS_MAX_HASH_SIZE back
871 // to 64
872 result->root_hash = range(p, INCFS_MAX_HASH_SIZE);
873 if (copy_from_user(p, u64_to_user_ptr(usr_si.root_hash),
874 result->root_hash.len) > 0) {
875 error = -EFAULT;
876 goto err;
877 }
878 }
879
880 if (usr_si.additional_data_size > INCFS_MAX_FILE_ATTR_SIZE) {
881 error = -E2BIG;
882 goto err;
883 }
884
885 if (usr_si.additional_data && usr_si.additional_data_size) {
886 void *p = kzalloc(usr_si.additional_data_size, GFP_NOFS);
887
888 if (!p) {
889 error = -ENOMEM;
890 goto err;
891 }
892 result->additional_data = range(p,
893 usr_si.additional_data_size);
894 if (copy_from_user(p, u64_to_user_ptr(usr_si.additional_data),
895 result->additional_data.len) > 0) {
896 error = -EFAULT;
897 goto err;
898 }
899 }
900
901 if (usr_si.signature_size > INCFS_MAX_SIGNATURE_SIZE) {
902 error = -E2BIG;
903 goto err;
904 }
905
906 if (usr_si.signature && usr_si.signature_size) {
907 void *p = kzalloc(usr_si.signature_size, GFP_NOFS);
908
909 if (!p) {
910 error = -ENOMEM;
911 goto err;
912 }
913 result->signature = range(p, usr_si.signature_size);
914 if (copy_from_user(p, u64_to_user_ptr(usr_si.signature),
915 result->signature.len) > 0) {
916 error = -EFAULT;
917 goto err;
918 }
919 }
920
921 return result;
922
923 err:
924 incfs_free_signature_info(result);
925 return ERR_PTR(-error);
926 }
927
init_new_file(struct mount_info * mi,struct dentry * dentry,incfs_uuid_t * uuid,u64 size,struct mem_range attr,struct incfs_file_signature_info __user * fsi)928 static int init_new_file(struct mount_info *mi, struct dentry *dentry,
929 incfs_uuid_t *uuid, u64 size, struct mem_range attr,
930 struct incfs_file_signature_info __user *fsi)
931 {
932 struct path path = {};
933 struct file *new_file;
934 int error = 0;
935 struct backing_file_context *bfc = NULL;
936 u32 block_count;
937 struct mem_range mem_range = {NULL};
938 struct signature_info *si = NULL;
939 struct mtree *hash_tree = NULL;
940
941 if (!mi || !dentry || !uuid)
942 return -EFAULT;
943
944 /* Resize newly created file to its true size. */
945 path = (struct path) {
946 .mnt = mi->mi_backing_dir_path.mnt,
947 .dentry = dentry
948 };
949 new_file = dentry_open(&path, O_RDWR | O_NOATIME, mi->mi_owner);
950
951 if (IS_ERR(new_file)) {
952 error = PTR_ERR(new_file);
953 goto out;
954 }
955
956 bfc = incfs_alloc_bfc(new_file);
957 if (IS_ERR(bfc)) {
958 error = PTR_ERR(bfc);
959 bfc = NULL;
960 goto out;
961 }
962
963 mutex_lock(&bfc->bc_mutex);
964 error = incfs_write_fh_to_backing_file(bfc, uuid, size);
965 if (error)
966 goto out;
967
968 block_count = (u32)get_blocks_count_for_size(size);
969 error = incfs_write_blockmap_to_backing_file(bfc, block_count, NULL);
970 if (error)
971 goto out;
972
973 /* This fill has data, reserve space for the block map. */
974 if (block_count > 0) {
975 error = incfs_write_blockmap_to_backing_file(
976 bfc, block_count, NULL);
977 if (error)
978 goto out;
979 }
980
981 if (attr.data && attr.len) {
982 error = incfs_write_file_attr_to_backing_file(bfc,
983 attr, NULL);
984 if (error)
985 goto out;
986 }
987
988 if (fsi) {
989 si = incfs_copy_signature_info_from_user(fsi);
990
991 if (IS_ERR(si)) {
992 error = PTR_ERR(si);
993 si = NULL;
994 goto out;
995 }
996
997 if (si->hash_alg) {
998 hash_tree = incfs_alloc_mtree(si->hash_alg, block_count,
999 si->root_hash);
1000 if (IS_ERR(hash_tree)) {
1001 error = PTR_ERR(hash_tree);
1002 hash_tree = NULL;
1003 goto out;
1004 }
1005
1006 // TODO This code seems wrong when len is zero - we
1007 // should error out??
1008 if (si->signature.len > 0)
1009 error = incfs_validate_pkcs7_signature(
1010 si->signature,
1011 si->root_hash,
1012 si->additional_data);
1013 if (error)
1014 goto out;
1015
1016 error = incfs_write_signature_to_backing_file(bfc,
1017 si->hash_alg,
1018 hash_tree->hash_tree_area_size,
1019 si->root_hash, si->additional_data,
1020 si->signature);
1021
1022 if (error)
1023 goto out;
1024 }
1025 }
1026
1027 out:
1028 if (bfc) {
1029 mutex_unlock(&bfc->bc_mutex);
1030 incfs_free_bfc(bfc);
1031 }
1032 incfs_free_mtree(hash_tree);
1033 incfs_free_signature_info(si);
1034 kfree(mem_range.data);
1035
1036 if (error)
1037 pr_debug("incfs: %s error: %d\n", __func__, error);
1038 return error;
1039 }
1040
incfs_link(struct dentry * what,struct dentry * where)1041 static int incfs_link(struct dentry *what, struct dentry *where)
1042 {
1043 struct dentry *parent_dentry = dget_parent(where);
1044 struct inode *pinode = d_inode(parent_dentry);
1045 int error = 0;
1046
1047 inode_lock_nested(pinode, I_MUTEX_PARENT);
1048 error = vfs_link(what, pinode, where, NULL);
1049 inode_unlock(pinode);
1050
1051 dput(parent_dentry);
1052 return error;
1053 }
1054
incfs_unlink(struct dentry * dentry)1055 static int incfs_unlink(struct dentry *dentry)
1056 {
1057 struct dentry *parent_dentry = dget_parent(dentry);
1058 struct inode *pinode = d_inode(parent_dentry);
1059 int error = 0;
1060
1061 inode_lock_nested(pinode, I_MUTEX_PARENT);
1062 error = vfs_unlink(pinode, dentry, NULL);
1063 inode_unlock(pinode);
1064
1065 dput(parent_dentry);
1066 return error;
1067 }
1068
incfs_rmdir(struct dentry * dentry)1069 static int incfs_rmdir(struct dentry *dentry)
1070 {
1071 struct dentry *parent_dentry = dget_parent(dentry);
1072 struct inode *pinode = d_inode(parent_dentry);
1073 int error = 0;
1074
1075 inode_lock_nested(pinode, I_MUTEX_PARENT);
1076 error = vfs_rmdir(pinode, dentry);
1077 inode_unlock(pinode);
1078
1079 dput(parent_dentry);
1080 return error;
1081 }
1082
dir_relative_path_resolve(struct mount_info * mi,const char __user * relative_path,struct path * result_path)1083 static int dir_relative_path_resolve(
1084 struct mount_info *mi,
1085 const char __user *relative_path,
1086 struct path *result_path)
1087 {
1088 struct path *base_path = &mi->mi_backing_dir_path;
1089 int dir_fd = get_unused_fd_flags(0);
1090 struct file *dir_f = NULL;
1091 int error = 0;
1092
1093 if (dir_fd < 0)
1094 return dir_fd;
1095
1096 dir_f = dentry_open(base_path, O_RDONLY | O_NOATIME, mi->mi_owner);
1097
1098 if (IS_ERR(dir_f)) {
1099 error = PTR_ERR(dir_f);
1100 goto out;
1101 }
1102 fd_install(dir_fd, dir_f);
1103
1104 if (!relative_path) {
1105 /* No relative path given, just return the base dir. */
1106 *result_path = *base_path;
1107 path_get(result_path);
1108 goto out;
1109 }
1110
1111 error = user_path_at_empty(dir_fd, relative_path,
1112 LOOKUP_FOLLOW | LOOKUP_DIRECTORY, result_path, NULL);
1113
1114 out:
1115 ksys_close(dir_fd);
1116 if (error)
1117 pr_debug("incfs: %s %d\n", __func__, error);
1118 return error;
1119 }
1120
validate_name(char * file_name)1121 static int validate_name(char *file_name)
1122 {
1123 struct mem_range name = range(file_name, strlen(file_name));
1124 int i = 0;
1125
1126 if (name.len > INCFS_MAX_NAME_LEN)
1127 return -ENAMETOOLONG;
1128
1129 if (incfs_equal_ranges(pending_reads_file_name_range, name))
1130 return -EINVAL;
1131
1132 for (i = 0; i < name.len; i++)
1133 if (name.data[i] == '/')
1134 return -EINVAL;
1135
1136 return 0;
1137 }
1138
ioctl_create_file(struct mount_info * mi,struct incfs_new_file_args __user * usr_args)1139 static long ioctl_create_file(struct mount_info *mi,
1140 struct incfs_new_file_args __user *usr_args)
1141 {
1142 struct incfs_new_file_args args;
1143 char *file_id_str = NULL;
1144 struct dentry *index_file_dentry = NULL;
1145 struct dentry *named_file_dentry = NULL;
1146 struct path parent_dir_path = {};
1147 struct inode *index_dir_inode = NULL;
1148 __le64 size_attr_value = 0;
1149 char *file_name = NULL;
1150 char *attr_value = NULL;
1151 int error = 0;
1152 bool locked = false;
1153
1154 if (!mi || !mi->mi_index_dir) {
1155 error = -EFAULT;
1156 goto out;
1157 }
1158 if (!access_ok(usr_args, sizeof(args))) {
1159 error = -EFAULT;
1160 goto out;
1161 }
1162 if (copy_from_user(&args, usr_args, sizeof(args)) > 0) {
1163 error = -EFAULT;
1164 goto out;
1165 }
1166
1167 file_name = strndup_user(u64_to_user_ptr(args.file_name), PATH_MAX);
1168 if (IS_ERR(file_name)) {
1169 error = PTR_ERR(file_name);
1170 file_name = NULL;
1171 goto out;
1172 }
1173
1174 error = validate_name(file_name);
1175 if (error)
1176 goto out;
1177
1178 file_id_str = file_id_to_str(args.file_id);
1179 if (!file_id_str) {
1180 error = -ENOMEM;
1181 goto out;
1182 }
1183
1184 error = mutex_lock_interruptible(&mi->mi_dir_struct_mutex);
1185 if (error)
1186 goto out;
1187 locked = true;
1188
1189 /* Find a directory to put the file into. */
1190 error = dir_relative_path_resolve(mi,
1191 u64_to_user_ptr(args.directory_path),
1192 &parent_dir_path);
1193 if (error)
1194 goto out;
1195
1196 if (parent_dir_path.dentry == mi->mi_index_dir) {
1197 /* Can't create a file directly inside .index */
1198 error = -EBUSY;
1199 goto out;
1200 }
1201
1202 /* Look up a dentry in the parent dir. It should be negative. */
1203 named_file_dentry = incfs_lookup_dentry(parent_dir_path.dentry,
1204 file_name);
1205 if (!named_file_dentry) {
1206 error = -EFAULT;
1207 goto out;
1208 }
1209 if (IS_ERR(named_file_dentry)) {
1210 error = PTR_ERR(named_file_dentry);
1211 named_file_dentry = NULL;
1212 goto out;
1213 }
1214 if (d_really_is_positive(named_file_dentry)) {
1215 /* File with this path already exists. */
1216 error = -EEXIST;
1217 goto out;
1218 }
1219 /* Look up a dentry in the .index dir. It should be negative. */
1220 index_file_dentry = incfs_lookup_dentry(mi->mi_index_dir, file_id_str);
1221 if (!index_file_dentry) {
1222 error = -EFAULT;
1223 goto out;
1224 }
1225 if (IS_ERR(index_file_dentry)) {
1226 error = PTR_ERR(index_file_dentry);
1227 index_file_dentry = NULL;
1228 goto out;
1229 }
1230 if (d_really_is_positive(index_file_dentry)) {
1231 /* File with this ID already exists in index. */
1232 error = -EEXIST;
1233 goto out;
1234 }
1235
1236 /* Creating a file in the .index dir. */
1237 index_dir_inode = d_inode(mi->mi_index_dir);
1238 inode_lock_nested(index_dir_inode, I_MUTEX_PARENT);
1239 error = vfs_create(index_dir_inode, index_file_dentry,
1240 args.mode, true);
1241 inode_unlock(index_dir_inode);
1242
1243 if (error)
1244 goto out;
1245 if (!d_really_is_positive(index_file_dentry)) {
1246 error = -EINVAL;
1247 goto out;
1248 }
1249
1250 /* Save the file's ID as an xattr for easy fetching in future. */
1251 error = vfs_setxattr(index_file_dentry, INCFS_XATTR_ID_NAME,
1252 file_id_str, strlen(file_id_str), XATTR_CREATE);
1253 if (error) {
1254 pr_debug("incfs: vfs_setxattr err:%d\n", error);
1255 goto delete_index_file;
1256 }
1257
1258 /* Save the file's size as an xattr for easy fetching in future. */
1259 size_attr_value = cpu_to_le64(args.size);
1260 error = vfs_setxattr(index_file_dentry, INCFS_XATTR_SIZE_NAME,
1261 (char *)&size_attr_value, sizeof(size_attr_value),
1262 XATTR_CREATE);
1263 if (error) {
1264 pr_debug("incfs: vfs_setxattr err:%d\n", error);
1265 goto delete_index_file;
1266 }
1267
1268 /* Save the file's attrubute as an xattr */
1269 if (args.file_attr_len && args.file_attr) {
1270 if (args.file_attr_len > INCFS_MAX_FILE_ATTR_SIZE) {
1271 error = -E2BIG;
1272 goto delete_index_file;
1273 }
1274
1275 attr_value = kmalloc(args.file_attr_len, GFP_NOFS);
1276 if (!attr_value) {
1277 error = -ENOMEM;
1278 goto delete_index_file;
1279 }
1280
1281 if (!access_ok(u64_to_user_ptr(args.file_attr),
1282 args.file_attr_len)) {
1283 error = -EFAULT;
1284 goto delete_index_file;
1285 }
1286
1287 if (copy_from_user(attr_value,
1288 u64_to_user_ptr(args.file_attr),
1289 args.file_attr_len) > 0) {
1290 error = -EFAULT;
1291 goto delete_index_file;
1292 }
1293
1294 error = vfs_setxattr(index_file_dentry,
1295 INCFS_XATTR_METADATA_NAME,
1296 attr_value, args.file_attr_len,
1297 XATTR_CREATE);
1298
1299 if (error)
1300 goto delete_index_file;
1301 }
1302
1303 /* Initializing a newly created file. */
1304 error = init_new_file(mi, index_file_dentry, &args.file_id, args.size,
1305 range(attr_value, args.file_attr_len),
1306 (struct incfs_file_signature_info __user *)
1307 args.signature_info);
1308 if (error)
1309 goto delete_index_file;
1310
1311 /* Linking a file with it's real name from the requested dir. */
1312 error = incfs_link(index_file_dentry, named_file_dentry);
1313
1314 if (!error)
1315 goto out;
1316
1317 delete_index_file:
1318 incfs_unlink(index_file_dentry);
1319
1320 out:
1321 if (error)
1322 pr_debug("incfs: %s err:%d\n", __func__, error);
1323
1324 kfree(file_id_str);
1325 kfree(file_name);
1326 kfree(attr_value);
1327 dput(named_file_dentry);
1328 dput(index_file_dentry);
1329 path_put(&parent_dir_path);
1330 if (locked)
1331 mutex_unlock(&mi->mi_dir_struct_mutex);
1332 return error;
1333 }
1334
ioctl_read_file_signature(struct file * f,void __user * arg)1335 static long ioctl_read_file_signature(struct file *f, void __user *arg)
1336 {
1337 struct incfs_get_file_sig_args __user *args_usr_ptr = arg;
1338 struct incfs_get_file_sig_args args = {};
1339 u8 *sig_buffer = NULL;
1340 size_t sig_buf_size = 0;
1341 int error = 0;
1342 int read_result = 0;
1343 struct data_file *df = get_incfs_data_file(f);
1344
1345 if (!df)
1346 return -EINVAL;
1347
1348 if (!access_ok(args_usr_ptr, sizeof(args)))
1349 return -EFAULT;
1350 if (copy_from_user(&args, args_usr_ptr, sizeof(args)) > 0)
1351 return -EINVAL;
1352
1353 if (!access_ok(u64_to_user_ptr(args.file_signature),
1354 args.file_signature_buf_size))
1355 return -EFAULT;
1356
1357 sig_buf_size = args.file_signature_buf_size;
1358 if (sig_buf_size > INCFS_MAX_SIGNATURE_SIZE)
1359 return -E2BIG;
1360
1361 sig_buffer = kzalloc(sig_buf_size, GFP_NOFS);
1362 if (!sig_buffer)
1363 return -ENOMEM;
1364
1365 read_result = incfs_read_file_signature(df,
1366 range(sig_buffer, sig_buf_size));
1367
1368 if (read_result < 0) {
1369 error = read_result;
1370 goto out;
1371 }
1372
1373 if (copy_to_user(u64_to_user_ptr(args.file_signature), sig_buffer,
1374 read_result)) {
1375 error = -EFAULT;
1376 goto out;
1377 }
1378
1379 args.file_signature_len_out = read_result;
1380 if (copy_to_user(args_usr_ptr, &args, sizeof(args)))
1381 error = -EFAULT;
1382
1383 out:
1384 kfree(sig_buffer);
1385
1386 return error;
1387 }
1388
dispatch_ioctl(struct file * f,unsigned int req,unsigned long arg)1389 static long dispatch_ioctl(struct file *f, unsigned int req, unsigned long arg)
1390 {
1391 struct mount_info *mi = get_mount_info(file_superblock(f));
1392
1393 switch (req) {
1394 case INCFS_IOC_CREATE_FILE:
1395 return ioctl_create_file(mi, (void __user *)arg);
1396 case INCFS_IOC_READ_FILE_SIGNATURE:
1397 return ioctl_read_file_signature(f, (void __user *)arg);
1398 default:
1399 return -EINVAL;
1400 }
1401 }
1402
dir_lookup(struct inode * dir_inode,struct dentry * dentry,unsigned int flags)1403 static struct dentry *dir_lookup(struct inode *dir_inode, struct dentry *dentry,
1404 unsigned int flags)
1405 {
1406 struct mount_info *mi = get_mount_info(dir_inode->i_sb);
1407 struct dentry *dir_dentry = NULL;
1408 struct dentry *backing_dentry = NULL;
1409 struct path dir_backing_path = {};
1410 struct inode_info *dir_info = get_incfs_node(dir_inode);
1411 struct mem_range name_range =
1412 range((u8 *)dentry->d_name.name, dentry->d_name.len);
1413 int err = 0;
1414
1415 if (d_inode(mi->mi_backing_dir_path.dentry) ==
1416 dir_info->n_backing_inode) {
1417 /* We do lookup in the FS root. Show pseudo files. */
1418
1419 if (incfs_equal_ranges(pending_reads_file_name_range,
1420 name_range)) {
1421 struct inode *inode = fetch_pending_reads_inode(
1422 dir_inode->i_sb);
1423
1424 if (IS_ERR(inode)) {
1425 err = PTR_ERR(inode);
1426 goto out;
1427 }
1428
1429 d_add(dentry, inode);
1430 goto out;
1431 }
1432
1433 if (incfs_equal_ranges(log_file_name_range, name_range)) {
1434 struct inode *inode = fetch_log_inode(
1435 dir_inode->i_sb);
1436
1437 if (IS_ERR(inode)) {
1438 err = PTR_ERR(inode);
1439 goto out;
1440 }
1441
1442 d_add(dentry, inode);
1443 goto out;
1444 }
1445 }
1446
1447 dir_dentry = dget_parent(dentry);
1448 get_incfs_backing_path(dir_dentry, &dir_backing_path);
1449 backing_dentry = incfs_lookup_dentry(dir_backing_path.dentry,
1450 dentry->d_name.name);
1451
1452 if (!backing_dentry || IS_ERR(backing_dentry)) {
1453 err = IS_ERR(backing_dentry)
1454 ? PTR_ERR(backing_dentry)
1455 : -EFAULT;
1456 goto out;
1457 } else {
1458 struct inode *inode = NULL;
1459 struct path backing_path = {
1460 .mnt = dir_backing_path.mnt,
1461 .dentry = backing_dentry
1462 };
1463
1464 err = incfs_init_dentry(dentry, &backing_path);
1465 if (err)
1466 goto out;
1467
1468 if (!d_really_is_positive(backing_dentry)) {
1469 /*
1470 * No such entry found in the backing dir.
1471 * Create a negative entry.
1472 */
1473 d_add(dentry, NULL);
1474 err = 0;
1475 goto out;
1476 }
1477
1478 if (d_inode(backing_dentry)->i_sb !=
1479 dir_info->n_backing_inode->i_sb) {
1480 /*
1481 * Somehow after the path lookup we ended up in a
1482 * different fs mount. If we keep going it's going
1483 * to end badly.
1484 */
1485 err = -EXDEV;
1486 goto out;
1487 }
1488
1489 inode = fetch_regular_inode(dir_inode->i_sb, backing_dentry);
1490 if (IS_ERR(inode)) {
1491 err = PTR_ERR(inode);
1492 goto out;
1493 }
1494
1495 d_add(dentry, inode);
1496 }
1497
1498 out:
1499 dput(dir_dentry);
1500 dput(backing_dentry);
1501 path_put(&dir_backing_path);
1502 if (err)
1503 pr_debug("incfs: %s %s %d\n", __func__,
1504 dentry->d_name.name, err);
1505 return ERR_PTR(err);
1506 }
1507
dir_mkdir(struct inode * dir,struct dentry * dentry,umode_t mode)1508 static int dir_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1509 {
1510 struct mount_info *mi = get_mount_info(dir->i_sb);
1511 struct inode_info *dir_node = get_incfs_node(dir);
1512 struct dentry *backing_dentry = NULL;
1513 struct path backing_path = {};
1514 int err = 0;
1515
1516
1517 if (!mi || !dir_node || !dir_node->n_backing_inode)
1518 return -EBADF;
1519
1520 err = mutex_lock_interruptible(&mi->mi_dir_struct_mutex);
1521 if (err)
1522 return err;
1523
1524 get_incfs_backing_path(dentry, &backing_path);
1525 backing_dentry = backing_path.dentry;
1526
1527 if (!backing_dentry) {
1528 err = -EBADF;
1529 goto out;
1530 }
1531
1532 if (backing_dentry->d_parent == mi->mi_index_dir) {
1533 /* Can't create a subdir inside .index */
1534 err = -EBUSY;
1535 goto out;
1536 }
1537
1538 inode_lock_nested(dir_node->n_backing_inode, I_MUTEX_PARENT);
1539 err = vfs_mkdir(dir_node->n_backing_inode, backing_dentry, mode);
1540 inode_unlock(dir_node->n_backing_inode);
1541 if (!err) {
1542 struct inode *inode = NULL;
1543
1544 if (d_really_is_negative(backing_dentry)) {
1545 err = -EINVAL;
1546 goto out;
1547 }
1548
1549 inode = fetch_regular_inode(dir->i_sb, backing_dentry);
1550 if (IS_ERR(inode)) {
1551 err = PTR_ERR(inode);
1552 goto out;
1553 }
1554 d_instantiate(dentry, inode);
1555 }
1556
1557 out:
1558 if (d_really_is_negative(dentry))
1559 d_drop(dentry);
1560 path_put(&backing_path);
1561 mutex_unlock(&mi->mi_dir_struct_mutex);
1562 if (err)
1563 pr_debug("incfs: %s err:%d\n", __func__, err);
1564 return err;
1565 }
1566
1567 /* Delete file referenced by backing_dentry and also its hardlink from .index */
final_file_delete(struct mount_info * mi,struct dentry * backing_dentry)1568 static int final_file_delete(struct mount_info *mi,
1569 struct dentry *backing_dentry)
1570 {
1571 struct dentry *index_file_dentry = NULL;
1572 /* 2 chars per byte of file ID + 1 char for \0 */
1573 char file_id_str[2 * sizeof(incfs_uuid_t) + 1] = {0};
1574 ssize_t uuid_size = 0;
1575 int error = 0;
1576
1577 WARN_ON(!mutex_is_locked(&mi->mi_dir_struct_mutex));
1578 uuid_size = vfs_getxattr(backing_dentry, INCFS_XATTR_ID_NAME,
1579 file_id_str, 2 * sizeof(incfs_uuid_t));
1580 if (uuid_size < 0) {
1581 error = uuid_size;
1582 goto out;
1583 }
1584
1585 if (uuid_size != 2 * sizeof(incfs_uuid_t)) {
1586 error = -EBADMSG;
1587 goto out;
1588 }
1589
1590 index_file_dentry = incfs_lookup_dentry(mi->mi_index_dir, file_id_str);
1591 if (IS_ERR(index_file_dentry)) {
1592 error = PTR_ERR(index_file_dentry);
1593 goto out;
1594 }
1595
1596 error = incfs_unlink(backing_dentry);
1597 if (error)
1598 goto out;
1599
1600 if (d_really_is_positive(index_file_dentry))
1601 error = incfs_unlink(index_file_dentry);
1602 out:
1603 if (error)
1604 pr_debug("incfs: delete_file_from_index err:%d\n", error);
1605 return error;
1606 }
1607
dir_unlink(struct inode * dir,struct dentry * dentry)1608 static int dir_unlink(struct inode *dir, struct dentry *dentry)
1609 {
1610 struct mount_info *mi = get_mount_info(dir->i_sb);
1611 struct path backing_path = {};
1612 struct kstat stat;
1613 int err = 0;
1614
1615 err = mutex_lock_interruptible(&mi->mi_dir_struct_mutex);
1616 if (err)
1617 return err;
1618
1619 get_incfs_backing_path(dentry, &backing_path);
1620 if (!backing_path.dentry) {
1621 err = -EBADF;
1622 goto out;
1623 }
1624
1625 if (backing_path.dentry->d_parent == mi->mi_index_dir) {
1626 /* Direct unlink from .index are not allowed. */
1627 err = -EBUSY;
1628 goto out;
1629 }
1630
1631 err = vfs_getattr(&backing_path, &stat, STATX_NLINK,
1632 AT_STATX_SYNC_AS_STAT);
1633 if (err)
1634 goto out;
1635
1636 if (stat.nlink == 2) {
1637 /*
1638 * This is the last named link to this file. The only one left
1639 * is in .index. Remove them both now.
1640 */
1641 err = final_file_delete(mi, backing_path.dentry);
1642 } else {
1643 /* There are other links to this file. Remove just this one. */
1644 err = incfs_unlink(backing_path.dentry);
1645 }
1646
1647 d_drop(dentry);
1648 out:
1649 path_put(&backing_path);
1650 if (err)
1651 pr_debug("incfs: %s err:%d\n", __func__, err);
1652 mutex_unlock(&mi->mi_dir_struct_mutex);
1653 return err;
1654 }
1655
dir_link(struct dentry * old_dentry,struct inode * dir,struct dentry * new_dentry)1656 static int dir_link(struct dentry *old_dentry, struct inode *dir,
1657 struct dentry *new_dentry)
1658 {
1659 struct mount_info *mi = get_mount_info(dir->i_sb);
1660 struct path backing_old_path = {};
1661 struct path backing_new_path = {};
1662 int error = 0;
1663
1664 error = mutex_lock_interruptible(&mi->mi_dir_struct_mutex);
1665 if (error)
1666 return error;
1667
1668 get_incfs_backing_path(old_dentry, &backing_old_path);
1669 get_incfs_backing_path(new_dentry, &backing_new_path);
1670
1671 if (backing_new_path.dentry->d_parent == mi->mi_index_dir) {
1672 /* Can't link to .index */
1673 error = -EBUSY;
1674 goto out;
1675 }
1676
1677 error = incfs_link(backing_old_path.dentry, backing_new_path.dentry);
1678 if (!error) {
1679 struct inode *inode = NULL;
1680 struct dentry *bdentry = backing_new_path.dentry;
1681
1682 if (d_really_is_negative(bdentry)) {
1683 error = -EINVAL;
1684 goto out;
1685 }
1686
1687 inode = fetch_regular_inode(dir->i_sb, bdentry);
1688 if (IS_ERR(inode)) {
1689 error = PTR_ERR(inode);
1690 goto out;
1691 }
1692 d_instantiate(new_dentry, inode);
1693 }
1694
1695 out:
1696 path_put(&backing_old_path);
1697 path_put(&backing_new_path);
1698 if (error)
1699 pr_debug("incfs: %s err:%d\n", __func__, error);
1700 mutex_unlock(&mi->mi_dir_struct_mutex);
1701 return error;
1702 }
1703
dir_rmdir(struct inode * dir,struct dentry * dentry)1704 static int dir_rmdir(struct inode *dir, struct dentry *dentry)
1705 {
1706 struct mount_info *mi = get_mount_info(dir->i_sb);
1707 struct path backing_path = {};
1708 int err = 0;
1709
1710 err = mutex_lock_interruptible(&mi->mi_dir_struct_mutex);
1711 if (err)
1712 return err;
1713
1714 get_incfs_backing_path(dentry, &backing_path);
1715 if (!backing_path.dentry) {
1716 err = -EBADF;
1717 goto out;
1718 }
1719
1720 if (backing_path.dentry == mi->mi_index_dir) {
1721 /* Can't delete .index */
1722 err = -EBUSY;
1723 goto out;
1724 }
1725
1726 err = incfs_rmdir(backing_path.dentry);
1727 if (!err)
1728 d_drop(dentry);
1729 out:
1730 path_put(&backing_path);
1731 if (err)
1732 pr_debug("incfs: %s err:%d\n", __func__, err);
1733 mutex_unlock(&mi->mi_dir_struct_mutex);
1734 return err;
1735 }
1736
dir_rename(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry)1737 static int dir_rename(struct inode *old_dir, struct dentry *old_dentry,
1738 struct inode *new_dir, struct dentry *new_dentry)
1739 {
1740 struct mount_info *mi = get_mount_info(old_dir->i_sb);
1741 struct dentry *backing_old_dentry;
1742 struct dentry *backing_new_dentry;
1743 struct dentry *backing_old_dir_dentry;
1744 struct dentry *backing_new_dir_dentry;
1745 struct inode *target_inode;
1746 struct dentry *trap;
1747 int error = 0;
1748
1749 error = mutex_lock_interruptible(&mi->mi_dir_struct_mutex);
1750 if (error)
1751 return error;
1752
1753 backing_old_dentry = get_incfs_dentry(old_dentry)->backing_path.dentry;
1754 backing_new_dentry = get_incfs_dentry(new_dentry)->backing_path.dentry;
1755 dget(backing_old_dentry);
1756 dget(backing_new_dentry);
1757
1758 backing_old_dir_dentry = dget_parent(backing_old_dentry);
1759 backing_new_dir_dentry = dget_parent(backing_new_dentry);
1760 target_inode = d_inode(new_dentry);
1761
1762 if (backing_old_dir_dentry == mi->mi_index_dir) {
1763 /* Direct moves from .index are not allowed. */
1764 error = -EBUSY;
1765 goto out;
1766 }
1767
1768 trap = lock_rename(backing_old_dir_dentry, backing_new_dir_dentry);
1769
1770 if (trap == backing_old_dentry) {
1771 error = -EINVAL;
1772 goto unlock_out;
1773 }
1774 if (trap == backing_new_dentry) {
1775 error = -ENOTEMPTY;
1776 goto unlock_out;
1777 }
1778
1779 error = vfs_rename(d_inode(backing_old_dir_dentry), backing_old_dentry,
1780 d_inode(backing_new_dir_dentry), backing_new_dentry,
1781 NULL, 0);
1782 if (error)
1783 goto unlock_out;
1784 if (target_inode)
1785 fsstack_copy_attr_all(target_inode,
1786 get_incfs_node(target_inode)->n_backing_inode);
1787 fsstack_copy_attr_all(new_dir, d_inode(backing_new_dir_dentry));
1788 if (new_dir != old_dir)
1789 fsstack_copy_attr_all(old_dir, d_inode(backing_old_dir_dentry));
1790
1791 unlock_out:
1792 unlock_rename(backing_old_dir_dentry, backing_new_dir_dentry);
1793
1794 out:
1795 dput(backing_new_dir_dentry);
1796 dput(backing_old_dir_dentry);
1797 dput(backing_new_dentry);
1798 dput(backing_old_dentry);
1799
1800 mutex_unlock(&mi->mi_dir_struct_mutex);
1801 if (error)
1802 pr_debug("incfs: %s err:%d\n", __func__, error);
1803 return error;
1804 }
1805
1806
file_open(struct inode * inode,struct file * file)1807 static int file_open(struct inode *inode, struct file *file)
1808 {
1809 struct mount_info *mi = get_mount_info(inode->i_sb);
1810 struct file *backing_file = NULL;
1811 struct path backing_path = {};
1812 int err = 0;
1813
1814 get_incfs_backing_path(file->f_path.dentry, &backing_path);
1815 backing_file = dentry_open(&backing_path, O_RDWR | O_NOATIME,
1816 mi->mi_owner);
1817 path_put(&backing_path);
1818
1819 if (IS_ERR(backing_file)) {
1820 err = PTR_ERR(backing_file);
1821 backing_file = NULL;
1822 goto out;
1823 }
1824
1825 if (S_ISREG(inode->i_mode))
1826 err = make_inode_ready_for_data_ops(mi, inode, backing_file);
1827 else if (S_ISDIR(inode->i_mode)) {
1828 struct dir_file *dir = NULL;
1829
1830 dir = incfs_open_dir_file(mi, backing_file);
1831 if (IS_ERR(dir))
1832 err = PTR_ERR(dir);
1833 else
1834 file->private_data = dir;
1835 } else
1836 err = -EBADF;
1837
1838 out:
1839 if (err)
1840 pr_debug("incfs: %s name:%s err: %d\n", __func__,
1841 file->f_path.dentry->d_name.name, err);
1842 if (backing_file)
1843 fput(backing_file);
1844 return err;
1845 }
1846
file_release(struct inode * inode,struct file * file)1847 static int file_release(struct inode *inode, struct file *file)
1848 {
1849 if (S_ISREG(inode->i_mode)) {
1850 /* Do nothing.
1851 * data_file is released only by inode eviction.
1852 */
1853 } else if (S_ISDIR(inode->i_mode)) {
1854 struct dir_file *dir = get_incfs_dir_file(file);
1855
1856 incfs_free_dir_file(dir);
1857 }
1858
1859 return 0;
1860 }
1861
file_write(struct file * f,const char __user * buf,size_t size,loff_t * offset)1862 static ssize_t file_write(struct file *f, const char __user *buf,
1863 size_t size, loff_t *offset)
1864 {
1865 struct data_file *df = get_incfs_data_file(f);
1866 const ssize_t data_buf_size = 2 * INCFS_DATA_FILE_BLOCK_SIZE;
1867 size_t block_count = size / sizeof(struct incfs_new_data_block);
1868 struct incfs_new_data_block __user *usr_blocks =
1869 (struct incfs_new_data_block __user *)buf;
1870 u8 *data_buf = NULL;
1871 ssize_t error = 0;
1872 int i = 0;
1873
1874 if (!df)
1875 return -EBADF;
1876
1877 if (!access_ok(usr_blocks, size))
1878 return -EFAULT;
1879
1880 data_buf = (u8 *)__get_free_pages(GFP_NOFS, get_order(data_buf_size));
1881 if (!data_buf)
1882 return -ENOMEM;
1883
1884 for (i = 0; i < block_count; i++) {
1885 struct incfs_new_data_block block = {};
1886
1887 if (copy_from_user(&block, &usr_blocks[i], sizeof(block)) > 0) {
1888 error = -EFAULT;
1889 break;
1890 }
1891
1892 if (block.data_len > data_buf_size) {
1893 error = -E2BIG;
1894 break;
1895 }
1896 if (!access_ok(u64_to_user_ptr(block.data),
1897 block.data_len)) {
1898 error = -EFAULT;
1899 break;
1900 }
1901 if (copy_from_user(data_buf, u64_to_user_ptr(block.data),
1902 block.data_len) > 0) {
1903 error = -EFAULT;
1904 break;
1905 }
1906 block.data = 0; /* To make sure nobody uses it. */
1907 if (block.flags & INCFS_BLOCK_FLAGS_HASH) {
1908 error = incfs_process_new_hash_block(df, &block,
1909 data_buf);
1910 } else {
1911 error = incfs_process_new_data_block(df, &block,
1912 data_buf);
1913 }
1914 if (error)
1915 break;
1916 }
1917
1918 if (data_buf)
1919 free_pages((unsigned long)data_buf, get_order(data_buf_size));
1920 *offset = 0;
1921
1922 /*
1923 * Only report the error if no records were processed, otherwise
1924 * just return how many were processed successfully.
1925 */
1926 if (i == 0)
1927 return error;
1928
1929 return i * sizeof(struct incfs_new_data_block);
1930 }
1931
1932
dentry_revalidate(struct dentry * d,unsigned int flags)1933 static int dentry_revalidate(struct dentry *d, unsigned int flags)
1934 {
1935 struct path backing_path = {};
1936 struct inode_info *info = get_incfs_node(d_inode(d));
1937 struct inode *binode = (info == NULL) ? NULL : info->n_backing_inode;
1938 struct dentry *backing_dentry = NULL;
1939 int result = 0;
1940
1941 if (flags & LOOKUP_RCU)
1942 return -ECHILD;
1943
1944 get_incfs_backing_path(d, &backing_path);
1945 backing_dentry = backing_path.dentry;
1946 if (!backing_dentry)
1947 goto out;
1948
1949 if (d_inode(backing_dentry) != binode) {
1950 /*
1951 * Backing inodes obtained via dentry and inode don't match.
1952 * It indicates that most likely backing dir has changed
1953 * directly bypassing Incremental FS interface.
1954 */
1955 goto out;
1956 }
1957
1958 if (backing_dentry->d_flags & DCACHE_OP_REVALIDATE) {
1959 result = backing_dentry->d_op->d_revalidate(backing_dentry,
1960 flags);
1961 } else
1962 result = 1;
1963
1964 out:
1965 path_put(&backing_path);
1966 return result;
1967 }
1968
dentry_release(struct dentry * d)1969 static void dentry_release(struct dentry *d)
1970 {
1971 struct dentry_info *di = get_incfs_dentry(d);
1972
1973 if (di)
1974 path_put(&di->backing_path);
1975 d->d_fsdata = NULL;
1976 }
1977
alloc_inode(struct super_block * sb)1978 static struct inode *alloc_inode(struct super_block *sb)
1979 {
1980 struct inode_info *node = kzalloc(sizeof(*node), GFP_NOFS);
1981
1982 /* TODO: add a slab-based cache here. */
1983 if (!node)
1984 return NULL;
1985 inode_init_once(&node->n_vfs_inode);
1986 return &node->n_vfs_inode;
1987 }
1988
free_inode(struct inode * inode)1989 static void free_inode(struct inode *inode)
1990 {
1991 struct inode_info *node = get_incfs_node(inode);
1992
1993 kfree(node);
1994 }
1995
evict_inode(struct inode * inode)1996 static void evict_inode(struct inode *inode)
1997 {
1998 struct inode_info *node = get_incfs_node(inode);
1999
2000 if (node) {
2001 if (node->n_backing_inode) {
2002 iput(node->n_backing_inode);
2003 node->n_backing_inode = NULL;
2004 }
2005 if (node->n_file) {
2006 incfs_free_data_file(node->n_file);
2007 node->n_file = NULL;
2008 }
2009 }
2010
2011 truncate_inode_pages(&inode->i_data, 0);
2012 clear_inode(inode);
2013 }
2014
incfs_getxattr(struct dentry * d,const char * name,void * value,size_t size)2015 static ssize_t incfs_getxattr(struct dentry *d, const char *name,
2016 void *value, size_t size)
2017 {
2018 struct dentry_info *di = get_incfs_dentry(d);
2019
2020 if (!di || !di->backing_path.dentry)
2021 return -ENODATA;
2022
2023 return vfs_getxattr(di->backing_path.dentry, name, value, size);
2024 }
2025
incfs_listxattr(struct dentry * d,char * list,size_t size)2026 static ssize_t incfs_listxattr(struct dentry *d, char *list, size_t size)
2027 {
2028 struct dentry_info *di = get_incfs_dentry(d);
2029
2030 if (!di || !di->backing_path.dentry)
2031 return -ENODATA;
2032
2033 return vfs_listxattr(di->backing_path.dentry, list, size);
2034 }
2035
incfs_mount_fs(struct file_system_type * type,int flags,const char * dev_name,void * data)2036 struct dentry *incfs_mount_fs(struct file_system_type *type, int flags,
2037 const char *dev_name, void *data)
2038 {
2039 struct mount_options options = {};
2040 struct mount_info *mi = NULL;
2041 struct path backing_dir_path = {};
2042 struct dentry *index_dir;
2043 struct super_block *src_fs_sb = NULL;
2044 struct inode *root_inode = NULL;
2045 struct super_block *sb = sget(type, NULL, set_anon_super, flags, NULL);
2046 int error = 0;
2047
2048 if (IS_ERR(sb))
2049 return ERR_CAST(sb);
2050
2051 sb->s_op = &incfs_super_ops;
2052 sb->s_d_op = &incfs_dentry_ops;
2053 sb->s_flags |= S_NOATIME;
2054 sb->s_magic = INCFS_MAGIC_NUMBER;
2055 sb->s_time_gran = 1;
2056 sb->s_blocksize = INCFS_DATA_FILE_BLOCK_SIZE;
2057 sb->s_blocksize_bits = blksize_bits(sb->s_blocksize);
2058 sb->s_xattr = incfs_xattr_ops;
2059
2060 BUILD_BUG_ON(PAGE_SIZE != INCFS_DATA_FILE_BLOCK_SIZE);
2061
2062 error = parse_options(&options, (char *)data);
2063 if (error != 0) {
2064 pr_err("incfs: Options parsing error. %d\n", error);
2065 goto err;
2066 }
2067
2068 sb->s_bdi->ra_pages = options.readahead_pages;
2069 if (!dev_name) {
2070 pr_err("incfs: Backing dir is not set, filesystem can't be mounted.\n");
2071 error = -ENOENT;
2072 goto err;
2073 }
2074
2075 error = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
2076 &backing_dir_path);
2077 if (error || backing_dir_path.dentry == NULL ||
2078 !d_really_is_positive(backing_dir_path.dentry)) {
2079 pr_err("incfs: Error accessing: %s.\n",
2080 dev_name);
2081 goto err;
2082 }
2083 src_fs_sb = backing_dir_path.dentry->d_sb;
2084 sb->s_maxbytes = src_fs_sb->s_maxbytes;
2085
2086 mi = incfs_alloc_mount_info(sb, &options, &backing_dir_path);
2087
2088 if (IS_ERR_OR_NULL(mi)) {
2089 error = PTR_ERR(mi);
2090 pr_err("incfs: Error allocating mount info. %d\n", error);
2091 mi = NULL;
2092 goto err;
2093 }
2094
2095 index_dir = open_or_create_index_dir(backing_dir_path.dentry);
2096 if (IS_ERR_OR_NULL(index_dir)) {
2097 error = PTR_ERR(index_dir);
2098 pr_err("incfs: Can't find or create .index dir in %s\n",
2099 dev_name);
2100 goto err;
2101 }
2102 mi->mi_index_dir = index_dir;
2103
2104 sb->s_fs_info = mi;
2105 root_inode = fetch_regular_inode(sb, backing_dir_path.dentry);
2106 if (IS_ERR(root_inode)) {
2107 error = PTR_ERR(root_inode);
2108 goto err;
2109 }
2110
2111 sb->s_root = d_make_root(root_inode);
2112 if (!sb->s_root) {
2113 error = -ENOMEM;
2114 goto err;
2115 }
2116 error = incfs_init_dentry(sb->s_root, &backing_dir_path);
2117 if (error)
2118 goto err;
2119
2120 path_put(&backing_dir_path);
2121 sb->s_flags |= SB_ACTIVE;
2122
2123 pr_debug("infs: mount\n");
2124 return dget(sb->s_root);
2125 err:
2126 sb->s_fs_info = NULL;
2127 path_put(&backing_dir_path);
2128 incfs_free_mount_info(mi);
2129 deactivate_locked_super(sb);
2130 return ERR_PTR(error);
2131 }
2132
incfs_remount_fs(struct super_block * sb,int * flags,char * data)2133 static int incfs_remount_fs(struct super_block *sb, int *flags, char *data)
2134 {
2135 struct mount_options options;
2136 struct mount_info *mi = get_mount_info(sb);
2137 int err = 0;
2138
2139 sync_filesystem(sb);
2140 err = parse_options(&options, (char *)data);
2141 if (err)
2142 return err;
2143
2144 if (mi->mi_options.read_timeout_ms != options.read_timeout_ms) {
2145 mi->mi_options.read_timeout_ms = options.read_timeout_ms;
2146 pr_debug("incfs: new timeout_ms=%d", options.read_timeout_ms);
2147 }
2148
2149 pr_debug("infs: remount\n");
2150 return 0;
2151 }
2152
incfs_kill_sb(struct super_block * sb)2153 void incfs_kill_sb(struct super_block *sb)
2154 {
2155 struct mount_info *mi = sb->s_fs_info;
2156
2157 pr_debug("infs: unmount\n");
2158 incfs_free_mount_info(mi);
2159 generic_shutdown_super(sb);
2160 }
2161
show_options(struct seq_file * m,struct dentry * root)2162 static int show_options(struct seq_file *m, struct dentry *root)
2163 {
2164 struct mount_info *mi = get_mount_info(root->d_sb);
2165
2166 seq_printf(m, ",read_timeout_ms=%u", mi->mi_options.read_timeout_ms);
2167 seq_printf(m, ",readahead=%u", mi->mi_options.readahead_pages);
2168 if (mi->mi_options.read_log_pages != 0) {
2169 seq_printf(m, ",rlog_pages=%u", mi->mi_options.read_log_pages);
2170 seq_printf(m, ",rlog_wakeup_cnt=%u",
2171 mi->mi_options.read_log_wakeup_count);
2172 }
2173 if (mi->mi_options.no_backing_file_cache)
2174 seq_puts(m, ",no_bf_cache");
2175 if (mi->mi_options.no_backing_file_readahead)
2176 seq_puts(m, ",no_bf_readahead");
2177 return 0;
2178 }
2179