1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright 2018 Google LLC
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun #include <linux/blkdev.h>
7*4882a593Smuzhiyun #include <linux/compat.h>
8*4882a593Smuzhiyun #include <linux/file.h>
9*4882a593Smuzhiyun #include <linux/fs.h>
10*4882a593Smuzhiyun #include <linux/fs_stack.h>
11*4882a593Smuzhiyun #include <linux/fsnotify.h>
12*4882a593Smuzhiyun #include <linux/fsverity.h>
13*4882a593Smuzhiyun #include <linux/mmap_lock.h>
14*4882a593Smuzhiyun #include <linux/namei.h>
15*4882a593Smuzhiyun #include <linux/parser.h>
16*4882a593Smuzhiyun #include <linux/seq_file.h>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #include <uapi/linux/incrementalfs.h>
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #include "vfs.h"
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun #include "data_mgmt.h"
23*4882a593Smuzhiyun #include "format.h"
24*4882a593Smuzhiyun #include "internal.h"
25*4882a593Smuzhiyun #include "pseudo_files.h"
26*4882a593Smuzhiyun #include "sysfs.h"
27*4882a593Smuzhiyun #include "verity.h"
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun static int incfs_remount_fs(struct super_block *sb, int *flags, char *data);
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun static int dentry_revalidate(struct dentry *dentry, unsigned int flags);
32*4882a593Smuzhiyun static void dentry_release(struct dentry *d);
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun static int iterate_incfs_dir(struct file *file, struct dir_context *ctx);
35*4882a593Smuzhiyun static struct dentry *dir_lookup(struct inode *dir_inode,
36*4882a593Smuzhiyun struct dentry *dentry, unsigned int flags);
37*4882a593Smuzhiyun static int dir_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
38*4882a593Smuzhiyun static int dir_unlink(struct inode *dir, struct dentry *dentry);
39*4882a593Smuzhiyun static int dir_link(struct dentry *old_dentry, struct inode *dir,
40*4882a593Smuzhiyun struct dentry *new_dentry);
41*4882a593Smuzhiyun static int dir_rmdir(struct inode *dir, struct dentry *dentry);
42*4882a593Smuzhiyun static int dir_rename(struct inode *old_dir, struct dentry *old_dentry,
43*4882a593Smuzhiyun struct inode *new_dir, struct dentry *new_dentry);
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun static int file_open(struct inode *inode, struct file *file);
46*4882a593Smuzhiyun static int file_release(struct inode *inode, struct file *file);
47*4882a593Smuzhiyun static int read_single_page(struct file *f, struct page *page);
48*4882a593Smuzhiyun static long dispatch_ioctl(struct file *f, unsigned int req, unsigned long arg);
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
51*4882a593Smuzhiyun static long incfs_compat_ioctl(struct file *file, unsigned int cmd,
52*4882a593Smuzhiyun unsigned long arg);
53*4882a593Smuzhiyun #endif
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun static struct inode *alloc_inode(struct super_block *sb);
56*4882a593Smuzhiyun static void free_inode(struct inode *inode);
57*4882a593Smuzhiyun static void evict_inode(struct inode *inode);
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun static int incfs_setattr(struct dentry *dentry, struct iattr *ia);
60*4882a593Smuzhiyun static int incfs_getattr(const struct path *path,
61*4882a593Smuzhiyun struct kstat *stat, u32 request_mask,
62*4882a593Smuzhiyun unsigned int query_flags);
63*4882a593Smuzhiyun static ssize_t incfs_getxattr(struct dentry *d, const char *name,
64*4882a593Smuzhiyun void *value, size_t size);
65*4882a593Smuzhiyun static ssize_t incfs_setxattr(struct dentry *d, const char *name,
66*4882a593Smuzhiyun const void *value, size_t size, int flags);
67*4882a593Smuzhiyun static ssize_t incfs_listxattr(struct dentry *d, char *list, size_t size);
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun static int show_options(struct seq_file *, struct dentry *);
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun static const struct super_operations incfs_super_ops = {
72*4882a593Smuzhiyun .statfs = simple_statfs,
73*4882a593Smuzhiyun .remount_fs = incfs_remount_fs,
74*4882a593Smuzhiyun .alloc_inode = alloc_inode,
75*4882a593Smuzhiyun .destroy_inode = free_inode,
76*4882a593Smuzhiyun .evict_inode = evict_inode,
77*4882a593Smuzhiyun .show_options = show_options
78*4882a593Smuzhiyun };
79*4882a593Smuzhiyun
dir_rename_wrap(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)80*4882a593Smuzhiyun static int dir_rename_wrap(struct inode *old_dir, struct dentry *old_dentry,
81*4882a593Smuzhiyun struct inode *new_dir, struct dentry *new_dentry,
82*4882a593Smuzhiyun unsigned int flags)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun return dir_rename(old_dir, old_dentry, new_dir, new_dentry);
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun static const struct inode_operations incfs_dir_inode_ops = {
88*4882a593Smuzhiyun .lookup = dir_lookup,
89*4882a593Smuzhiyun .mkdir = dir_mkdir,
90*4882a593Smuzhiyun .rename = dir_rename_wrap,
91*4882a593Smuzhiyun .unlink = dir_unlink,
92*4882a593Smuzhiyun .link = dir_link,
93*4882a593Smuzhiyun .rmdir = dir_rmdir,
94*4882a593Smuzhiyun .setattr = incfs_setattr,
95*4882a593Smuzhiyun };
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun static const struct file_operations incfs_dir_fops = {
98*4882a593Smuzhiyun .llseek = generic_file_llseek,
99*4882a593Smuzhiyun .read = generic_read_dir,
100*4882a593Smuzhiyun .iterate = iterate_incfs_dir,
101*4882a593Smuzhiyun .open = file_open,
102*4882a593Smuzhiyun .release = file_release,
103*4882a593Smuzhiyun };
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun static const struct dentry_operations incfs_dentry_ops = {
106*4882a593Smuzhiyun .d_revalidate = dentry_revalidate,
107*4882a593Smuzhiyun .d_release = dentry_release
108*4882a593Smuzhiyun };
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun static const struct address_space_operations incfs_address_space_ops = {
111*4882a593Smuzhiyun .readpage = read_single_page,
112*4882a593Smuzhiyun /* .readpages = readpages */
113*4882a593Smuzhiyun };
114*4882a593Smuzhiyun
incfs_fault(struct vm_fault * vmf)115*4882a593Smuzhiyun static vm_fault_t incfs_fault(struct vm_fault *vmf)
116*4882a593Smuzhiyun {
117*4882a593Smuzhiyun vmf->flags &= ~FAULT_FLAG_ALLOW_RETRY;
118*4882a593Smuzhiyun return filemap_fault(vmf);
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun static const struct vm_operations_struct incfs_file_vm_ops = {
122*4882a593Smuzhiyun .fault = incfs_fault,
123*4882a593Smuzhiyun .map_pages = filemap_map_pages,
124*4882a593Smuzhiyun .page_mkwrite = filemap_page_mkwrite,
125*4882a593Smuzhiyun };
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun /* This is used for a general mmap of a disk file */
128*4882a593Smuzhiyun
incfs_file_mmap(struct file * file,struct vm_area_struct * vma)129*4882a593Smuzhiyun static int incfs_file_mmap(struct file *file, struct vm_area_struct *vma)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun struct address_space *mapping = file->f_mapping;
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun if (!mapping->a_ops->readpage)
134*4882a593Smuzhiyun return -ENOEXEC;
135*4882a593Smuzhiyun file_accessed(file);
136*4882a593Smuzhiyun vma->vm_ops = &incfs_file_vm_ops;
137*4882a593Smuzhiyun return 0;
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun const struct file_operations incfs_file_ops = {
141*4882a593Smuzhiyun .open = file_open,
142*4882a593Smuzhiyun .release = file_release,
143*4882a593Smuzhiyun .read_iter = generic_file_read_iter,
144*4882a593Smuzhiyun .mmap = incfs_file_mmap,
145*4882a593Smuzhiyun .splice_read = generic_file_splice_read,
146*4882a593Smuzhiyun .llseek = generic_file_llseek,
147*4882a593Smuzhiyun .unlocked_ioctl = dispatch_ioctl,
148*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
149*4882a593Smuzhiyun .compat_ioctl = incfs_compat_ioctl,
150*4882a593Smuzhiyun #endif
151*4882a593Smuzhiyun };
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun const struct inode_operations incfs_file_inode_ops = {
154*4882a593Smuzhiyun .setattr = incfs_setattr,
155*4882a593Smuzhiyun .getattr = incfs_getattr,
156*4882a593Smuzhiyun .listxattr = incfs_listxattr
157*4882a593Smuzhiyun };
158*4882a593Smuzhiyun
incfs_handler_getxattr(const struct xattr_handler * xh,struct dentry * d,struct inode * inode,const char * name,void * buffer,size_t size,int flags)159*4882a593Smuzhiyun static int incfs_handler_getxattr(const struct xattr_handler *xh,
160*4882a593Smuzhiyun struct dentry *d, struct inode *inode,
161*4882a593Smuzhiyun const char *name, void *buffer, size_t size,
162*4882a593Smuzhiyun int flags)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun return incfs_getxattr(d, name, buffer, size);
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun
incfs_handler_setxattr(const struct xattr_handler * xh,struct dentry * d,struct inode * inode,const char * name,const void * buffer,size_t size,int flags)167*4882a593Smuzhiyun static int incfs_handler_setxattr(const struct xattr_handler *xh,
168*4882a593Smuzhiyun struct dentry *d, struct inode *inode,
169*4882a593Smuzhiyun const char *name, const void *buffer,
170*4882a593Smuzhiyun size_t size, int flags)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun return incfs_setxattr(d, name, buffer, size, flags);
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun static const struct xattr_handler incfs_xattr_handler = {
176*4882a593Smuzhiyun .prefix = "", /* AKA all attributes */
177*4882a593Smuzhiyun .get = incfs_handler_getxattr,
178*4882a593Smuzhiyun .set = incfs_handler_setxattr,
179*4882a593Smuzhiyun };
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun static const struct xattr_handler *incfs_xattr_ops[] = {
182*4882a593Smuzhiyun &incfs_xattr_handler,
183*4882a593Smuzhiyun NULL,
184*4882a593Smuzhiyun };
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun struct inode_search {
187*4882a593Smuzhiyun unsigned long ino;
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun struct dentry *backing_dentry;
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun size_t size;
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun bool verity;
194*4882a593Smuzhiyun };
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun enum parse_parameter {
197*4882a593Smuzhiyun Opt_read_timeout,
198*4882a593Smuzhiyun Opt_readahead_pages,
199*4882a593Smuzhiyun Opt_rlog_pages,
200*4882a593Smuzhiyun Opt_rlog_wakeup_cnt,
201*4882a593Smuzhiyun Opt_report_uid,
202*4882a593Smuzhiyun Opt_sysfs_name,
203*4882a593Smuzhiyun Opt_err
204*4882a593Smuzhiyun };
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun static const match_table_t option_tokens = {
207*4882a593Smuzhiyun { Opt_read_timeout, "read_timeout_ms=%u" },
208*4882a593Smuzhiyun { Opt_readahead_pages, "readahead=%u" },
209*4882a593Smuzhiyun { Opt_rlog_pages, "rlog_pages=%u" },
210*4882a593Smuzhiyun { Opt_rlog_wakeup_cnt, "rlog_wakeup_cnt=%u" },
211*4882a593Smuzhiyun { Opt_report_uid, "report_uid" },
212*4882a593Smuzhiyun { Opt_sysfs_name, "sysfs_name=%s" },
213*4882a593Smuzhiyun { Opt_err, NULL }
214*4882a593Smuzhiyun };
215*4882a593Smuzhiyun
free_options(struct mount_options * opts)216*4882a593Smuzhiyun static void free_options(struct mount_options *opts)
217*4882a593Smuzhiyun {
218*4882a593Smuzhiyun kfree(opts->sysfs_name);
219*4882a593Smuzhiyun opts->sysfs_name = NULL;
220*4882a593Smuzhiyun }
221*4882a593Smuzhiyun
parse_options(struct mount_options * opts,char * str)222*4882a593Smuzhiyun static int parse_options(struct mount_options *opts, char *str)
223*4882a593Smuzhiyun {
224*4882a593Smuzhiyun substring_t args[MAX_OPT_ARGS];
225*4882a593Smuzhiyun int value;
226*4882a593Smuzhiyun char *position;
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun if (opts == NULL)
229*4882a593Smuzhiyun return -EFAULT;
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun *opts = (struct mount_options) {
232*4882a593Smuzhiyun .read_timeout_ms = 1000, /* Default: 1s */
233*4882a593Smuzhiyun .readahead_pages = 10,
234*4882a593Smuzhiyun .read_log_pages = 2,
235*4882a593Smuzhiyun .read_log_wakeup_count = 10,
236*4882a593Smuzhiyun };
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun if (str == NULL || *str == 0)
239*4882a593Smuzhiyun return 0;
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun while ((position = strsep(&str, ",")) != NULL) {
242*4882a593Smuzhiyun int token;
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun if (!*position)
245*4882a593Smuzhiyun continue;
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun token = match_token(position, option_tokens, args);
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun switch (token) {
250*4882a593Smuzhiyun case Opt_read_timeout:
251*4882a593Smuzhiyun if (match_int(&args[0], &value))
252*4882a593Smuzhiyun return -EINVAL;
253*4882a593Smuzhiyun if (value > 3600000)
254*4882a593Smuzhiyun return -EINVAL;
255*4882a593Smuzhiyun opts->read_timeout_ms = value;
256*4882a593Smuzhiyun break;
257*4882a593Smuzhiyun case Opt_readahead_pages:
258*4882a593Smuzhiyun if (match_int(&args[0], &value))
259*4882a593Smuzhiyun return -EINVAL;
260*4882a593Smuzhiyun opts->readahead_pages = value;
261*4882a593Smuzhiyun break;
262*4882a593Smuzhiyun case Opt_rlog_pages:
263*4882a593Smuzhiyun if (match_int(&args[0], &value))
264*4882a593Smuzhiyun return -EINVAL;
265*4882a593Smuzhiyun opts->read_log_pages = value;
266*4882a593Smuzhiyun break;
267*4882a593Smuzhiyun case Opt_rlog_wakeup_cnt:
268*4882a593Smuzhiyun if (match_int(&args[0], &value))
269*4882a593Smuzhiyun return -EINVAL;
270*4882a593Smuzhiyun opts->read_log_wakeup_count = value;
271*4882a593Smuzhiyun break;
272*4882a593Smuzhiyun case Opt_report_uid:
273*4882a593Smuzhiyun opts->report_uid = true;
274*4882a593Smuzhiyun break;
275*4882a593Smuzhiyun case Opt_sysfs_name:
276*4882a593Smuzhiyun opts->sysfs_name = match_strdup(&args[0]);
277*4882a593Smuzhiyun break;
278*4882a593Smuzhiyun default:
279*4882a593Smuzhiyun free_options(opts);
280*4882a593Smuzhiyun return -EINVAL;
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun return 0;
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun /* Read file size from the attribute. Quicker than reading the header */
read_size_attr(struct dentry * backing_dentry)288*4882a593Smuzhiyun static u64 read_size_attr(struct dentry *backing_dentry)
289*4882a593Smuzhiyun {
290*4882a593Smuzhiyun __le64 attr_value;
291*4882a593Smuzhiyun ssize_t bytes_read;
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun bytes_read = vfs_getxattr(backing_dentry, INCFS_XATTR_SIZE_NAME,
294*4882a593Smuzhiyun (char *)&attr_value, sizeof(attr_value));
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun if (bytes_read != sizeof(attr_value))
297*4882a593Smuzhiyun return 0;
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun return le64_to_cpu(attr_value);
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun /* Read verity flag from the attribute. Quicker than reading the header */
read_verity_attr(struct dentry * backing_dentry)303*4882a593Smuzhiyun static bool read_verity_attr(struct dentry *backing_dentry)
304*4882a593Smuzhiyun {
305*4882a593Smuzhiyun return vfs_getxattr(backing_dentry, INCFS_XATTR_VERITY_NAME, NULL, 0)
306*4882a593Smuzhiyun >= 0;
307*4882a593Smuzhiyun }
308*4882a593Smuzhiyun
inode_test(struct inode * inode,void * opaque)309*4882a593Smuzhiyun static int inode_test(struct inode *inode, void *opaque)
310*4882a593Smuzhiyun {
311*4882a593Smuzhiyun struct inode_search *search = opaque;
312*4882a593Smuzhiyun struct inode_info *node = get_incfs_node(inode);
313*4882a593Smuzhiyun struct inode *backing_inode = d_inode(search->backing_dentry);
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun if (!node)
316*4882a593Smuzhiyun return 0;
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun return node->n_backing_inode == backing_inode &&
319*4882a593Smuzhiyun inode->i_ino == search->ino;
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun
inode_set(struct inode * inode,void * opaque)322*4882a593Smuzhiyun static int inode_set(struct inode *inode, void *opaque)
323*4882a593Smuzhiyun {
324*4882a593Smuzhiyun struct inode_search *search = opaque;
325*4882a593Smuzhiyun struct inode_info *node = get_incfs_node(inode);
326*4882a593Smuzhiyun struct dentry *backing_dentry = search->backing_dentry;
327*4882a593Smuzhiyun struct inode *backing_inode = d_inode(backing_dentry);
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun fsstack_copy_attr_all(inode, backing_inode);
330*4882a593Smuzhiyun if (S_ISREG(inode->i_mode)) {
331*4882a593Smuzhiyun u64 size = search->size;
332*4882a593Smuzhiyun
333*4882a593Smuzhiyun inode->i_size = size;
334*4882a593Smuzhiyun inode->i_blocks = get_blocks_count_for_size(size);
335*4882a593Smuzhiyun inode->i_mapping->a_ops = &incfs_address_space_ops;
336*4882a593Smuzhiyun inode->i_op = &incfs_file_inode_ops;
337*4882a593Smuzhiyun inode->i_fop = &incfs_file_ops;
338*4882a593Smuzhiyun inode->i_mode &= ~0222;
339*4882a593Smuzhiyun if (search->verity)
340*4882a593Smuzhiyun inode_set_flags(inode, S_VERITY, S_VERITY);
341*4882a593Smuzhiyun } else if (S_ISDIR(inode->i_mode)) {
342*4882a593Smuzhiyun inode->i_size = 0;
343*4882a593Smuzhiyun inode->i_blocks = 1;
344*4882a593Smuzhiyun inode->i_mapping->a_ops = &incfs_address_space_ops;
345*4882a593Smuzhiyun inode->i_op = &incfs_dir_inode_ops;
346*4882a593Smuzhiyun inode->i_fop = &incfs_dir_fops;
347*4882a593Smuzhiyun } else {
348*4882a593Smuzhiyun pr_warn_once("incfs: Unexpected inode type\n");
349*4882a593Smuzhiyun return -EBADF;
350*4882a593Smuzhiyun }
351*4882a593Smuzhiyun
352*4882a593Smuzhiyun ihold(backing_inode);
353*4882a593Smuzhiyun node->n_backing_inode = backing_inode;
354*4882a593Smuzhiyun node->n_mount_info = get_mount_info(inode->i_sb);
355*4882a593Smuzhiyun inode->i_ctime = backing_inode->i_ctime;
356*4882a593Smuzhiyun inode->i_mtime = backing_inode->i_mtime;
357*4882a593Smuzhiyun inode->i_atime = backing_inode->i_atime;
358*4882a593Smuzhiyun inode->i_ino = backing_inode->i_ino;
359*4882a593Smuzhiyun if (backing_inode->i_ino < INCFS_START_INO_RANGE) {
360*4882a593Smuzhiyun pr_warn("incfs: ino conflict with backing FS %ld\n",
361*4882a593Smuzhiyun backing_inode->i_ino);
362*4882a593Smuzhiyun }
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun return 0;
365*4882a593Smuzhiyun }
366*4882a593Smuzhiyun
fetch_regular_inode(struct super_block * sb,struct dentry * backing_dentry)367*4882a593Smuzhiyun static struct inode *fetch_regular_inode(struct super_block *sb,
368*4882a593Smuzhiyun struct dentry *backing_dentry)
369*4882a593Smuzhiyun {
370*4882a593Smuzhiyun struct inode *backing_inode = d_inode(backing_dentry);
371*4882a593Smuzhiyun struct inode_search search = {
372*4882a593Smuzhiyun .ino = backing_inode->i_ino,
373*4882a593Smuzhiyun .backing_dentry = backing_dentry,
374*4882a593Smuzhiyun .size = read_size_attr(backing_dentry),
375*4882a593Smuzhiyun .verity = read_verity_attr(backing_dentry),
376*4882a593Smuzhiyun };
377*4882a593Smuzhiyun struct inode *inode = iget5_locked(sb, search.ino, inode_test,
378*4882a593Smuzhiyun inode_set, &search);
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun if (!inode)
381*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun if (inode->i_state & I_NEW)
384*4882a593Smuzhiyun unlock_new_inode(inode);
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun return inode;
387*4882a593Smuzhiyun }
388*4882a593Smuzhiyun
iterate_incfs_dir(struct file * file,struct dir_context * ctx)389*4882a593Smuzhiyun static int iterate_incfs_dir(struct file *file, struct dir_context *ctx)
390*4882a593Smuzhiyun {
391*4882a593Smuzhiyun struct dir_file *dir = get_incfs_dir_file(file);
392*4882a593Smuzhiyun int error = 0;
393*4882a593Smuzhiyun struct mount_info *mi = get_mount_info(file_superblock(file));
394*4882a593Smuzhiyun bool root;
395*4882a593Smuzhiyun
396*4882a593Smuzhiyun if (!dir) {
397*4882a593Smuzhiyun error = -EBADF;
398*4882a593Smuzhiyun goto out;
399*4882a593Smuzhiyun }
400*4882a593Smuzhiyun
401*4882a593Smuzhiyun root = dir->backing_dir->f_inode
402*4882a593Smuzhiyun == d_inode(mi->mi_backing_dir_path.dentry);
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun if (root) {
405*4882a593Smuzhiyun error = emit_pseudo_files(ctx);
406*4882a593Smuzhiyun if (error)
407*4882a593Smuzhiyun goto out;
408*4882a593Smuzhiyun }
409*4882a593Smuzhiyun
410*4882a593Smuzhiyun ctx->pos -= PSEUDO_FILE_COUNT;
411*4882a593Smuzhiyun error = iterate_dir(dir->backing_dir, ctx);
412*4882a593Smuzhiyun ctx->pos += PSEUDO_FILE_COUNT;
413*4882a593Smuzhiyun file->f_pos = dir->backing_dir->f_pos;
414*4882a593Smuzhiyun out:
415*4882a593Smuzhiyun if (error)
416*4882a593Smuzhiyun pr_warn("incfs: %s %s %d\n", __func__,
417*4882a593Smuzhiyun file->f_path.dentry->d_name.name, error);
418*4882a593Smuzhiyun return error;
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun
incfs_init_dentry(struct dentry * dentry,struct path * path)421*4882a593Smuzhiyun static int incfs_init_dentry(struct dentry *dentry, struct path *path)
422*4882a593Smuzhiyun {
423*4882a593Smuzhiyun struct dentry_info *d_info = NULL;
424*4882a593Smuzhiyun
425*4882a593Smuzhiyun if (!dentry || !path)
426*4882a593Smuzhiyun return -EFAULT;
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun d_info = kzalloc(sizeof(*d_info), GFP_NOFS);
429*4882a593Smuzhiyun if (!d_info)
430*4882a593Smuzhiyun return -ENOMEM;
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun d_info->backing_path = *path;
433*4882a593Smuzhiyun path_get(path);
434*4882a593Smuzhiyun
435*4882a593Smuzhiyun dentry->d_fsdata = d_info;
436*4882a593Smuzhiyun return 0;
437*4882a593Smuzhiyun }
438*4882a593Smuzhiyun
open_or_create_special_dir(struct dentry * backing_dir,const char * name,bool * created)439*4882a593Smuzhiyun static struct dentry *open_or_create_special_dir(struct dentry *backing_dir,
440*4882a593Smuzhiyun const char *name,
441*4882a593Smuzhiyun bool *created)
442*4882a593Smuzhiyun {
443*4882a593Smuzhiyun struct dentry *index_dentry;
444*4882a593Smuzhiyun struct inode *backing_inode = d_inode(backing_dir);
445*4882a593Smuzhiyun int err = 0;
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun index_dentry = incfs_lookup_dentry(backing_dir, name);
448*4882a593Smuzhiyun if (!index_dentry) {
449*4882a593Smuzhiyun return ERR_PTR(-EINVAL);
450*4882a593Smuzhiyun } else if (IS_ERR(index_dentry)) {
451*4882a593Smuzhiyun return index_dentry;
452*4882a593Smuzhiyun } else if (d_really_is_positive(index_dentry)) {
453*4882a593Smuzhiyun /* Index already exists. */
454*4882a593Smuzhiyun *created = false;
455*4882a593Smuzhiyun return index_dentry;
456*4882a593Smuzhiyun }
457*4882a593Smuzhiyun
458*4882a593Smuzhiyun /* Index needs to be created. */
459*4882a593Smuzhiyun inode_lock_nested(backing_inode, I_MUTEX_PARENT);
460*4882a593Smuzhiyun err = vfs_mkdir(backing_inode, index_dentry, 0777);
461*4882a593Smuzhiyun inode_unlock(backing_inode);
462*4882a593Smuzhiyun
463*4882a593Smuzhiyun if (err) {
464*4882a593Smuzhiyun dput(index_dentry);
465*4882a593Smuzhiyun return ERR_PTR(err);
466*4882a593Smuzhiyun }
467*4882a593Smuzhiyun
468*4882a593Smuzhiyun if (!d_really_is_positive(index_dentry) ||
469*4882a593Smuzhiyun unlikely(d_unhashed(index_dentry))) {
470*4882a593Smuzhiyun dput(index_dentry);
471*4882a593Smuzhiyun return ERR_PTR(-EINVAL);
472*4882a593Smuzhiyun }
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun *created = true;
475*4882a593Smuzhiyun return index_dentry;
476*4882a593Smuzhiyun }
477*4882a593Smuzhiyun
read_single_page_timeouts(struct data_file * df,struct file * f,int block_index,struct mem_range range,struct mem_range tmp)478*4882a593Smuzhiyun static int read_single_page_timeouts(struct data_file *df, struct file *f,
479*4882a593Smuzhiyun int block_index, struct mem_range range,
480*4882a593Smuzhiyun struct mem_range tmp)
481*4882a593Smuzhiyun {
482*4882a593Smuzhiyun struct mount_info *mi = df->df_mount_info;
483*4882a593Smuzhiyun struct incfs_read_data_file_timeouts timeouts = {
484*4882a593Smuzhiyun .max_pending_time_us = U32_MAX,
485*4882a593Smuzhiyun };
486*4882a593Smuzhiyun int uid = current_uid().val;
487*4882a593Smuzhiyun int i;
488*4882a593Smuzhiyun
489*4882a593Smuzhiyun spin_lock(&mi->mi_per_uid_read_timeouts_lock);
490*4882a593Smuzhiyun for (i = 0; i < mi->mi_per_uid_read_timeouts_size /
491*4882a593Smuzhiyun sizeof(*mi->mi_per_uid_read_timeouts); ++i) {
492*4882a593Smuzhiyun struct incfs_per_uid_read_timeouts *t =
493*4882a593Smuzhiyun &mi->mi_per_uid_read_timeouts[i];
494*4882a593Smuzhiyun
495*4882a593Smuzhiyun if(t->uid == uid) {
496*4882a593Smuzhiyun timeouts.min_time_us = t->min_time_us;
497*4882a593Smuzhiyun timeouts.min_pending_time_us = t->min_pending_time_us;
498*4882a593Smuzhiyun timeouts.max_pending_time_us = t->max_pending_time_us;
499*4882a593Smuzhiyun break;
500*4882a593Smuzhiyun }
501*4882a593Smuzhiyun }
502*4882a593Smuzhiyun spin_unlock(&mi->mi_per_uid_read_timeouts_lock);
503*4882a593Smuzhiyun if (timeouts.max_pending_time_us == U32_MAX) {
504*4882a593Smuzhiyun u64 read_timeout_us = (u64)mi->mi_options.read_timeout_ms *
505*4882a593Smuzhiyun 1000;
506*4882a593Smuzhiyun
507*4882a593Smuzhiyun timeouts.max_pending_time_us = read_timeout_us <= U32_MAX ?
508*4882a593Smuzhiyun read_timeout_us : U32_MAX;
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun return incfs_read_data_file_block(range, f, block_index, tmp,
512*4882a593Smuzhiyun &timeouts);
513*4882a593Smuzhiyun }
514*4882a593Smuzhiyun
read_single_page(struct file * f,struct page * page)515*4882a593Smuzhiyun static int read_single_page(struct file *f, struct page *page)
516*4882a593Smuzhiyun {
517*4882a593Smuzhiyun loff_t offset = 0;
518*4882a593Smuzhiyun loff_t size = 0;
519*4882a593Smuzhiyun ssize_t bytes_to_read = 0;
520*4882a593Smuzhiyun ssize_t read_result = 0;
521*4882a593Smuzhiyun struct data_file *df = get_incfs_data_file(f);
522*4882a593Smuzhiyun int result = 0;
523*4882a593Smuzhiyun void *page_start;
524*4882a593Smuzhiyun int block_index;
525*4882a593Smuzhiyun
526*4882a593Smuzhiyun if (!df) {
527*4882a593Smuzhiyun SetPageError(page);
528*4882a593Smuzhiyun unlock_page(page);
529*4882a593Smuzhiyun return -EBADF;
530*4882a593Smuzhiyun }
531*4882a593Smuzhiyun
532*4882a593Smuzhiyun page_start = kmap(page);
533*4882a593Smuzhiyun offset = page_offset(page);
534*4882a593Smuzhiyun block_index = (offset + df->df_mapped_offset) /
535*4882a593Smuzhiyun INCFS_DATA_FILE_BLOCK_SIZE;
536*4882a593Smuzhiyun size = df->df_size;
537*4882a593Smuzhiyun
538*4882a593Smuzhiyun if (offset < size) {
539*4882a593Smuzhiyun struct mem_range tmp = {
540*4882a593Smuzhiyun .len = 2 * INCFS_DATA_FILE_BLOCK_SIZE
541*4882a593Smuzhiyun };
542*4882a593Smuzhiyun tmp.data = (u8 *)__get_free_pages(GFP_NOFS, get_order(tmp.len));
543*4882a593Smuzhiyun if (!tmp.data) {
544*4882a593Smuzhiyun read_result = -ENOMEM;
545*4882a593Smuzhiyun goto err;
546*4882a593Smuzhiyun }
547*4882a593Smuzhiyun bytes_to_read = min_t(loff_t, size - offset, PAGE_SIZE);
548*4882a593Smuzhiyun
549*4882a593Smuzhiyun read_result = read_single_page_timeouts(df, f, block_index,
550*4882a593Smuzhiyun range(page_start, bytes_to_read), tmp);
551*4882a593Smuzhiyun
552*4882a593Smuzhiyun free_pages((unsigned long)tmp.data, get_order(tmp.len));
553*4882a593Smuzhiyun } else {
554*4882a593Smuzhiyun bytes_to_read = 0;
555*4882a593Smuzhiyun read_result = 0;
556*4882a593Smuzhiyun }
557*4882a593Smuzhiyun
558*4882a593Smuzhiyun err:
559*4882a593Smuzhiyun if (read_result < 0)
560*4882a593Smuzhiyun result = read_result;
561*4882a593Smuzhiyun else if (read_result < PAGE_SIZE)
562*4882a593Smuzhiyun zero_user(page, read_result, PAGE_SIZE - read_result);
563*4882a593Smuzhiyun
564*4882a593Smuzhiyun if (result == 0)
565*4882a593Smuzhiyun SetPageUptodate(page);
566*4882a593Smuzhiyun else
567*4882a593Smuzhiyun SetPageError(page);
568*4882a593Smuzhiyun
569*4882a593Smuzhiyun flush_dcache_page(page);
570*4882a593Smuzhiyun kunmap(page);
571*4882a593Smuzhiyun unlock_page(page);
572*4882a593Smuzhiyun return result;
573*4882a593Smuzhiyun }
574*4882a593Smuzhiyun
incfs_link(struct dentry * what,struct dentry * where)575*4882a593Smuzhiyun int incfs_link(struct dentry *what, struct dentry *where)
576*4882a593Smuzhiyun {
577*4882a593Smuzhiyun struct dentry *parent_dentry = dget_parent(where);
578*4882a593Smuzhiyun struct inode *pinode = d_inode(parent_dentry);
579*4882a593Smuzhiyun int error = 0;
580*4882a593Smuzhiyun
581*4882a593Smuzhiyun inode_lock_nested(pinode, I_MUTEX_PARENT);
582*4882a593Smuzhiyun error = vfs_link(what, pinode, where, NULL);
583*4882a593Smuzhiyun inode_unlock(pinode);
584*4882a593Smuzhiyun
585*4882a593Smuzhiyun dput(parent_dentry);
586*4882a593Smuzhiyun return error;
587*4882a593Smuzhiyun }
588*4882a593Smuzhiyun
incfs_unlink(struct dentry * dentry)589*4882a593Smuzhiyun int incfs_unlink(struct dentry *dentry)
590*4882a593Smuzhiyun {
591*4882a593Smuzhiyun struct dentry *parent_dentry = dget_parent(dentry);
592*4882a593Smuzhiyun struct inode *pinode = d_inode(parent_dentry);
593*4882a593Smuzhiyun int error = 0;
594*4882a593Smuzhiyun
595*4882a593Smuzhiyun inode_lock_nested(pinode, I_MUTEX_PARENT);
596*4882a593Smuzhiyun error = vfs_unlink(pinode, dentry, NULL);
597*4882a593Smuzhiyun inode_unlock(pinode);
598*4882a593Smuzhiyun
599*4882a593Smuzhiyun dput(parent_dentry);
600*4882a593Smuzhiyun return error;
601*4882a593Smuzhiyun }
602*4882a593Smuzhiyun
incfs_rmdir(struct dentry * dentry)603*4882a593Smuzhiyun static int incfs_rmdir(struct dentry *dentry)
604*4882a593Smuzhiyun {
605*4882a593Smuzhiyun struct dentry *parent_dentry = dget_parent(dentry);
606*4882a593Smuzhiyun struct inode *pinode = d_inode(parent_dentry);
607*4882a593Smuzhiyun int error = 0;
608*4882a593Smuzhiyun
609*4882a593Smuzhiyun inode_lock_nested(pinode, I_MUTEX_PARENT);
610*4882a593Smuzhiyun error = vfs_rmdir(pinode, dentry);
611*4882a593Smuzhiyun inode_unlock(pinode);
612*4882a593Smuzhiyun
613*4882a593Smuzhiyun dput(parent_dentry);
614*4882a593Smuzhiyun return error;
615*4882a593Smuzhiyun }
616*4882a593Smuzhiyun
notify_unlink(struct dentry * dentry,const char * file_id_str,const char * special_directory)617*4882a593Smuzhiyun static void notify_unlink(struct dentry *dentry, const char *file_id_str,
618*4882a593Smuzhiyun const char *special_directory)
619*4882a593Smuzhiyun {
620*4882a593Smuzhiyun struct dentry *root = dentry;
621*4882a593Smuzhiyun struct dentry *file = NULL;
622*4882a593Smuzhiyun struct dentry *dir = NULL;
623*4882a593Smuzhiyun int error = 0;
624*4882a593Smuzhiyun bool take_lock = root->d_parent != root->d_parent->d_parent;
625*4882a593Smuzhiyun
626*4882a593Smuzhiyun while (root != root->d_parent)
627*4882a593Smuzhiyun root = root->d_parent;
628*4882a593Smuzhiyun
629*4882a593Smuzhiyun if (take_lock)
630*4882a593Smuzhiyun dir = incfs_lookup_dentry(root, special_directory);
631*4882a593Smuzhiyun else
632*4882a593Smuzhiyun dir = lookup_one_len(special_directory, root,
633*4882a593Smuzhiyun strlen(special_directory));
634*4882a593Smuzhiyun
635*4882a593Smuzhiyun if (IS_ERR(dir)) {
636*4882a593Smuzhiyun error = PTR_ERR(dir);
637*4882a593Smuzhiyun goto out;
638*4882a593Smuzhiyun }
639*4882a593Smuzhiyun if (d_is_negative(dir)) {
640*4882a593Smuzhiyun error = -ENOENT;
641*4882a593Smuzhiyun goto out;
642*4882a593Smuzhiyun }
643*4882a593Smuzhiyun
644*4882a593Smuzhiyun file = incfs_lookup_dentry(dir, file_id_str);
645*4882a593Smuzhiyun if (IS_ERR(file)) {
646*4882a593Smuzhiyun error = PTR_ERR(file);
647*4882a593Smuzhiyun goto out;
648*4882a593Smuzhiyun }
649*4882a593Smuzhiyun if (d_is_negative(file)) {
650*4882a593Smuzhiyun error = -ENOENT;
651*4882a593Smuzhiyun goto out;
652*4882a593Smuzhiyun }
653*4882a593Smuzhiyun
654*4882a593Smuzhiyun fsnotify_unlink(d_inode(dir), file);
655*4882a593Smuzhiyun d_delete(file);
656*4882a593Smuzhiyun
657*4882a593Smuzhiyun out:
658*4882a593Smuzhiyun if (error)
659*4882a593Smuzhiyun pr_warn("%s failed with error %d\n", __func__, error);
660*4882a593Smuzhiyun
661*4882a593Smuzhiyun dput(dir);
662*4882a593Smuzhiyun dput(file);
663*4882a593Smuzhiyun }
664*4882a593Smuzhiyun
maybe_delete_incomplete_file(struct file * f,struct data_file * df)665*4882a593Smuzhiyun static void maybe_delete_incomplete_file(struct file *f,
666*4882a593Smuzhiyun struct data_file *df)
667*4882a593Smuzhiyun {
668*4882a593Smuzhiyun struct backing_file_context *bfc;
669*4882a593Smuzhiyun struct mount_info *mi = df->df_mount_info;
670*4882a593Smuzhiyun char *file_id_str = NULL;
671*4882a593Smuzhiyun struct dentry *incomplete_file_dentry = NULL;
672*4882a593Smuzhiyun const struct cred *old_cred = override_creds(mi->mi_owner);
673*4882a593Smuzhiyun int error;
674*4882a593Smuzhiyun
675*4882a593Smuzhiyun if (atomic_read(&df->df_data_blocks_written) < df->df_data_block_count)
676*4882a593Smuzhiyun goto out;
677*4882a593Smuzhiyun
678*4882a593Smuzhiyun /* Truncate file to remove any preallocated space */
679*4882a593Smuzhiyun bfc = df->df_backing_file_context;
680*4882a593Smuzhiyun if (bfc) {
681*4882a593Smuzhiyun struct file *f = bfc->bc_file;
682*4882a593Smuzhiyun
683*4882a593Smuzhiyun if (f) {
684*4882a593Smuzhiyun loff_t size = i_size_read(file_inode(f));
685*4882a593Smuzhiyun
686*4882a593Smuzhiyun error = vfs_truncate(&f->f_path, size);
687*4882a593Smuzhiyun if (error)
688*4882a593Smuzhiyun /* No useful action on failure */
689*4882a593Smuzhiyun pr_warn("incfs: Failed to truncate complete file: %d\n",
690*4882a593Smuzhiyun error);
691*4882a593Smuzhiyun }
692*4882a593Smuzhiyun }
693*4882a593Smuzhiyun
694*4882a593Smuzhiyun /* This is best effort - there is no useful action to take on failure */
695*4882a593Smuzhiyun file_id_str = file_id_to_str(df->df_id);
696*4882a593Smuzhiyun if (!file_id_str)
697*4882a593Smuzhiyun goto out;
698*4882a593Smuzhiyun
699*4882a593Smuzhiyun incomplete_file_dentry = incfs_lookup_dentry(
700*4882a593Smuzhiyun df->df_mount_info->mi_incomplete_dir,
701*4882a593Smuzhiyun file_id_str);
702*4882a593Smuzhiyun if (!incomplete_file_dentry || IS_ERR(incomplete_file_dentry)) {
703*4882a593Smuzhiyun incomplete_file_dentry = NULL;
704*4882a593Smuzhiyun goto out;
705*4882a593Smuzhiyun }
706*4882a593Smuzhiyun
707*4882a593Smuzhiyun if (!d_really_is_positive(incomplete_file_dentry))
708*4882a593Smuzhiyun goto out;
709*4882a593Smuzhiyun
710*4882a593Smuzhiyun vfs_fsync(df->df_backing_file_context->bc_file, 0);
711*4882a593Smuzhiyun error = incfs_unlink(incomplete_file_dentry);
712*4882a593Smuzhiyun if (error) {
713*4882a593Smuzhiyun pr_warn("incfs: Deleting incomplete file failed: %d\n", error);
714*4882a593Smuzhiyun goto out;
715*4882a593Smuzhiyun }
716*4882a593Smuzhiyun
717*4882a593Smuzhiyun notify_unlink(f->f_path.dentry, file_id_str, INCFS_INCOMPLETE_NAME);
718*4882a593Smuzhiyun
719*4882a593Smuzhiyun out:
720*4882a593Smuzhiyun dput(incomplete_file_dentry);
721*4882a593Smuzhiyun kfree(file_id_str);
722*4882a593Smuzhiyun revert_creds(old_cred);
723*4882a593Smuzhiyun }
724*4882a593Smuzhiyun
ioctl_fill_blocks(struct file * f,void __user * arg)725*4882a593Smuzhiyun static long ioctl_fill_blocks(struct file *f, void __user *arg)
726*4882a593Smuzhiyun {
727*4882a593Smuzhiyun struct incfs_fill_blocks __user *usr_fill_blocks = arg;
728*4882a593Smuzhiyun struct incfs_fill_blocks fill_blocks;
729*4882a593Smuzhiyun struct incfs_fill_block __user *usr_fill_block_array;
730*4882a593Smuzhiyun struct data_file *df = get_incfs_data_file(f);
731*4882a593Smuzhiyun struct incfs_file_data *fd = f->private_data;
732*4882a593Smuzhiyun const ssize_t data_buf_size = 2 * INCFS_DATA_FILE_BLOCK_SIZE;
733*4882a593Smuzhiyun u8 *data_buf = NULL;
734*4882a593Smuzhiyun ssize_t error = 0;
735*4882a593Smuzhiyun int i = 0;
736*4882a593Smuzhiyun
737*4882a593Smuzhiyun if (!df)
738*4882a593Smuzhiyun return -EBADF;
739*4882a593Smuzhiyun
740*4882a593Smuzhiyun if (!fd || fd->fd_fill_permission != CAN_FILL)
741*4882a593Smuzhiyun return -EPERM;
742*4882a593Smuzhiyun
743*4882a593Smuzhiyun if (copy_from_user(&fill_blocks, usr_fill_blocks, sizeof(fill_blocks)))
744*4882a593Smuzhiyun return -EFAULT;
745*4882a593Smuzhiyun
746*4882a593Smuzhiyun usr_fill_block_array = u64_to_user_ptr(fill_blocks.fill_blocks);
747*4882a593Smuzhiyun data_buf = (u8 *)__get_free_pages(GFP_NOFS | __GFP_COMP,
748*4882a593Smuzhiyun get_order(data_buf_size));
749*4882a593Smuzhiyun if (!data_buf)
750*4882a593Smuzhiyun return -ENOMEM;
751*4882a593Smuzhiyun
752*4882a593Smuzhiyun for (i = 0; i < fill_blocks.count; i++) {
753*4882a593Smuzhiyun struct incfs_fill_block fill_block = {};
754*4882a593Smuzhiyun
755*4882a593Smuzhiyun if (copy_from_user(&fill_block, &usr_fill_block_array[i],
756*4882a593Smuzhiyun sizeof(fill_block)) > 0) {
757*4882a593Smuzhiyun error = -EFAULT;
758*4882a593Smuzhiyun break;
759*4882a593Smuzhiyun }
760*4882a593Smuzhiyun
761*4882a593Smuzhiyun if (fill_block.data_len > data_buf_size) {
762*4882a593Smuzhiyun error = -E2BIG;
763*4882a593Smuzhiyun break;
764*4882a593Smuzhiyun }
765*4882a593Smuzhiyun
766*4882a593Smuzhiyun if (copy_from_user(data_buf, u64_to_user_ptr(fill_block.data),
767*4882a593Smuzhiyun fill_block.data_len) > 0) {
768*4882a593Smuzhiyun error = -EFAULT;
769*4882a593Smuzhiyun break;
770*4882a593Smuzhiyun }
771*4882a593Smuzhiyun fill_block.data = 0; /* To make sure nobody uses it. */
772*4882a593Smuzhiyun if (fill_block.flags & INCFS_BLOCK_FLAGS_HASH) {
773*4882a593Smuzhiyun error = incfs_process_new_hash_block(df, &fill_block,
774*4882a593Smuzhiyun data_buf);
775*4882a593Smuzhiyun } else {
776*4882a593Smuzhiyun error = incfs_process_new_data_block(df, &fill_block,
777*4882a593Smuzhiyun data_buf);
778*4882a593Smuzhiyun }
779*4882a593Smuzhiyun if (error)
780*4882a593Smuzhiyun break;
781*4882a593Smuzhiyun }
782*4882a593Smuzhiyun
783*4882a593Smuzhiyun if (data_buf)
784*4882a593Smuzhiyun free_pages((unsigned long)data_buf, get_order(data_buf_size));
785*4882a593Smuzhiyun
786*4882a593Smuzhiyun maybe_delete_incomplete_file(f, df);
787*4882a593Smuzhiyun
788*4882a593Smuzhiyun /*
789*4882a593Smuzhiyun * Only report the error if no records were processed, otherwise
790*4882a593Smuzhiyun * just return how many were processed successfully.
791*4882a593Smuzhiyun */
792*4882a593Smuzhiyun if (i == 0)
793*4882a593Smuzhiyun return error;
794*4882a593Smuzhiyun
795*4882a593Smuzhiyun return i;
796*4882a593Smuzhiyun }
797*4882a593Smuzhiyun
ioctl_read_file_signature(struct file * f,void __user * arg)798*4882a593Smuzhiyun static long ioctl_read_file_signature(struct file *f, void __user *arg)
799*4882a593Smuzhiyun {
800*4882a593Smuzhiyun struct incfs_get_file_sig_args __user *args_usr_ptr = arg;
801*4882a593Smuzhiyun struct incfs_get_file_sig_args args = {};
802*4882a593Smuzhiyun u8 *sig_buffer = NULL;
803*4882a593Smuzhiyun size_t sig_buf_size = 0;
804*4882a593Smuzhiyun int error = 0;
805*4882a593Smuzhiyun int read_result = 0;
806*4882a593Smuzhiyun struct data_file *df = get_incfs_data_file(f);
807*4882a593Smuzhiyun
808*4882a593Smuzhiyun if (!df)
809*4882a593Smuzhiyun return -EINVAL;
810*4882a593Smuzhiyun
811*4882a593Smuzhiyun if (copy_from_user(&args, args_usr_ptr, sizeof(args)) > 0)
812*4882a593Smuzhiyun return -EINVAL;
813*4882a593Smuzhiyun
814*4882a593Smuzhiyun sig_buf_size = args.file_signature_buf_size;
815*4882a593Smuzhiyun if (sig_buf_size > INCFS_MAX_SIGNATURE_SIZE)
816*4882a593Smuzhiyun return -E2BIG;
817*4882a593Smuzhiyun
818*4882a593Smuzhiyun sig_buffer = kzalloc(sig_buf_size, GFP_NOFS | __GFP_COMP);
819*4882a593Smuzhiyun if (!sig_buffer)
820*4882a593Smuzhiyun return -ENOMEM;
821*4882a593Smuzhiyun
822*4882a593Smuzhiyun read_result = incfs_read_file_signature(df,
823*4882a593Smuzhiyun range(sig_buffer, sig_buf_size));
824*4882a593Smuzhiyun
825*4882a593Smuzhiyun if (read_result < 0) {
826*4882a593Smuzhiyun error = read_result;
827*4882a593Smuzhiyun goto out;
828*4882a593Smuzhiyun }
829*4882a593Smuzhiyun
830*4882a593Smuzhiyun if (copy_to_user(u64_to_user_ptr(args.file_signature), sig_buffer,
831*4882a593Smuzhiyun read_result)) {
832*4882a593Smuzhiyun error = -EFAULT;
833*4882a593Smuzhiyun goto out;
834*4882a593Smuzhiyun }
835*4882a593Smuzhiyun
836*4882a593Smuzhiyun args.file_signature_len_out = read_result;
837*4882a593Smuzhiyun if (copy_to_user(args_usr_ptr, &args, sizeof(args)))
838*4882a593Smuzhiyun error = -EFAULT;
839*4882a593Smuzhiyun
840*4882a593Smuzhiyun out:
841*4882a593Smuzhiyun kfree(sig_buffer);
842*4882a593Smuzhiyun
843*4882a593Smuzhiyun return error;
844*4882a593Smuzhiyun }
845*4882a593Smuzhiyun
ioctl_get_filled_blocks(struct file * f,void __user * arg)846*4882a593Smuzhiyun static long ioctl_get_filled_blocks(struct file *f, void __user *arg)
847*4882a593Smuzhiyun {
848*4882a593Smuzhiyun struct incfs_get_filled_blocks_args __user *args_usr_ptr = arg;
849*4882a593Smuzhiyun struct incfs_get_filled_blocks_args args = {};
850*4882a593Smuzhiyun struct data_file *df = get_incfs_data_file(f);
851*4882a593Smuzhiyun struct incfs_file_data *fd = f->private_data;
852*4882a593Smuzhiyun int error;
853*4882a593Smuzhiyun
854*4882a593Smuzhiyun if (!df || !fd)
855*4882a593Smuzhiyun return -EINVAL;
856*4882a593Smuzhiyun
857*4882a593Smuzhiyun if (fd->fd_fill_permission != CAN_FILL)
858*4882a593Smuzhiyun return -EPERM;
859*4882a593Smuzhiyun
860*4882a593Smuzhiyun if (copy_from_user(&args, args_usr_ptr, sizeof(args)) > 0)
861*4882a593Smuzhiyun return -EINVAL;
862*4882a593Smuzhiyun
863*4882a593Smuzhiyun error = incfs_get_filled_blocks(df, fd, &args);
864*4882a593Smuzhiyun
865*4882a593Smuzhiyun if (copy_to_user(args_usr_ptr, &args, sizeof(args)))
866*4882a593Smuzhiyun return -EFAULT;
867*4882a593Smuzhiyun
868*4882a593Smuzhiyun return error;
869*4882a593Smuzhiyun }
870*4882a593Smuzhiyun
ioctl_get_block_count(struct file * f,void __user * arg)871*4882a593Smuzhiyun static long ioctl_get_block_count(struct file *f, void __user *arg)
872*4882a593Smuzhiyun {
873*4882a593Smuzhiyun struct incfs_get_block_count_args __user *args_usr_ptr = arg;
874*4882a593Smuzhiyun struct incfs_get_block_count_args args = {};
875*4882a593Smuzhiyun struct data_file *df = get_incfs_data_file(f);
876*4882a593Smuzhiyun
877*4882a593Smuzhiyun if (!df)
878*4882a593Smuzhiyun return -EINVAL;
879*4882a593Smuzhiyun
880*4882a593Smuzhiyun args.total_data_blocks_out = df->df_data_block_count;
881*4882a593Smuzhiyun args.filled_data_blocks_out = atomic_read(&df->df_data_blocks_written);
882*4882a593Smuzhiyun args.total_hash_blocks_out = df->df_total_block_count -
883*4882a593Smuzhiyun df->df_data_block_count;
884*4882a593Smuzhiyun args.filled_hash_blocks_out = atomic_read(&df->df_hash_blocks_written);
885*4882a593Smuzhiyun
886*4882a593Smuzhiyun if (copy_to_user(args_usr_ptr, &args, sizeof(args)))
887*4882a593Smuzhiyun return -EFAULT;
888*4882a593Smuzhiyun
889*4882a593Smuzhiyun return 0;
890*4882a593Smuzhiyun }
891*4882a593Smuzhiyun
incfs_ioctl_get_flags(struct file * f,void __user * arg)892*4882a593Smuzhiyun static int incfs_ioctl_get_flags(struct file *f, void __user *arg)
893*4882a593Smuzhiyun {
894*4882a593Smuzhiyun u32 flags = IS_VERITY(file_inode(f)) ? FS_VERITY_FL : 0;
895*4882a593Smuzhiyun
896*4882a593Smuzhiyun return put_user(flags, (int __user *) arg);
897*4882a593Smuzhiyun }
898*4882a593Smuzhiyun
dispatch_ioctl(struct file * f,unsigned int req,unsigned long arg)899*4882a593Smuzhiyun static long dispatch_ioctl(struct file *f, unsigned int req, unsigned long arg)
900*4882a593Smuzhiyun {
901*4882a593Smuzhiyun switch (req) {
902*4882a593Smuzhiyun case INCFS_IOC_FILL_BLOCKS:
903*4882a593Smuzhiyun return ioctl_fill_blocks(f, (void __user *)arg);
904*4882a593Smuzhiyun case INCFS_IOC_READ_FILE_SIGNATURE:
905*4882a593Smuzhiyun return ioctl_read_file_signature(f, (void __user *)arg);
906*4882a593Smuzhiyun case INCFS_IOC_GET_FILLED_BLOCKS:
907*4882a593Smuzhiyun return ioctl_get_filled_blocks(f, (void __user *)arg);
908*4882a593Smuzhiyun case INCFS_IOC_GET_BLOCK_COUNT:
909*4882a593Smuzhiyun return ioctl_get_block_count(f, (void __user *)arg);
910*4882a593Smuzhiyun case FS_IOC_ENABLE_VERITY:
911*4882a593Smuzhiyun return incfs_ioctl_enable_verity(f, (const void __user *)arg);
912*4882a593Smuzhiyun case FS_IOC_GETFLAGS:
913*4882a593Smuzhiyun return incfs_ioctl_get_flags(f, (void __user *) arg);
914*4882a593Smuzhiyun case FS_IOC_MEASURE_VERITY:
915*4882a593Smuzhiyun return incfs_ioctl_measure_verity(f, (void __user *)arg);
916*4882a593Smuzhiyun case FS_IOC_READ_VERITY_METADATA:
917*4882a593Smuzhiyun return incfs_ioctl_read_verity_metadata(f, (void __user *)arg);
918*4882a593Smuzhiyun default:
919*4882a593Smuzhiyun return -EINVAL;
920*4882a593Smuzhiyun }
921*4882a593Smuzhiyun }
922*4882a593Smuzhiyun
923*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
incfs_compat_ioctl(struct file * file,unsigned int cmd,unsigned long arg)924*4882a593Smuzhiyun static long incfs_compat_ioctl(struct file *file, unsigned int cmd,
925*4882a593Smuzhiyun unsigned long arg)
926*4882a593Smuzhiyun {
927*4882a593Smuzhiyun switch (cmd) {
928*4882a593Smuzhiyun case FS_IOC32_GETFLAGS:
929*4882a593Smuzhiyun cmd = FS_IOC_GETFLAGS;
930*4882a593Smuzhiyun break;
931*4882a593Smuzhiyun case INCFS_IOC_FILL_BLOCKS:
932*4882a593Smuzhiyun case INCFS_IOC_READ_FILE_SIGNATURE:
933*4882a593Smuzhiyun case INCFS_IOC_GET_FILLED_BLOCKS:
934*4882a593Smuzhiyun case INCFS_IOC_GET_BLOCK_COUNT:
935*4882a593Smuzhiyun case FS_IOC_ENABLE_VERITY:
936*4882a593Smuzhiyun case FS_IOC_MEASURE_VERITY:
937*4882a593Smuzhiyun case FS_IOC_READ_VERITY_METADATA:
938*4882a593Smuzhiyun break;
939*4882a593Smuzhiyun default:
940*4882a593Smuzhiyun return -ENOIOCTLCMD;
941*4882a593Smuzhiyun }
942*4882a593Smuzhiyun return dispatch_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
943*4882a593Smuzhiyun }
944*4882a593Smuzhiyun #endif
945*4882a593Smuzhiyun
dir_lookup(struct inode * dir_inode,struct dentry * dentry,unsigned int flags)946*4882a593Smuzhiyun static struct dentry *dir_lookup(struct inode *dir_inode, struct dentry *dentry,
947*4882a593Smuzhiyun unsigned int flags)
948*4882a593Smuzhiyun {
949*4882a593Smuzhiyun struct mount_info *mi = get_mount_info(dir_inode->i_sb);
950*4882a593Smuzhiyun struct dentry *dir_dentry = NULL;
951*4882a593Smuzhiyun struct dentry *backing_dentry = NULL;
952*4882a593Smuzhiyun struct path dir_backing_path = {};
953*4882a593Smuzhiyun struct inode_info *dir_info = get_incfs_node(dir_inode);
954*4882a593Smuzhiyun int err = 0;
955*4882a593Smuzhiyun
956*4882a593Smuzhiyun if (!mi || !dir_info || !dir_info->n_backing_inode)
957*4882a593Smuzhiyun return ERR_PTR(-EBADF);
958*4882a593Smuzhiyun
959*4882a593Smuzhiyun if (d_inode(mi->mi_backing_dir_path.dentry) ==
960*4882a593Smuzhiyun dir_info->n_backing_inode) {
961*4882a593Smuzhiyun /* We do lookup in the FS root. Show pseudo files. */
962*4882a593Smuzhiyun err = dir_lookup_pseudo_files(dir_inode->i_sb, dentry);
963*4882a593Smuzhiyun if (err != -ENOENT)
964*4882a593Smuzhiyun goto out;
965*4882a593Smuzhiyun err = 0;
966*4882a593Smuzhiyun }
967*4882a593Smuzhiyun
968*4882a593Smuzhiyun dir_dentry = dget_parent(dentry);
969*4882a593Smuzhiyun get_incfs_backing_path(dir_dentry, &dir_backing_path);
970*4882a593Smuzhiyun backing_dentry = incfs_lookup_dentry(dir_backing_path.dentry,
971*4882a593Smuzhiyun dentry->d_name.name);
972*4882a593Smuzhiyun
973*4882a593Smuzhiyun if (!backing_dentry || IS_ERR(backing_dentry)) {
974*4882a593Smuzhiyun err = IS_ERR(backing_dentry)
975*4882a593Smuzhiyun ? PTR_ERR(backing_dentry)
976*4882a593Smuzhiyun : -EFAULT;
977*4882a593Smuzhiyun backing_dentry = NULL;
978*4882a593Smuzhiyun goto out;
979*4882a593Smuzhiyun } else {
980*4882a593Smuzhiyun struct inode *inode = NULL;
981*4882a593Smuzhiyun struct path backing_path = {
982*4882a593Smuzhiyun .mnt = dir_backing_path.mnt,
983*4882a593Smuzhiyun .dentry = backing_dentry
984*4882a593Smuzhiyun };
985*4882a593Smuzhiyun
986*4882a593Smuzhiyun err = incfs_init_dentry(dentry, &backing_path);
987*4882a593Smuzhiyun if (err)
988*4882a593Smuzhiyun goto out;
989*4882a593Smuzhiyun
990*4882a593Smuzhiyun if (!d_really_is_positive(backing_dentry)) {
991*4882a593Smuzhiyun /*
992*4882a593Smuzhiyun * No such entry found in the backing dir.
993*4882a593Smuzhiyun * Create a negative entry.
994*4882a593Smuzhiyun */
995*4882a593Smuzhiyun d_add(dentry, NULL);
996*4882a593Smuzhiyun err = 0;
997*4882a593Smuzhiyun goto out;
998*4882a593Smuzhiyun }
999*4882a593Smuzhiyun
1000*4882a593Smuzhiyun if (d_inode(backing_dentry)->i_sb !=
1001*4882a593Smuzhiyun dir_info->n_backing_inode->i_sb) {
1002*4882a593Smuzhiyun /*
1003*4882a593Smuzhiyun * Somehow after the path lookup we ended up in a
1004*4882a593Smuzhiyun * different fs mount. If we keep going it's going
1005*4882a593Smuzhiyun * to end badly.
1006*4882a593Smuzhiyun */
1007*4882a593Smuzhiyun err = -EXDEV;
1008*4882a593Smuzhiyun goto out;
1009*4882a593Smuzhiyun }
1010*4882a593Smuzhiyun
1011*4882a593Smuzhiyun inode = fetch_regular_inode(dir_inode->i_sb, backing_dentry);
1012*4882a593Smuzhiyun if (IS_ERR(inode)) {
1013*4882a593Smuzhiyun err = PTR_ERR(inode);
1014*4882a593Smuzhiyun goto out;
1015*4882a593Smuzhiyun }
1016*4882a593Smuzhiyun
1017*4882a593Smuzhiyun d_add(dentry, inode);
1018*4882a593Smuzhiyun }
1019*4882a593Smuzhiyun
1020*4882a593Smuzhiyun out:
1021*4882a593Smuzhiyun dput(dir_dentry);
1022*4882a593Smuzhiyun dput(backing_dentry);
1023*4882a593Smuzhiyun path_put(&dir_backing_path);
1024*4882a593Smuzhiyun if (err)
1025*4882a593Smuzhiyun pr_debug("incfs: %s %s %d\n", __func__,
1026*4882a593Smuzhiyun dentry->d_name.name, err);
1027*4882a593Smuzhiyun return ERR_PTR(err);
1028*4882a593Smuzhiyun }
1029*4882a593Smuzhiyun
dir_mkdir(struct inode * dir,struct dentry * dentry,umode_t mode)1030*4882a593Smuzhiyun static int dir_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1031*4882a593Smuzhiyun {
1032*4882a593Smuzhiyun struct mount_info *mi = get_mount_info(dir->i_sb);
1033*4882a593Smuzhiyun struct inode_info *dir_node = get_incfs_node(dir);
1034*4882a593Smuzhiyun struct dentry *backing_dentry = NULL;
1035*4882a593Smuzhiyun struct path backing_path = {};
1036*4882a593Smuzhiyun int err = 0;
1037*4882a593Smuzhiyun
1038*4882a593Smuzhiyun
1039*4882a593Smuzhiyun if (!mi || !dir_node || !dir_node->n_backing_inode)
1040*4882a593Smuzhiyun return -EBADF;
1041*4882a593Smuzhiyun
1042*4882a593Smuzhiyun err = mutex_lock_interruptible(&mi->mi_dir_struct_mutex);
1043*4882a593Smuzhiyun if (err)
1044*4882a593Smuzhiyun return err;
1045*4882a593Smuzhiyun
1046*4882a593Smuzhiyun get_incfs_backing_path(dentry, &backing_path);
1047*4882a593Smuzhiyun backing_dentry = backing_path.dentry;
1048*4882a593Smuzhiyun
1049*4882a593Smuzhiyun if (!backing_dentry) {
1050*4882a593Smuzhiyun err = -EBADF;
1051*4882a593Smuzhiyun goto path_err;
1052*4882a593Smuzhiyun }
1053*4882a593Smuzhiyun
1054*4882a593Smuzhiyun if (backing_dentry->d_parent == mi->mi_index_dir) {
1055*4882a593Smuzhiyun /* Can't create a subdir inside .index */
1056*4882a593Smuzhiyun err = -EBUSY;
1057*4882a593Smuzhiyun goto out;
1058*4882a593Smuzhiyun }
1059*4882a593Smuzhiyun
1060*4882a593Smuzhiyun if (backing_dentry->d_parent == mi->mi_incomplete_dir) {
1061*4882a593Smuzhiyun /* Can't create a subdir inside .incomplete */
1062*4882a593Smuzhiyun err = -EBUSY;
1063*4882a593Smuzhiyun goto out;
1064*4882a593Smuzhiyun }
1065*4882a593Smuzhiyun inode_lock_nested(dir_node->n_backing_inode, I_MUTEX_PARENT);
1066*4882a593Smuzhiyun err = vfs_mkdir(dir_node->n_backing_inode, backing_dentry, mode | 0222);
1067*4882a593Smuzhiyun inode_unlock(dir_node->n_backing_inode);
1068*4882a593Smuzhiyun if (!err) {
1069*4882a593Smuzhiyun struct inode *inode = NULL;
1070*4882a593Smuzhiyun
1071*4882a593Smuzhiyun if (d_really_is_negative(backing_dentry) ||
1072*4882a593Smuzhiyun unlikely(d_unhashed(backing_dentry))) {
1073*4882a593Smuzhiyun err = -EINVAL;
1074*4882a593Smuzhiyun goto out;
1075*4882a593Smuzhiyun }
1076*4882a593Smuzhiyun
1077*4882a593Smuzhiyun inode = fetch_regular_inode(dir->i_sb, backing_dentry);
1078*4882a593Smuzhiyun if (IS_ERR(inode)) {
1079*4882a593Smuzhiyun err = PTR_ERR(inode);
1080*4882a593Smuzhiyun goto out;
1081*4882a593Smuzhiyun }
1082*4882a593Smuzhiyun d_instantiate(dentry, inode);
1083*4882a593Smuzhiyun }
1084*4882a593Smuzhiyun
1085*4882a593Smuzhiyun out:
1086*4882a593Smuzhiyun if (d_really_is_negative(dentry))
1087*4882a593Smuzhiyun d_drop(dentry);
1088*4882a593Smuzhiyun path_put(&backing_path);
1089*4882a593Smuzhiyun
1090*4882a593Smuzhiyun path_err:
1091*4882a593Smuzhiyun mutex_unlock(&mi->mi_dir_struct_mutex);
1092*4882a593Smuzhiyun if (err)
1093*4882a593Smuzhiyun pr_debug("incfs: %s err:%d\n", __func__, err);
1094*4882a593Smuzhiyun return err;
1095*4882a593Smuzhiyun }
1096*4882a593Smuzhiyun
1097*4882a593Smuzhiyun /*
1098*4882a593Smuzhiyun * Delete file referenced by backing_dentry and if appropriate its hardlink
1099*4882a593Smuzhiyun * from .index and .incomplete
1100*4882a593Smuzhiyun */
file_delete(struct mount_info * mi,struct dentry * dentry,struct dentry * backing_dentry,int nlink)1101*4882a593Smuzhiyun static int file_delete(struct mount_info *mi, struct dentry *dentry,
1102*4882a593Smuzhiyun struct dentry *backing_dentry, int nlink)
1103*4882a593Smuzhiyun {
1104*4882a593Smuzhiyun struct dentry *index_file_dentry = NULL;
1105*4882a593Smuzhiyun struct dentry *incomplete_file_dentry = NULL;
1106*4882a593Smuzhiyun /* 2 chars per byte of file ID + 1 char for \0 */
1107*4882a593Smuzhiyun char file_id_str[2 * sizeof(incfs_uuid_t) + 1] = {0};
1108*4882a593Smuzhiyun ssize_t uuid_size = 0;
1109*4882a593Smuzhiyun int error = 0;
1110*4882a593Smuzhiyun
1111*4882a593Smuzhiyun WARN_ON(!mutex_is_locked(&mi->mi_dir_struct_mutex));
1112*4882a593Smuzhiyun
1113*4882a593Smuzhiyun if (nlink > 3)
1114*4882a593Smuzhiyun goto just_unlink;
1115*4882a593Smuzhiyun
1116*4882a593Smuzhiyun uuid_size = vfs_getxattr(backing_dentry, INCFS_XATTR_ID_NAME,
1117*4882a593Smuzhiyun file_id_str, 2 * sizeof(incfs_uuid_t));
1118*4882a593Smuzhiyun if (uuid_size < 0) {
1119*4882a593Smuzhiyun error = uuid_size;
1120*4882a593Smuzhiyun goto out;
1121*4882a593Smuzhiyun }
1122*4882a593Smuzhiyun
1123*4882a593Smuzhiyun if (uuid_size != 2 * sizeof(incfs_uuid_t)) {
1124*4882a593Smuzhiyun error = -EBADMSG;
1125*4882a593Smuzhiyun goto out;
1126*4882a593Smuzhiyun }
1127*4882a593Smuzhiyun
1128*4882a593Smuzhiyun index_file_dentry = incfs_lookup_dentry(mi->mi_index_dir, file_id_str);
1129*4882a593Smuzhiyun if (IS_ERR(index_file_dentry)) {
1130*4882a593Smuzhiyun error = PTR_ERR(index_file_dentry);
1131*4882a593Smuzhiyun index_file_dentry = NULL;
1132*4882a593Smuzhiyun goto out;
1133*4882a593Smuzhiyun }
1134*4882a593Smuzhiyun
1135*4882a593Smuzhiyun if (d_really_is_positive(index_file_dentry) && nlink > 0)
1136*4882a593Smuzhiyun nlink--;
1137*4882a593Smuzhiyun
1138*4882a593Smuzhiyun if (nlink > 2)
1139*4882a593Smuzhiyun goto just_unlink;
1140*4882a593Smuzhiyun
1141*4882a593Smuzhiyun incomplete_file_dentry = incfs_lookup_dentry(mi->mi_incomplete_dir,
1142*4882a593Smuzhiyun file_id_str);
1143*4882a593Smuzhiyun if (IS_ERR(incomplete_file_dentry)) {
1144*4882a593Smuzhiyun error = PTR_ERR(incomplete_file_dentry);
1145*4882a593Smuzhiyun incomplete_file_dentry = NULL;
1146*4882a593Smuzhiyun goto out;
1147*4882a593Smuzhiyun }
1148*4882a593Smuzhiyun
1149*4882a593Smuzhiyun if (d_really_is_positive(incomplete_file_dentry) && nlink > 0)
1150*4882a593Smuzhiyun nlink--;
1151*4882a593Smuzhiyun
1152*4882a593Smuzhiyun if (nlink > 1)
1153*4882a593Smuzhiyun goto just_unlink;
1154*4882a593Smuzhiyun
1155*4882a593Smuzhiyun if (d_really_is_positive(index_file_dentry)) {
1156*4882a593Smuzhiyun error = incfs_unlink(index_file_dentry);
1157*4882a593Smuzhiyun if (error)
1158*4882a593Smuzhiyun goto out;
1159*4882a593Smuzhiyun notify_unlink(dentry, file_id_str, INCFS_INDEX_NAME);
1160*4882a593Smuzhiyun }
1161*4882a593Smuzhiyun
1162*4882a593Smuzhiyun if (d_really_is_positive(incomplete_file_dentry)) {
1163*4882a593Smuzhiyun error = incfs_unlink(incomplete_file_dentry);
1164*4882a593Smuzhiyun if (error)
1165*4882a593Smuzhiyun goto out;
1166*4882a593Smuzhiyun notify_unlink(dentry, file_id_str, INCFS_INCOMPLETE_NAME);
1167*4882a593Smuzhiyun }
1168*4882a593Smuzhiyun
1169*4882a593Smuzhiyun just_unlink:
1170*4882a593Smuzhiyun error = incfs_unlink(backing_dentry);
1171*4882a593Smuzhiyun
1172*4882a593Smuzhiyun out:
1173*4882a593Smuzhiyun dput(index_file_dentry);
1174*4882a593Smuzhiyun dput(incomplete_file_dentry);
1175*4882a593Smuzhiyun if (error)
1176*4882a593Smuzhiyun pr_debug("incfs: delete_file_from_index err:%d\n", error);
1177*4882a593Smuzhiyun return error;
1178*4882a593Smuzhiyun }
1179*4882a593Smuzhiyun
dir_unlink(struct inode * dir,struct dentry * dentry)1180*4882a593Smuzhiyun static int dir_unlink(struct inode *dir, struct dentry *dentry)
1181*4882a593Smuzhiyun {
1182*4882a593Smuzhiyun struct mount_info *mi = get_mount_info(dir->i_sb);
1183*4882a593Smuzhiyun struct path backing_path = {};
1184*4882a593Smuzhiyun struct kstat stat;
1185*4882a593Smuzhiyun int err = 0;
1186*4882a593Smuzhiyun
1187*4882a593Smuzhiyun if (!mi)
1188*4882a593Smuzhiyun return -EBADF;
1189*4882a593Smuzhiyun
1190*4882a593Smuzhiyun err = mutex_lock_interruptible(&mi->mi_dir_struct_mutex);
1191*4882a593Smuzhiyun if (err)
1192*4882a593Smuzhiyun return err;
1193*4882a593Smuzhiyun
1194*4882a593Smuzhiyun get_incfs_backing_path(dentry, &backing_path);
1195*4882a593Smuzhiyun if (!backing_path.dentry) {
1196*4882a593Smuzhiyun err = -EBADF;
1197*4882a593Smuzhiyun goto path_err;
1198*4882a593Smuzhiyun }
1199*4882a593Smuzhiyun
1200*4882a593Smuzhiyun if (backing_path.dentry->d_parent == mi->mi_index_dir) {
1201*4882a593Smuzhiyun /* Direct unlink from .index are not allowed. */
1202*4882a593Smuzhiyun err = -EBUSY;
1203*4882a593Smuzhiyun goto out;
1204*4882a593Smuzhiyun }
1205*4882a593Smuzhiyun
1206*4882a593Smuzhiyun if (backing_path.dentry->d_parent == mi->mi_incomplete_dir) {
1207*4882a593Smuzhiyun /* Direct unlink from .incomplete are not allowed. */
1208*4882a593Smuzhiyun err = -EBUSY;
1209*4882a593Smuzhiyun goto out;
1210*4882a593Smuzhiyun }
1211*4882a593Smuzhiyun
1212*4882a593Smuzhiyun err = vfs_getattr(&backing_path, &stat, STATX_NLINK,
1213*4882a593Smuzhiyun AT_STATX_SYNC_AS_STAT);
1214*4882a593Smuzhiyun if (err)
1215*4882a593Smuzhiyun goto out;
1216*4882a593Smuzhiyun
1217*4882a593Smuzhiyun err = file_delete(mi, dentry, backing_path.dentry, stat.nlink);
1218*4882a593Smuzhiyun
1219*4882a593Smuzhiyun d_drop(dentry);
1220*4882a593Smuzhiyun out:
1221*4882a593Smuzhiyun path_put(&backing_path);
1222*4882a593Smuzhiyun path_err:
1223*4882a593Smuzhiyun if (err)
1224*4882a593Smuzhiyun pr_debug("incfs: %s err:%d\n", __func__, err);
1225*4882a593Smuzhiyun mutex_unlock(&mi->mi_dir_struct_mutex);
1226*4882a593Smuzhiyun return err;
1227*4882a593Smuzhiyun }
1228*4882a593Smuzhiyun
dir_link(struct dentry * old_dentry,struct inode * dir,struct dentry * new_dentry)1229*4882a593Smuzhiyun static int dir_link(struct dentry *old_dentry, struct inode *dir,
1230*4882a593Smuzhiyun struct dentry *new_dentry)
1231*4882a593Smuzhiyun {
1232*4882a593Smuzhiyun struct mount_info *mi = get_mount_info(dir->i_sb);
1233*4882a593Smuzhiyun struct path backing_old_path = {};
1234*4882a593Smuzhiyun struct path backing_new_path = {};
1235*4882a593Smuzhiyun int error = 0;
1236*4882a593Smuzhiyun
1237*4882a593Smuzhiyun if (!mi)
1238*4882a593Smuzhiyun return -EBADF;
1239*4882a593Smuzhiyun
1240*4882a593Smuzhiyun error = mutex_lock_interruptible(&mi->mi_dir_struct_mutex);
1241*4882a593Smuzhiyun if (error)
1242*4882a593Smuzhiyun return error;
1243*4882a593Smuzhiyun
1244*4882a593Smuzhiyun get_incfs_backing_path(old_dentry, &backing_old_path);
1245*4882a593Smuzhiyun get_incfs_backing_path(new_dentry, &backing_new_path);
1246*4882a593Smuzhiyun
1247*4882a593Smuzhiyun if (backing_new_path.dentry->d_parent == mi->mi_index_dir) {
1248*4882a593Smuzhiyun /* Can't link to .index */
1249*4882a593Smuzhiyun error = -EBUSY;
1250*4882a593Smuzhiyun goto out;
1251*4882a593Smuzhiyun }
1252*4882a593Smuzhiyun
1253*4882a593Smuzhiyun if (backing_new_path.dentry->d_parent == mi->mi_incomplete_dir) {
1254*4882a593Smuzhiyun /* Can't link to .incomplete */
1255*4882a593Smuzhiyun error = -EBUSY;
1256*4882a593Smuzhiyun goto out;
1257*4882a593Smuzhiyun }
1258*4882a593Smuzhiyun
1259*4882a593Smuzhiyun error = incfs_link(backing_old_path.dentry, backing_new_path.dentry);
1260*4882a593Smuzhiyun if (!error) {
1261*4882a593Smuzhiyun struct inode *inode = NULL;
1262*4882a593Smuzhiyun struct dentry *bdentry = backing_new_path.dentry;
1263*4882a593Smuzhiyun
1264*4882a593Smuzhiyun if (d_really_is_negative(bdentry)) {
1265*4882a593Smuzhiyun error = -EINVAL;
1266*4882a593Smuzhiyun goto out;
1267*4882a593Smuzhiyun }
1268*4882a593Smuzhiyun
1269*4882a593Smuzhiyun inode = fetch_regular_inode(dir->i_sb, bdentry);
1270*4882a593Smuzhiyun if (IS_ERR(inode)) {
1271*4882a593Smuzhiyun error = PTR_ERR(inode);
1272*4882a593Smuzhiyun goto out;
1273*4882a593Smuzhiyun }
1274*4882a593Smuzhiyun d_instantiate(new_dentry, inode);
1275*4882a593Smuzhiyun }
1276*4882a593Smuzhiyun
1277*4882a593Smuzhiyun out:
1278*4882a593Smuzhiyun path_put(&backing_old_path);
1279*4882a593Smuzhiyun path_put(&backing_new_path);
1280*4882a593Smuzhiyun if (error)
1281*4882a593Smuzhiyun pr_debug("incfs: %s err:%d\n", __func__, error);
1282*4882a593Smuzhiyun mutex_unlock(&mi->mi_dir_struct_mutex);
1283*4882a593Smuzhiyun return error;
1284*4882a593Smuzhiyun }
1285*4882a593Smuzhiyun
dir_rmdir(struct inode * dir,struct dentry * dentry)1286*4882a593Smuzhiyun static int dir_rmdir(struct inode *dir, struct dentry *dentry)
1287*4882a593Smuzhiyun {
1288*4882a593Smuzhiyun struct mount_info *mi = get_mount_info(dir->i_sb);
1289*4882a593Smuzhiyun struct path backing_path = {};
1290*4882a593Smuzhiyun int err = 0;
1291*4882a593Smuzhiyun
1292*4882a593Smuzhiyun if (!mi)
1293*4882a593Smuzhiyun return -EBADF;
1294*4882a593Smuzhiyun
1295*4882a593Smuzhiyun err = mutex_lock_interruptible(&mi->mi_dir_struct_mutex);
1296*4882a593Smuzhiyun if (err)
1297*4882a593Smuzhiyun return err;
1298*4882a593Smuzhiyun
1299*4882a593Smuzhiyun get_incfs_backing_path(dentry, &backing_path);
1300*4882a593Smuzhiyun if (!backing_path.dentry) {
1301*4882a593Smuzhiyun err = -EBADF;
1302*4882a593Smuzhiyun goto path_err;
1303*4882a593Smuzhiyun }
1304*4882a593Smuzhiyun
1305*4882a593Smuzhiyun if (backing_path.dentry == mi->mi_index_dir) {
1306*4882a593Smuzhiyun /* Can't delete .index */
1307*4882a593Smuzhiyun err = -EBUSY;
1308*4882a593Smuzhiyun goto out;
1309*4882a593Smuzhiyun }
1310*4882a593Smuzhiyun
1311*4882a593Smuzhiyun if (backing_path.dentry == mi->mi_incomplete_dir) {
1312*4882a593Smuzhiyun /* Can't delete .incomplete */
1313*4882a593Smuzhiyun err = -EBUSY;
1314*4882a593Smuzhiyun goto out;
1315*4882a593Smuzhiyun }
1316*4882a593Smuzhiyun
1317*4882a593Smuzhiyun err = incfs_rmdir(backing_path.dentry);
1318*4882a593Smuzhiyun if (!err)
1319*4882a593Smuzhiyun d_drop(dentry);
1320*4882a593Smuzhiyun out:
1321*4882a593Smuzhiyun path_put(&backing_path);
1322*4882a593Smuzhiyun
1323*4882a593Smuzhiyun path_err:
1324*4882a593Smuzhiyun if (err)
1325*4882a593Smuzhiyun pr_debug("incfs: %s err:%d\n", __func__, err);
1326*4882a593Smuzhiyun mutex_unlock(&mi->mi_dir_struct_mutex);
1327*4882a593Smuzhiyun return err;
1328*4882a593Smuzhiyun }
1329*4882a593Smuzhiyun
dir_rename(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry)1330*4882a593Smuzhiyun static int dir_rename(struct inode *old_dir, struct dentry *old_dentry,
1331*4882a593Smuzhiyun struct inode *new_dir, struct dentry *new_dentry)
1332*4882a593Smuzhiyun {
1333*4882a593Smuzhiyun struct mount_info *mi = get_mount_info(old_dir->i_sb);
1334*4882a593Smuzhiyun struct dentry *backing_old_dentry;
1335*4882a593Smuzhiyun struct dentry *backing_new_dentry;
1336*4882a593Smuzhiyun struct dentry *backing_old_dir_dentry;
1337*4882a593Smuzhiyun struct dentry *backing_new_dir_dentry;
1338*4882a593Smuzhiyun struct inode *target_inode;
1339*4882a593Smuzhiyun struct dentry *trap;
1340*4882a593Smuzhiyun int error = 0;
1341*4882a593Smuzhiyun
1342*4882a593Smuzhiyun error = mutex_lock_interruptible(&mi->mi_dir_struct_mutex);
1343*4882a593Smuzhiyun if (error)
1344*4882a593Smuzhiyun return error;
1345*4882a593Smuzhiyun
1346*4882a593Smuzhiyun backing_old_dentry = get_incfs_dentry(old_dentry)->backing_path.dentry;
1347*4882a593Smuzhiyun
1348*4882a593Smuzhiyun if (!backing_old_dentry || backing_old_dentry == mi->mi_index_dir ||
1349*4882a593Smuzhiyun backing_old_dentry == mi->mi_incomplete_dir) {
1350*4882a593Smuzhiyun /* Renaming .index or .incomplete not allowed */
1351*4882a593Smuzhiyun error = -EBUSY;
1352*4882a593Smuzhiyun goto exit;
1353*4882a593Smuzhiyun }
1354*4882a593Smuzhiyun
1355*4882a593Smuzhiyun backing_new_dentry = get_incfs_dentry(new_dentry)->backing_path.dentry;
1356*4882a593Smuzhiyun dget(backing_old_dentry);
1357*4882a593Smuzhiyun dget(backing_new_dentry);
1358*4882a593Smuzhiyun
1359*4882a593Smuzhiyun backing_old_dir_dentry = dget_parent(backing_old_dentry);
1360*4882a593Smuzhiyun backing_new_dir_dentry = dget_parent(backing_new_dentry);
1361*4882a593Smuzhiyun target_inode = d_inode(new_dentry);
1362*4882a593Smuzhiyun
1363*4882a593Smuzhiyun if (backing_old_dir_dentry == mi->mi_index_dir ||
1364*4882a593Smuzhiyun backing_old_dir_dentry == mi->mi_incomplete_dir) {
1365*4882a593Smuzhiyun /* Direct moves from .index or .incomplete are not allowed. */
1366*4882a593Smuzhiyun error = -EBUSY;
1367*4882a593Smuzhiyun goto out;
1368*4882a593Smuzhiyun }
1369*4882a593Smuzhiyun
1370*4882a593Smuzhiyun trap = lock_rename(backing_old_dir_dentry, backing_new_dir_dentry);
1371*4882a593Smuzhiyun
1372*4882a593Smuzhiyun if (trap == backing_old_dentry) {
1373*4882a593Smuzhiyun error = -EINVAL;
1374*4882a593Smuzhiyun goto unlock_out;
1375*4882a593Smuzhiyun }
1376*4882a593Smuzhiyun if (trap == backing_new_dentry) {
1377*4882a593Smuzhiyun error = -ENOTEMPTY;
1378*4882a593Smuzhiyun goto unlock_out;
1379*4882a593Smuzhiyun }
1380*4882a593Smuzhiyun
1381*4882a593Smuzhiyun error = vfs_rename(d_inode(backing_old_dir_dentry), backing_old_dentry,
1382*4882a593Smuzhiyun d_inode(backing_new_dir_dentry), backing_new_dentry,
1383*4882a593Smuzhiyun NULL, 0);
1384*4882a593Smuzhiyun if (error)
1385*4882a593Smuzhiyun goto unlock_out;
1386*4882a593Smuzhiyun if (target_inode)
1387*4882a593Smuzhiyun fsstack_copy_attr_all(target_inode,
1388*4882a593Smuzhiyun get_incfs_node(target_inode)->n_backing_inode);
1389*4882a593Smuzhiyun fsstack_copy_attr_all(new_dir, d_inode(backing_new_dir_dentry));
1390*4882a593Smuzhiyun if (new_dir != old_dir)
1391*4882a593Smuzhiyun fsstack_copy_attr_all(old_dir, d_inode(backing_old_dir_dentry));
1392*4882a593Smuzhiyun
1393*4882a593Smuzhiyun unlock_out:
1394*4882a593Smuzhiyun unlock_rename(backing_old_dir_dentry, backing_new_dir_dentry);
1395*4882a593Smuzhiyun
1396*4882a593Smuzhiyun out:
1397*4882a593Smuzhiyun dput(backing_new_dir_dentry);
1398*4882a593Smuzhiyun dput(backing_old_dir_dentry);
1399*4882a593Smuzhiyun dput(backing_new_dentry);
1400*4882a593Smuzhiyun dput(backing_old_dentry);
1401*4882a593Smuzhiyun
1402*4882a593Smuzhiyun exit:
1403*4882a593Smuzhiyun mutex_unlock(&mi->mi_dir_struct_mutex);
1404*4882a593Smuzhiyun if (error)
1405*4882a593Smuzhiyun pr_debug("incfs: %s err:%d\n", __func__, error);
1406*4882a593Smuzhiyun return error;
1407*4882a593Smuzhiyun }
1408*4882a593Smuzhiyun
1409*4882a593Smuzhiyun
file_open(struct inode * inode,struct file * file)1410*4882a593Smuzhiyun static int file_open(struct inode *inode, struct file *file)
1411*4882a593Smuzhiyun {
1412*4882a593Smuzhiyun struct mount_info *mi = get_mount_info(inode->i_sb);
1413*4882a593Smuzhiyun struct file *backing_file = NULL;
1414*4882a593Smuzhiyun struct path backing_path = {};
1415*4882a593Smuzhiyun int err = 0;
1416*4882a593Smuzhiyun int flags = O_NOATIME | O_LARGEFILE |
1417*4882a593Smuzhiyun (S_ISDIR(inode->i_mode) ? O_RDONLY : O_RDWR);
1418*4882a593Smuzhiyun const struct cred *old_cred;
1419*4882a593Smuzhiyun
1420*4882a593Smuzhiyun WARN_ON(file->private_data);
1421*4882a593Smuzhiyun
1422*4882a593Smuzhiyun if (!mi)
1423*4882a593Smuzhiyun return -EBADF;
1424*4882a593Smuzhiyun
1425*4882a593Smuzhiyun get_incfs_backing_path(file->f_path.dentry, &backing_path);
1426*4882a593Smuzhiyun if (!backing_path.dentry)
1427*4882a593Smuzhiyun return -EBADF;
1428*4882a593Smuzhiyun
1429*4882a593Smuzhiyun old_cred = override_creds(mi->mi_owner);
1430*4882a593Smuzhiyun backing_file = dentry_open(&backing_path, flags, current_cred());
1431*4882a593Smuzhiyun revert_creds(old_cred);
1432*4882a593Smuzhiyun path_put(&backing_path);
1433*4882a593Smuzhiyun
1434*4882a593Smuzhiyun if (IS_ERR(backing_file)) {
1435*4882a593Smuzhiyun err = PTR_ERR(backing_file);
1436*4882a593Smuzhiyun backing_file = NULL;
1437*4882a593Smuzhiyun goto out;
1438*4882a593Smuzhiyun }
1439*4882a593Smuzhiyun
1440*4882a593Smuzhiyun if (S_ISREG(inode->i_mode)) {
1441*4882a593Smuzhiyun struct incfs_file_data *fd = kzalloc(sizeof(*fd), GFP_NOFS);
1442*4882a593Smuzhiyun
1443*4882a593Smuzhiyun if (!fd) {
1444*4882a593Smuzhiyun err = -ENOMEM;
1445*4882a593Smuzhiyun goto out;
1446*4882a593Smuzhiyun }
1447*4882a593Smuzhiyun
1448*4882a593Smuzhiyun *fd = (struct incfs_file_data) {
1449*4882a593Smuzhiyun .fd_fill_permission = CANT_FILL,
1450*4882a593Smuzhiyun };
1451*4882a593Smuzhiyun file->private_data = fd;
1452*4882a593Smuzhiyun
1453*4882a593Smuzhiyun err = make_inode_ready_for_data_ops(mi, inode, backing_file);
1454*4882a593Smuzhiyun if (err)
1455*4882a593Smuzhiyun goto out;
1456*4882a593Smuzhiyun
1457*4882a593Smuzhiyun err = incfs_fsverity_file_open(inode, file);
1458*4882a593Smuzhiyun if (err)
1459*4882a593Smuzhiyun goto out;
1460*4882a593Smuzhiyun } else if (S_ISDIR(inode->i_mode)) {
1461*4882a593Smuzhiyun struct dir_file *dir = NULL;
1462*4882a593Smuzhiyun
1463*4882a593Smuzhiyun dir = incfs_open_dir_file(mi, backing_file);
1464*4882a593Smuzhiyun if (IS_ERR(dir))
1465*4882a593Smuzhiyun err = PTR_ERR(dir);
1466*4882a593Smuzhiyun else
1467*4882a593Smuzhiyun file->private_data = dir;
1468*4882a593Smuzhiyun } else
1469*4882a593Smuzhiyun err = -EBADF;
1470*4882a593Smuzhiyun
1471*4882a593Smuzhiyun out:
1472*4882a593Smuzhiyun if (err) {
1473*4882a593Smuzhiyun pr_debug("name:%s err: %d\n",
1474*4882a593Smuzhiyun file->f_path.dentry->d_name.name, err);
1475*4882a593Smuzhiyun if (S_ISREG(inode->i_mode))
1476*4882a593Smuzhiyun kfree(file->private_data);
1477*4882a593Smuzhiyun else if (S_ISDIR(inode->i_mode))
1478*4882a593Smuzhiyun incfs_free_dir_file(file->private_data);
1479*4882a593Smuzhiyun
1480*4882a593Smuzhiyun file->private_data = NULL;
1481*4882a593Smuzhiyun }
1482*4882a593Smuzhiyun
1483*4882a593Smuzhiyun if (backing_file)
1484*4882a593Smuzhiyun fput(backing_file);
1485*4882a593Smuzhiyun return err;
1486*4882a593Smuzhiyun }
1487*4882a593Smuzhiyun
file_release(struct inode * inode,struct file * file)1488*4882a593Smuzhiyun static int file_release(struct inode *inode, struct file *file)
1489*4882a593Smuzhiyun {
1490*4882a593Smuzhiyun if (S_ISREG(inode->i_mode)) {
1491*4882a593Smuzhiyun kfree(file->private_data);
1492*4882a593Smuzhiyun file->private_data = NULL;
1493*4882a593Smuzhiyun } else if (S_ISDIR(inode->i_mode)) {
1494*4882a593Smuzhiyun struct dir_file *dir = get_incfs_dir_file(file);
1495*4882a593Smuzhiyun
1496*4882a593Smuzhiyun incfs_free_dir_file(dir);
1497*4882a593Smuzhiyun }
1498*4882a593Smuzhiyun
1499*4882a593Smuzhiyun return 0;
1500*4882a593Smuzhiyun }
1501*4882a593Smuzhiyun
dentry_revalidate(struct dentry * d,unsigned int flags)1502*4882a593Smuzhiyun static int dentry_revalidate(struct dentry *d, unsigned int flags)
1503*4882a593Smuzhiyun {
1504*4882a593Smuzhiyun struct path backing_path = {};
1505*4882a593Smuzhiyun struct inode_info *info = get_incfs_node(d_inode(d));
1506*4882a593Smuzhiyun struct inode *binode = (info == NULL) ? NULL : info->n_backing_inode;
1507*4882a593Smuzhiyun struct dentry *backing_dentry = NULL;
1508*4882a593Smuzhiyun int result = 0;
1509*4882a593Smuzhiyun
1510*4882a593Smuzhiyun if (flags & LOOKUP_RCU)
1511*4882a593Smuzhiyun return -ECHILD;
1512*4882a593Smuzhiyun
1513*4882a593Smuzhiyun get_incfs_backing_path(d, &backing_path);
1514*4882a593Smuzhiyun backing_dentry = backing_path.dentry;
1515*4882a593Smuzhiyun if (!backing_dentry)
1516*4882a593Smuzhiyun goto out;
1517*4882a593Smuzhiyun
1518*4882a593Smuzhiyun if (d_inode(backing_dentry) != binode) {
1519*4882a593Smuzhiyun /*
1520*4882a593Smuzhiyun * Backing inodes obtained via dentry and inode don't match.
1521*4882a593Smuzhiyun * It indicates that most likely backing dir has changed
1522*4882a593Smuzhiyun * directly bypassing Incremental FS interface.
1523*4882a593Smuzhiyun */
1524*4882a593Smuzhiyun goto out;
1525*4882a593Smuzhiyun }
1526*4882a593Smuzhiyun
1527*4882a593Smuzhiyun if (backing_dentry->d_flags & DCACHE_OP_REVALIDATE) {
1528*4882a593Smuzhiyun result = backing_dentry->d_op->d_revalidate(backing_dentry,
1529*4882a593Smuzhiyun flags);
1530*4882a593Smuzhiyun } else
1531*4882a593Smuzhiyun result = 1;
1532*4882a593Smuzhiyun
1533*4882a593Smuzhiyun out:
1534*4882a593Smuzhiyun path_put(&backing_path);
1535*4882a593Smuzhiyun return result;
1536*4882a593Smuzhiyun }
1537*4882a593Smuzhiyun
dentry_release(struct dentry * d)1538*4882a593Smuzhiyun static void dentry_release(struct dentry *d)
1539*4882a593Smuzhiyun {
1540*4882a593Smuzhiyun struct dentry_info *di = get_incfs_dentry(d);
1541*4882a593Smuzhiyun
1542*4882a593Smuzhiyun if (di)
1543*4882a593Smuzhiyun path_put(&di->backing_path);
1544*4882a593Smuzhiyun kfree(d->d_fsdata);
1545*4882a593Smuzhiyun d->d_fsdata = NULL;
1546*4882a593Smuzhiyun }
1547*4882a593Smuzhiyun
alloc_inode(struct super_block * sb)1548*4882a593Smuzhiyun static struct inode *alloc_inode(struct super_block *sb)
1549*4882a593Smuzhiyun {
1550*4882a593Smuzhiyun struct inode_info *node = kzalloc(sizeof(*node), GFP_NOFS);
1551*4882a593Smuzhiyun
1552*4882a593Smuzhiyun /* TODO: add a slab-based cache here. */
1553*4882a593Smuzhiyun if (!node)
1554*4882a593Smuzhiyun return NULL;
1555*4882a593Smuzhiyun inode_init_once(&node->n_vfs_inode);
1556*4882a593Smuzhiyun return &node->n_vfs_inode;
1557*4882a593Smuzhiyun }
1558*4882a593Smuzhiyun
free_inode(struct inode * inode)1559*4882a593Smuzhiyun static void free_inode(struct inode *inode)
1560*4882a593Smuzhiyun {
1561*4882a593Smuzhiyun struct inode_info *node = get_incfs_node(inode);
1562*4882a593Smuzhiyun
1563*4882a593Smuzhiyun kfree(node);
1564*4882a593Smuzhiyun }
1565*4882a593Smuzhiyun
evict_inode(struct inode * inode)1566*4882a593Smuzhiyun static void evict_inode(struct inode *inode)
1567*4882a593Smuzhiyun {
1568*4882a593Smuzhiyun struct inode_info *node = get_incfs_node(inode);
1569*4882a593Smuzhiyun
1570*4882a593Smuzhiyun if (node) {
1571*4882a593Smuzhiyun if (node->n_backing_inode) {
1572*4882a593Smuzhiyun iput(node->n_backing_inode);
1573*4882a593Smuzhiyun node->n_backing_inode = NULL;
1574*4882a593Smuzhiyun }
1575*4882a593Smuzhiyun if (node->n_file) {
1576*4882a593Smuzhiyun incfs_free_data_file(node->n_file);
1577*4882a593Smuzhiyun node->n_file = NULL;
1578*4882a593Smuzhiyun }
1579*4882a593Smuzhiyun }
1580*4882a593Smuzhiyun
1581*4882a593Smuzhiyun truncate_inode_pages(&inode->i_data, 0);
1582*4882a593Smuzhiyun clear_inode(inode);
1583*4882a593Smuzhiyun }
1584*4882a593Smuzhiyun
incfs_setattr(struct dentry * dentry,struct iattr * ia)1585*4882a593Smuzhiyun static int incfs_setattr(struct dentry *dentry, struct iattr *ia)
1586*4882a593Smuzhiyun {
1587*4882a593Smuzhiyun struct dentry_info *di = get_incfs_dentry(dentry);
1588*4882a593Smuzhiyun struct dentry *backing_dentry;
1589*4882a593Smuzhiyun struct inode *backing_inode;
1590*4882a593Smuzhiyun int error;
1591*4882a593Smuzhiyun
1592*4882a593Smuzhiyun if (ia->ia_valid & ATTR_SIZE)
1593*4882a593Smuzhiyun return -EINVAL;
1594*4882a593Smuzhiyun
1595*4882a593Smuzhiyun if ((ia->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
1596*4882a593Smuzhiyun (ia->ia_valid & ATTR_MODE))
1597*4882a593Smuzhiyun return -EINVAL;
1598*4882a593Smuzhiyun
1599*4882a593Smuzhiyun if (!di)
1600*4882a593Smuzhiyun return -EINVAL;
1601*4882a593Smuzhiyun backing_dentry = di->backing_path.dentry;
1602*4882a593Smuzhiyun if (!backing_dentry)
1603*4882a593Smuzhiyun return -EINVAL;
1604*4882a593Smuzhiyun
1605*4882a593Smuzhiyun backing_inode = d_inode(backing_dentry);
1606*4882a593Smuzhiyun
1607*4882a593Smuzhiyun /* incfs files are readonly, but the backing files must be writeable */
1608*4882a593Smuzhiyun if (S_ISREG(backing_inode->i_mode)) {
1609*4882a593Smuzhiyun if ((ia->ia_valid & ATTR_MODE) && (ia->ia_mode & 0222))
1610*4882a593Smuzhiyun return -EINVAL;
1611*4882a593Smuzhiyun
1612*4882a593Smuzhiyun ia->ia_mode |= 0222;
1613*4882a593Smuzhiyun }
1614*4882a593Smuzhiyun
1615*4882a593Smuzhiyun inode_lock(d_inode(backing_dentry));
1616*4882a593Smuzhiyun error = notify_change(backing_dentry, ia, NULL);
1617*4882a593Smuzhiyun inode_unlock(d_inode(backing_dentry));
1618*4882a593Smuzhiyun
1619*4882a593Smuzhiyun if (error)
1620*4882a593Smuzhiyun return error;
1621*4882a593Smuzhiyun
1622*4882a593Smuzhiyun if (S_ISREG(backing_inode->i_mode))
1623*4882a593Smuzhiyun ia->ia_mode &= ~0222;
1624*4882a593Smuzhiyun
1625*4882a593Smuzhiyun return simple_setattr(dentry, ia);
1626*4882a593Smuzhiyun }
1627*4882a593Smuzhiyun
1628*4882a593Smuzhiyun
incfs_getattr(const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)1629*4882a593Smuzhiyun static int incfs_getattr(const struct path *path,
1630*4882a593Smuzhiyun struct kstat *stat, u32 request_mask,
1631*4882a593Smuzhiyun unsigned int query_flags)
1632*4882a593Smuzhiyun {
1633*4882a593Smuzhiyun struct inode *inode = d_inode(path->dentry);
1634*4882a593Smuzhiyun
1635*4882a593Smuzhiyun generic_fillattr(inode, stat);
1636*4882a593Smuzhiyun
1637*4882a593Smuzhiyun if (inode->i_ino < INCFS_START_INO_RANGE)
1638*4882a593Smuzhiyun return 0;
1639*4882a593Smuzhiyun
1640*4882a593Smuzhiyun stat->attributes &= ~STATX_ATTR_VERITY;
1641*4882a593Smuzhiyun if (IS_VERITY(inode))
1642*4882a593Smuzhiyun stat->attributes |= STATX_ATTR_VERITY;
1643*4882a593Smuzhiyun stat->attributes_mask |= STATX_ATTR_VERITY;
1644*4882a593Smuzhiyun
1645*4882a593Smuzhiyun if (request_mask & STATX_BLOCKS) {
1646*4882a593Smuzhiyun struct kstat backing_kstat;
1647*4882a593Smuzhiyun struct dentry_info *di = get_incfs_dentry(path->dentry);
1648*4882a593Smuzhiyun int error = 0;
1649*4882a593Smuzhiyun struct path *backing_path;
1650*4882a593Smuzhiyun
1651*4882a593Smuzhiyun if (!di)
1652*4882a593Smuzhiyun return -EFSCORRUPTED;
1653*4882a593Smuzhiyun backing_path = &di->backing_path;
1654*4882a593Smuzhiyun error = vfs_getattr(backing_path, &backing_kstat, STATX_BLOCKS,
1655*4882a593Smuzhiyun AT_STATX_SYNC_AS_STAT);
1656*4882a593Smuzhiyun if (error)
1657*4882a593Smuzhiyun return error;
1658*4882a593Smuzhiyun
1659*4882a593Smuzhiyun stat->blocks = backing_kstat.blocks;
1660*4882a593Smuzhiyun }
1661*4882a593Smuzhiyun
1662*4882a593Smuzhiyun return 0;
1663*4882a593Smuzhiyun }
1664*4882a593Smuzhiyun
incfs_getxattr(struct dentry * d,const char * name,void * value,size_t size)1665*4882a593Smuzhiyun static ssize_t incfs_getxattr(struct dentry *d, const char *name,
1666*4882a593Smuzhiyun void *value, size_t size)
1667*4882a593Smuzhiyun {
1668*4882a593Smuzhiyun struct dentry_info *di = get_incfs_dentry(d);
1669*4882a593Smuzhiyun struct mount_info *mi = get_mount_info(d->d_sb);
1670*4882a593Smuzhiyun char *stored_value;
1671*4882a593Smuzhiyun size_t stored_size;
1672*4882a593Smuzhiyun int i;
1673*4882a593Smuzhiyun
1674*4882a593Smuzhiyun if (di && di->backing_path.dentry)
1675*4882a593Smuzhiyun return vfs_getxattr(di->backing_path.dentry, name, value, size);
1676*4882a593Smuzhiyun
1677*4882a593Smuzhiyun if (strcmp(name, "security.selinux"))
1678*4882a593Smuzhiyun return -ENODATA;
1679*4882a593Smuzhiyun
1680*4882a593Smuzhiyun for (i = 0; i < PSEUDO_FILE_COUNT; ++i)
1681*4882a593Smuzhiyun if (!strcmp(d->d_iname, incfs_pseudo_file_names[i].data))
1682*4882a593Smuzhiyun break;
1683*4882a593Smuzhiyun if (i == PSEUDO_FILE_COUNT)
1684*4882a593Smuzhiyun return -ENODATA;
1685*4882a593Smuzhiyun
1686*4882a593Smuzhiyun stored_value = mi->pseudo_file_xattr[i].data;
1687*4882a593Smuzhiyun stored_size = mi->pseudo_file_xattr[i].len;
1688*4882a593Smuzhiyun if (!stored_value)
1689*4882a593Smuzhiyun return -ENODATA;
1690*4882a593Smuzhiyun
1691*4882a593Smuzhiyun if (stored_size > size)
1692*4882a593Smuzhiyun return -E2BIG;
1693*4882a593Smuzhiyun
1694*4882a593Smuzhiyun memcpy(value, stored_value, stored_size);
1695*4882a593Smuzhiyun return stored_size;
1696*4882a593Smuzhiyun }
1697*4882a593Smuzhiyun
1698*4882a593Smuzhiyun
incfs_setxattr(struct dentry * d,const char * name,const void * value,size_t size,int flags)1699*4882a593Smuzhiyun static ssize_t incfs_setxattr(struct dentry *d, const char *name,
1700*4882a593Smuzhiyun const void *value, size_t size, int flags)
1701*4882a593Smuzhiyun {
1702*4882a593Smuzhiyun struct dentry_info *di = get_incfs_dentry(d);
1703*4882a593Smuzhiyun struct mount_info *mi = get_mount_info(d->d_sb);
1704*4882a593Smuzhiyun u8 **stored_value;
1705*4882a593Smuzhiyun size_t *stored_size;
1706*4882a593Smuzhiyun int i;
1707*4882a593Smuzhiyun
1708*4882a593Smuzhiyun if (di && di->backing_path.dentry)
1709*4882a593Smuzhiyun return vfs_setxattr(di->backing_path.dentry, name, value, size,
1710*4882a593Smuzhiyun flags);
1711*4882a593Smuzhiyun
1712*4882a593Smuzhiyun if (strcmp(name, "security.selinux"))
1713*4882a593Smuzhiyun return -ENODATA;
1714*4882a593Smuzhiyun
1715*4882a593Smuzhiyun if (size > INCFS_MAX_FILE_ATTR_SIZE)
1716*4882a593Smuzhiyun return -E2BIG;
1717*4882a593Smuzhiyun
1718*4882a593Smuzhiyun for (i = 0; i < PSEUDO_FILE_COUNT; ++i)
1719*4882a593Smuzhiyun if (!strcmp(d->d_iname, incfs_pseudo_file_names[i].data))
1720*4882a593Smuzhiyun break;
1721*4882a593Smuzhiyun if (i == PSEUDO_FILE_COUNT)
1722*4882a593Smuzhiyun return -ENODATA;
1723*4882a593Smuzhiyun
1724*4882a593Smuzhiyun stored_value = &mi->pseudo_file_xattr[i].data;
1725*4882a593Smuzhiyun stored_size = &mi->pseudo_file_xattr[i].len;
1726*4882a593Smuzhiyun kfree (*stored_value);
1727*4882a593Smuzhiyun *stored_value = kzalloc(size, GFP_NOFS);
1728*4882a593Smuzhiyun if (!*stored_value)
1729*4882a593Smuzhiyun return -ENOMEM;
1730*4882a593Smuzhiyun
1731*4882a593Smuzhiyun memcpy(*stored_value, value, size);
1732*4882a593Smuzhiyun *stored_size = size;
1733*4882a593Smuzhiyun return 0;
1734*4882a593Smuzhiyun }
1735*4882a593Smuzhiyun
incfs_listxattr(struct dentry * d,char * list,size_t size)1736*4882a593Smuzhiyun static ssize_t incfs_listxattr(struct dentry *d, char *list, size_t size)
1737*4882a593Smuzhiyun {
1738*4882a593Smuzhiyun struct dentry_info *di = get_incfs_dentry(d);
1739*4882a593Smuzhiyun
1740*4882a593Smuzhiyun if (!di || !di->backing_path.dentry)
1741*4882a593Smuzhiyun return -ENODATA;
1742*4882a593Smuzhiyun
1743*4882a593Smuzhiyun return vfs_listxattr(di->backing_path.dentry, list, size);
1744*4882a593Smuzhiyun }
1745*4882a593Smuzhiyun
incfs_mount_fs(struct file_system_type * type,int flags,const char * dev_name,void * data)1746*4882a593Smuzhiyun struct dentry *incfs_mount_fs(struct file_system_type *type, int flags,
1747*4882a593Smuzhiyun const char *dev_name, void *data)
1748*4882a593Smuzhiyun {
1749*4882a593Smuzhiyun struct mount_options options = {};
1750*4882a593Smuzhiyun struct mount_info *mi = NULL;
1751*4882a593Smuzhiyun struct path backing_dir_path = {};
1752*4882a593Smuzhiyun struct dentry *index_dir = NULL;
1753*4882a593Smuzhiyun struct dentry *incomplete_dir = NULL;
1754*4882a593Smuzhiyun struct super_block *src_fs_sb = NULL;
1755*4882a593Smuzhiyun struct inode *root_inode = NULL;
1756*4882a593Smuzhiyun struct super_block *sb = sget(type, NULL, set_anon_super, flags, NULL);
1757*4882a593Smuzhiyun bool dir_created = false;
1758*4882a593Smuzhiyun int error = 0;
1759*4882a593Smuzhiyun
1760*4882a593Smuzhiyun if (IS_ERR(sb))
1761*4882a593Smuzhiyun return ERR_CAST(sb);
1762*4882a593Smuzhiyun
1763*4882a593Smuzhiyun sb->s_op = &incfs_super_ops;
1764*4882a593Smuzhiyun sb->s_d_op = &incfs_dentry_ops;
1765*4882a593Smuzhiyun sb->s_flags |= S_NOATIME;
1766*4882a593Smuzhiyun sb->s_magic = INCFS_MAGIC_NUMBER;
1767*4882a593Smuzhiyun sb->s_time_gran = 1;
1768*4882a593Smuzhiyun sb->s_blocksize = INCFS_DATA_FILE_BLOCK_SIZE;
1769*4882a593Smuzhiyun sb->s_blocksize_bits = blksize_bits(sb->s_blocksize);
1770*4882a593Smuzhiyun sb->s_xattr = incfs_xattr_ops;
1771*4882a593Smuzhiyun
1772*4882a593Smuzhiyun BUILD_BUG_ON(PAGE_SIZE != INCFS_DATA_FILE_BLOCK_SIZE);
1773*4882a593Smuzhiyun
1774*4882a593Smuzhiyun if (!dev_name) {
1775*4882a593Smuzhiyun pr_err("incfs: Backing dir is not set, filesystem can't be mounted.\n");
1776*4882a593Smuzhiyun error = -ENOENT;
1777*4882a593Smuzhiyun goto err_deactivate;
1778*4882a593Smuzhiyun }
1779*4882a593Smuzhiyun
1780*4882a593Smuzhiyun error = parse_options(&options, (char *)data);
1781*4882a593Smuzhiyun if (error != 0) {
1782*4882a593Smuzhiyun pr_err("incfs: Options parsing error. %d\n", error);
1783*4882a593Smuzhiyun goto err_deactivate;
1784*4882a593Smuzhiyun }
1785*4882a593Smuzhiyun
1786*4882a593Smuzhiyun sb->s_bdi->ra_pages = options.readahead_pages;
1787*4882a593Smuzhiyun if (!dev_name) {
1788*4882a593Smuzhiyun pr_err("incfs: Backing dir is not set, filesystem can't be mounted.\n");
1789*4882a593Smuzhiyun error = -ENOENT;
1790*4882a593Smuzhiyun goto err_free_opts;
1791*4882a593Smuzhiyun }
1792*4882a593Smuzhiyun
1793*4882a593Smuzhiyun error = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
1794*4882a593Smuzhiyun &backing_dir_path);
1795*4882a593Smuzhiyun if (error || backing_dir_path.dentry == NULL ||
1796*4882a593Smuzhiyun !d_really_is_positive(backing_dir_path.dentry)) {
1797*4882a593Smuzhiyun pr_err("incfs: Error accessing: %s.\n",
1798*4882a593Smuzhiyun dev_name);
1799*4882a593Smuzhiyun goto err_free_opts;
1800*4882a593Smuzhiyun }
1801*4882a593Smuzhiyun src_fs_sb = backing_dir_path.dentry->d_sb;
1802*4882a593Smuzhiyun sb->s_maxbytes = src_fs_sb->s_maxbytes;
1803*4882a593Smuzhiyun sb->s_stack_depth = src_fs_sb->s_stack_depth + 1;
1804*4882a593Smuzhiyun
1805*4882a593Smuzhiyun if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1806*4882a593Smuzhiyun error = -EINVAL;
1807*4882a593Smuzhiyun goto err_put_path;
1808*4882a593Smuzhiyun }
1809*4882a593Smuzhiyun
1810*4882a593Smuzhiyun mi = incfs_alloc_mount_info(sb, &options, &backing_dir_path);
1811*4882a593Smuzhiyun if (IS_ERR_OR_NULL(mi)) {
1812*4882a593Smuzhiyun error = PTR_ERR(mi);
1813*4882a593Smuzhiyun pr_err("incfs: Error allocating mount info. %d\n", error);
1814*4882a593Smuzhiyun goto err_put_path;
1815*4882a593Smuzhiyun }
1816*4882a593Smuzhiyun
1817*4882a593Smuzhiyun sb->s_fs_info = mi;
1818*4882a593Smuzhiyun mi->mi_backing_dir_path = backing_dir_path;
1819*4882a593Smuzhiyun index_dir = open_or_create_special_dir(backing_dir_path.dentry,
1820*4882a593Smuzhiyun INCFS_INDEX_NAME, &dir_created);
1821*4882a593Smuzhiyun if (IS_ERR_OR_NULL(index_dir)) {
1822*4882a593Smuzhiyun error = PTR_ERR(index_dir);
1823*4882a593Smuzhiyun pr_err("incfs: Can't find or create .index dir in %s\n",
1824*4882a593Smuzhiyun dev_name);
1825*4882a593Smuzhiyun /* No need to null index_dir since we don't put it */
1826*4882a593Smuzhiyun goto err_put_path;
1827*4882a593Smuzhiyun }
1828*4882a593Smuzhiyun
1829*4882a593Smuzhiyun mi->mi_index_dir = index_dir;
1830*4882a593Smuzhiyun mi->mi_index_free = dir_created;
1831*4882a593Smuzhiyun
1832*4882a593Smuzhiyun incomplete_dir = open_or_create_special_dir(backing_dir_path.dentry,
1833*4882a593Smuzhiyun INCFS_INCOMPLETE_NAME,
1834*4882a593Smuzhiyun &dir_created);
1835*4882a593Smuzhiyun if (IS_ERR_OR_NULL(incomplete_dir)) {
1836*4882a593Smuzhiyun error = PTR_ERR(incomplete_dir);
1837*4882a593Smuzhiyun pr_err("incfs: Can't find or create .incomplete dir in %s\n",
1838*4882a593Smuzhiyun dev_name);
1839*4882a593Smuzhiyun /* No need to null incomplete_dir since we don't put it */
1840*4882a593Smuzhiyun goto err_put_path;
1841*4882a593Smuzhiyun }
1842*4882a593Smuzhiyun mi->mi_incomplete_dir = incomplete_dir;
1843*4882a593Smuzhiyun mi->mi_incomplete_free = dir_created;
1844*4882a593Smuzhiyun
1845*4882a593Smuzhiyun root_inode = fetch_regular_inode(sb, backing_dir_path.dentry);
1846*4882a593Smuzhiyun if (IS_ERR(root_inode)) {
1847*4882a593Smuzhiyun error = PTR_ERR(root_inode);
1848*4882a593Smuzhiyun goto err_put_path;
1849*4882a593Smuzhiyun }
1850*4882a593Smuzhiyun
1851*4882a593Smuzhiyun sb->s_root = d_make_root(root_inode);
1852*4882a593Smuzhiyun if (!sb->s_root) {
1853*4882a593Smuzhiyun error = -ENOMEM;
1854*4882a593Smuzhiyun goto err_put_path;
1855*4882a593Smuzhiyun }
1856*4882a593Smuzhiyun error = incfs_init_dentry(sb->s_root, &backing_dir_path);
1857*4882a593Smuzhiyun if (error)
1858*4882a593Smuzhiyun goto err_put_path;
1859*4882a593Smuzhiyun
1860*4882a593Smuzhiyun path_put(&backing_dir_path);
1861*4882a593Smuzhiyun sb->s_flags |= SB_ACTIVE;
1862*4882a593Smuzhiyun
1863*4882a593Smuzhiyun pr_debug("incfs: mount\n");
1864*4882a593Smuzhiyun return dget(sb->s_root);
1865*4882a593Smuzhiyun
1866*4882a593Smuzhiyun err_put_path:
1867*4882a593Smuzhiyun path_put(&backing_dir_path);
1868*4882a593Smuzhiyun err_free_opts:
1869*4882a593Smuzhiyun free_options(&options);
1870*4882a593Smuzhiyun err_deactivate:
1871*4882a593Smuzhiyun deactivate_locked_super(sb);
1872*4882a593Smuzhiyun pr_err("incfs: mount failed %d\n", error);
1873*4882a593Smuzhiyun return ERR_PTR(error);
1874*4882a593Smuzhiyun }
1875*4882a593Smuzhiyun
incfs_remount_fs(struct super_block * sb,int * flags,char * data)1876*4882a593Smuzhiyun static int incfs_remount_fs(struct super_block *sb, int *flags, char *data)
1877*4882a593Smuzhiyun {
1878*4882a593Smuzhiyun struct mount_options options;
1879*4882a593Smuzhiyun struct mount_info *mi = get_mount_info(sb);
1880*4882a593Smuzhiyun int err = 0;
1881*4882a593Smuzhiyun
1882*4882a593Smuzhiyun sync_filesystem(sb);
1883*4882a593Smuzhiyun err = parse_options(&options, (char *)data);
1884*4882a593Smuzhiyun if (err)
1885*4882a593Smuzhiyun return err;
1886*4882a593Smuzhiyun
1887*4882a593Smuzhiyun if (options.report_uid != mi->mi_options.report_uid) {
1888*4882a593Smuzhiyun pr_err("incfs: Can't change report_uid mount option on remount\n");
1889*4882a593Smuzhiyun err = -EOPNOTSUPP;
1890*4882a593Smuzhiyun goto out;
1891*4882a593Smuzhiyun }
1892*4882a593Smuzhiyun
1893*4882a593Smuzhiyun err = incfs_realloc_mount_info(mi, &options);
1894*4882a593Smuzhiyun if (err)
1895*4882a593Smuzhiyun goto out;
1896*4882a593Smuzhiyun
1897*4882a593Smuzhiyun pr_debug("incfs: remount\n");
1898*4882a593Smuzhiyun
1899*4882a593Smuzhiyun out:
1900*4882a593Smuzhiyun free_options(&options);
1901*4882a593Smuzhiyun return err;
1902*4882a593Smuzhiyun }
1903*4882a593Smuzhiyun
incfs_kill_sb(struct super_block * sb)1904*4882a593Smuzhiyun void incfs_kill_sb(struct super_block *sb)
1905*4882a593Smuzhiyun {
1906*4882a593Smuzhiyun struct mount_info *mi = sb->s_fs_info;
1907*4882a593Smuzhiyun struct inode *dinode = NULL;
1908*4882a593Smuzhiyun
1909*4882a593Smuzhiyun pr_debug("incfs: unmount\n");
1910*4882a593Smuzhiyun
1911*4882a593Smuzhiyun if (mi) {
1912*4882a593Smuzhiyun if (mi->mi_backing_dir_path.dentry)
1913*4882a593Smuzhiyun dinode = d_inode(mi->mi_backing_dir_path.dentry);
1914*4882a593Smuzhiyun
1915*4882a593Smuzhiyun if (dinode) {
1916*4882a593Smuzhiyun if (mi->mi_index_dir && mi->mi_index_free)
1917*4882a593Smuzhiyun vfs_rmdir(dinode, mi->mi_index_dir);
1918*4882a593Smuzhiyun
1919*4882a593Smuzhiyun if (mi->mi_incomplete_dir && mi->mi_incomplete_free)
1920*4882a593Smuzhiyun vfs_rmdir(dinode, mi->mi_incomplete_dir);
1921*4882a593Smuzhiyun }
1922*4882a593Smuzhiyun
1923*4882a593Smuzhiyun incfs_free_mount_info(mi);
1924*4882a593Smuzhiyun sb->s_fs_info = NULL;
1925*4882a593Smuzhiyun }
1926*4882a593Smuzhiyun kill_anon_super(sb);
1927*4882a593Smuzhiyun }
1928*4882a593Smuzhiyun
show_options(struct seq_file * m,struct dentry * root)1929*4882a593Smuzhiyun static int show_options(struct seq_file *m, struct dentry *root)
1930*4882a593Smuzhiyun {
1931*4882a593Smuzhiyun struct mount_info *mi = get_mount_info(root->d_sb);
1932*4882a593Smuzhiyun
1933*4882a593Smuzhiyun seq_printf(m, ",read_timeout_ms=%u", mi->mi_options.read_timeout_ms);
1934*4882a593Smuzhiyun seq_printf(m, ",readahead=%u", mi->mi_options.readahead_pages);
1935*4882a593Smuzhiyun if (mi->mi_options.read_log_pages != 0) {
1936*4882a593Smuzhiyun seq_printf(m, ",rlog_pages=%u", mi->mi_options.read_log_pages);
1937*4882a593Smuzhiyun seq_printf(m, ",rlog_wakeup_cnt=%u",
1938*4882a593Smuzhiyun mi->mi_options.read_log_wakeup_count);
1939*4882a593Smuzhiyun }
1940*4882a593Smuzhiyun if (mi->mi_options.report_uid)
1941*4882a593Smuzhiyun seq_puts(m, ",report_uid");
1942*4882a593Smuzhiyun
1943*4882a593Smuzhiyun if (mi->mi_sysfs_node)
1944*4882a593Smuzhiyun seq_printf(m, ",sysfs_name=%s",
1945*4882a593Smuzhiyun kobject_name(&mi->mi_sysfs_node->isn_sysfs_node));
1946*4882a593Smuzhiyun return 0;
1947*4882a593Smuzhiyun }
1948