Blame view

fs/incfs/pseudo_files.c 30.5 KB
8334d69e6   Paul Lawrence   ANDROID: Incremen...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  // SPDX-License-Identifier: GPL-2.0
  /*
   * Copyright 2020 Google LLC
   */
  
  #include <linux/file.h>
  #include <linux/fs.h>
  #include <linux/namei.h>
  #include <linux/poll.h>
  #include <linux/syscalls.h>
  
  #include <uapi/linux/incrementalfs.h>
  
  #include "pseudo_files.h"
  
  #include "data_mgmt.h"
  #include "format.h"
  #include "integrity.h"
  #include "vfs.h"
8334d69e6   Paul Lawrence   ANDROID: Incremen...
20
  #define READ_WRITE_FILE_MODE 0666
b79605a90   Yurii Zubrytskyi   ANDROID: Incremen...
21
  static bool is_pseudo_filename(struct mem_range name);
8334d69e6   Paul Lawrence   ANDROID: Incremen...
22
23
24
25
  
  /*******************************************************************************
   * .pending_reads pseudo file definition
   ******************************************************************************/
b79605a90   Yurii Zubrytskyi   ANDROID: Incremen...
26
  #define INCFS_PENDING_READS_INODE 2
8334d69e6   Paul Lawrence   ANDROID: Incremen...
27
  static const char pending_reads_file_name[] = INCFS_PENDING_READS_FILENAME;
8334d69e6   Paul Lawrence   ANDROID: Incremen...
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
  
  /* State of an open .pending_reads file, unique for each file descriptor. */
  struct pending_reads_state {
  	/* A serial number of the last pending read obtained from this file. */
  	int last_pending_read_sn;
  };
  
  static ssize_t pending_reads_read(struct file *f, char __user *buf, size_t len,
  			    loff_t *ppos)
  {
  	struct pending_reads_state *pr_state = f->private_data;
  	struct mount_info *mi = get_mount_info(file_superblock(f));
  	bool report_uid;
  	unsigned long page = 0;
  	struct incfs_pending_read_info *reads_buf = NULL;
  	struct incfs_pending_read_info2 *reads_buf2 = NULL;
  	size_t record_size;
  	size_t reads_to_collect;
  	int last_known_read_sn = READ_ONCE(pr_state->last_pending_read_sn);
  	int new_max_sn = last_known_read_sn;
  	int reads_collected = 0;
  	ssize_t result = 0;
  
  	if (!mi)
  		return -EFAULT;
  
  	report_uid = mi->mi_options.report_uid;
  	record_size = report_uid ? sizeof(*reads_buf2) : sizeof(*reads_buf);
  	reads_to_collect = len / record_size;
  
  	if (!incfs_fresh_pending_reads_exist(mi, last_known_read_sn))
  		return 0;
  
  	page = get_zeroed_page(GFP_NOFS);
  	if (!page)
  		return -ENOMEM;
  
  	if (report_uid)
  		reads_buf2 = (struct incfs_pending_read_info2 *) page;
  	else
  		reads_buf = (struct incfs_pending_read_info *) page;
  
  	reads_to_collect =
  		min_t(size_t, PAGE_SIZE / record_size, reads_to_collect);
  
  	reads_collected = incfs_collect_pending_reads(mi, last_known_read_sn,
  				reads_buf, reads_buf2, reads_to_collect,
  				&new_max_sn);
  
  	if (reads_collected < 0) {
  		result = reads_collected;
  		goto out;
  	}
  
  	/*
  	 * Just to make sure that we don't accidentally copy more data
  	 * to reads buffer than userspace can handle.
  	 */
  	reads_collected = min_t(size_t, reads_collected, reads_to_collect);
  	result = reads_collected * record_size;
  
  	/* Copy reads info to the userspace buffer */
  	if (copy_to_user(buf, (void *)page, result)) {
  		result = -EFAULT;
  		goto out;
  	}
  
  	WRITE_ONCE(pr_state->last_pending_read_sn, new_max_sn);
  	*ppos = 0;
  
  out:
  	free_page(page);
  	return result;
  }
  
  static __poll_t pending_reads_poll(struct file *file, poll_table *wait)
  {
  	struct pending_reads_state *state = file->private_data;
  	struct mount_info *mi = get_mount_info(file_superblock(file));
  	__poll_t ret = 0;
  
  	poll_wait(file, &mi->mi_pending_reads_notif_wq, wait);
  	if (incfs_fresh_pending_reads_exist(mi,
  					    state->last_pending_read_sn))
  		ret = EPOLLIN | EPOLLRDNORM;
  
  	return ret;
  }
  
  static int pending_reads_open(struct inode *inode, struct file *file)
  {
  	struct pending_reads_state *state = NULL;
  
  	state = kzalloc(sizeof(*state), GFP_NOFS);
  	if (!state)
  		return -ENOMEM;
  
  	file->private_data = state;
  	return 0;
  }
  
  static int pending_reads_release(struct inode *inode, struct file *file)
  {
  	kfree(file->private_data);
  	return 0;
  }
  
  static long ioctl_permit_fill(struct file *f, void __user *arg)
  {
  	struct incfs_permit_fill __user *usr_permit_fill = arg;
  	struct incfs_permit_fill permit_fill;
  	long error = 0;
  	struct file *file = NULL;
9cbdd375f   Paul Lawrence   ANDROID: Incremen...
141
  	struct incfs_file_data *fd;
8334d69e6   Paul Lawrence   ANDROID: Incremen...
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
  
  	if (copy_from_user(&permit_fill, usr_permit_fill, sizeof(permit_fill)))
  		return -EFAULT;
  
  	file = fget(permit_fill.file_descriptor);
  	if (IS_ERR(file))
  		return PTR_ERR(file);
  
  	if (file->f_op != &incfs_file_ops) {
  		error = -EPERM;
  		goto out;
  	}
  
  	if (file->f_inode->i_sb != f->f_inode->i_sb) {
  		error = -EPERM;
  		goto out;
  	}
9cbdd375f   Paul Lawrence   ANDROID: Incremen...
159
160
161
  	fd = file->private_data;
  
  	switch (fd->fd_fill_permission) {
8334d69e6   Paul Lawrence   ANDROID: Incremen...
162
  	case CANT_FILL:
9cbdd375f   Paul Lawrence   ANDROID: Incremen...
163
  		fd->fd_fill_permission = CAN_FILL;
8334d69e6   Paul Lawrence   ANDROID: Incremen...
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
  		break;
  
  	case CAN_FILL:
  		pr_debug("CAN_FILL already set");
  		break;
  
  	default:
  		pr_warn("Invalid file private data");
  		error = -EFAULT;
  		goto out;
  	}
  
  out:
  	fput(file);
  	return error;
  }
  
  static int chmod(struct dentry *dentry, umode_t mode)
  {
  	struct inode *inode = dentry->d_inode;
  	struct inode *delegated_inode = NULL;
  	struct iattr newattrs;
  	int error;
  
  retry_deleg:
  	inode_lock(inode);
  	newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
  	newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  	error = notify_change(dentry, &newattrs, &delegated_inode);
  	inode_unlock(inode);
  	if (delegated_inode) {
  		error = break_deleg_wait(&delegated_inode);
  		if (!error)
  			goto retry_deleg;
  	}
  	return error;
  }
b1cc5f1b6   Paul Lawrence   ANDROID: Incremen...
201
202
203
204
205
206
  static bool incfs_equal_ranges(struct mem_range lhs, struct mem_range rhs)
  {
  	if (lhs.len != rhs.len)
  		return false;
  	return memcmp(lhs.data, rhs.data, lhs.len) == 0;
  }
8334d69e6   Paul Lawrence   ANDROID: Incremen...
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
  static int validate_name(char *file_name)
  {
  	struct mem_range name = range(file_name, strlen(file_name));
  	int i = 0;
  
  	if (name.len > INCFS_MAX_NAME_LEN)
  		return -ENAMETOOLONG;
  
  	if (is_pseudo_filename(name))
  		return -EINVAL;
  
  	for (i = 0; i < name.len; i++)
  		if (name.data[i] == '/')
  			return -EINVAL;
  
  	return 0;
  }
  
  static int dir_relative_path_resolve(
  			struct mount_info *mi,
  			const char __user *relative_path,
  			struct path *result_path)
  {
  	struct path *base_path = &mi->mi_backing_dir_path;
  	int dir_fd = get_unused_fd_flags(0);
  	struct file *dir_f = NULL;
  	int error = 0;
  
  	if (dir_fd < 0)
  		return dir_fd;
a3c935a49   Paul Lawrence   ANDROID: Incremen...
237
  	dir_f = dentry_open(base_path, O_RDONLY | O_NOATIME, current_cred());
8334d69e6   Paul Lawrence   ANDROID: Incremen...
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
  
  	if (IS_ERR(dir_f)) {
  		error = PTR_ERR(dir_f);
  		goto out;
  	}
  	fd_install(dir_fd, dir_f);
  
  	if (!relative_path) {
  		/* No relative path given, just return the base dir. */
  		*result_path = *base_path;
  		path_get(result_path);
  		goto out;
  	}
  
  	error = user_path_at_empty(dir_fd, relative_path,
  		LOOKUP_FOLLOW | LOOKUP_DIRECTORY, result_path, NULL);
  
  out:
  	ksys_close(dir_fd);
  	if (error)
  		pr_debug("incfs: %s %d
  ", __func__, error);
  	return error;
  }
  
  static struct mem_range incfs_copy_signature_info_from_user(u8 __user *original,
  							    u64 size)
  {
  	u8 *result;
  
  	if (!original)
  		return range(NULL, 0);
  
  	if (size > INCFS_MAX_SIGNATURE_SIZE)
  		return range(ERR_PTR(-EFAULT), 0);
  
  	result = kzalloc(size, GFP_NOFS | __GFP_COMP);
  	if (!result)
  		return range(ERR_PTR(-ENOMEM), 0);
  
  	if (copy_from_user(result, original, size)) {
  		kfree(result);
  		return range(ERR_PTR(-EFAULT), 0);
  	}
  
  	return range(result, size);
  }
  
  static int init_new_file(struct mount_info *mi, struct dentry *dentry,
  			 incfs_uuid_t *uuid, u64 size, struct mem_range attr,
  			 u8 __user *user_signature_info, u64 signature_size)
  {
  	struct path path = {};
  	struct file *new_file;
  	int error = 0;
  	struct backing_file_context *bfc = NULL;
  	u32 block_count;
  	struct mem_range raw_signature = { NULL };
  	struct mtree *hash_tree = NULL;
  
  	if (!mi || !dentry || !uuid)
  		return -EFAULT;
  
  	/* Resize newly created file to its true size. */
  	path = (struct path) {
  		.mnt = mi->mi_backing_dir_path.mnt,
  		.dentry = dentry
  	};
a3c935a49   Paul Lawrence   ANDROID: Incremen...
306

8334d69e6   Paul Lawrence   ANDROID: Incremen...
307
  	new_file = dentry_open(&path, O_RDWR | O_NOATIME | O_LARGEFILE,
a3c935a49   Paul Lawrence   ANDROID: Incremen...
308
  			       current_cred());
8334d69e6   Paul Lawrence   ANDROID: Incremen...
309
310
311
312
313
  
  	if (IS_ERR(new_file)) {
  		error = PTR_ERR(new_file);
  		goto out;
  	}
dedf46b2a   Paul Lawrence   ANDROID: Incremen...
314
  	bfc = incfs_alloc_bfc(mi, new_file);
8334d69e6   Paul Lawrence   ANDROID: Incremen...
315
316
317
318
319
320
321
322
323
324
325
  	fput(new_file);
  	if (IS_ERR(bfc)) {
  		error = PTR_ERR(bfc);
  		bfc = NULL;
  		goto out;
  	}
  
  	mutex_lock(&bfc->bc_mutex);
  	error = incfs_write_fh_to_backing_file(bfc, uuid, size);
  	if (error)
  		goto out;
8334d69e6   Paul Lawrence   ANDROID: Incremen...
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
  	block_count = (u32)get_blocks_count_for_size(size);
  
  	if (user_signature_info) {
  		raw_signature = incfs_copy_signature_info_from_user(
  			user_signature_info, signature_size);
  
  		if (IS_ERR(raw_signature.data)) {
  			error = PTR_ERR(raw_signature.data);
  			raw_signature.data = NULL;
  			goto out;
  		}
  
  		hash_tree = incfs_alloc_mtree(raw_signature, block_count);
  		if (IS_ERR(hash_tree)) {
  			error = PTR_ERR(hash_tree);
  			hash_tree = NULL;
  			goto out;
  		}
  
  		error = incfs_write_signature_to_backing_file(
  			bfc, raw_signature, hash_tree->hash_tree_area_size);
  		if (error)
  			goto out;
  
  		block_count += get_blocks_count_for_size(
  			hash_tree->hash_tree_area_size);
  	}
  
  	if (block_count)
  		error = incfs_write_blockmap_to_backing_file(bfc, block_count);
  
  	if (error)
  		goto out;
  out:
  	if (bfc) {
  		mutex_unlock(&bfc->bc_mutex);
  		incfs_free_bfc(bfc);
  	}
  	incfs_free_mtree(hash_tree);
  	kfree(raw_signature.data);
  
  	if (error)
  		pr_debug("incfs: %s error: %d
  ", __func__, error);
  	return error;
  }
  
  static long ioctl_create_file(struct mount_info *mi,
  			struct incfs_new_file_args __user *usr_args)
  {
  	struct incfs_new_file_args args;
  	char *file_id_str = NULL;
  	struct dentry *index_file_dentry = NULL;
  	struct dentry *named_file_dentry = NULL;
7e7bfa94e   Paul Lawrence   ANDROID: Incremen...
380
  	struct dentry *incomplete_file_dentry = NULL;
8334d69e6   Paul Lawrence   ANDROID: Incremen...
381
382
383
384
385
386
387
  	struct path parent_dir_path = {};
  	struct inode *index_dir_inode = NULL;
  	__le64 size_attr_value = 0;
  	char *file_name = NULL;
  	char *attr_value = NULL;
  	int error = 0;
  	bool locked = false;
7e7bfa94e   Paul Lawrence   ANDROID: Incremen...
388
389
390
  	bool index_linked = false;
  	bool name_linked = false;
  	bool incomplete_linked = false;
8334d69e6   Paul Lawrence   ANDROID: Incremen...
391

7e7bfa94e   Paul Lawrence   ANDROID: Incremen...
392
  	if (!mi || !mi->mi_index_dir || !mi->mi_incomplete_dir) {
8334d69e6   Paul Lawrence   ANDROID: Incremen...
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
  		error = -EFAULT;
  		goto out;
  	}
  
  	if (copy_from_user(&args, usr_args, sizeof(args)) > 0) {
  		error = -EFAULT;
  		goto out;
  	}
  
  	file_name = strndup_user(u64_to_user_ptr(args.file_name), PATH_MAX);
  	if (IS_ERR(file_name)) {
  		error = PTR_ERR(file_name);
  		file_name = NULL;
  		goto out;
  	}
  
  	error = validate_name(file_name);
  	if (error)
  		goto out;
  
  	file_id_str = file_id_to_str(args.file_id);
  	if (!file_id_str) {
  		error = -ENOMEM;
  		goto out;
  	}
  
  	error = mutex_lock_interruptible(&mi->mi_dir_struct_mutex);
  	if (error)
  		goto out;
  	locked = true;
  
  	/* Find a directory to put the file into. */
  	error = dir_relative_path_resolve(mi,
  			u64_to_user_ptr(args.directory_path),
  			&parent_dir_path);
  	if (error)
  		goto out;
  
  	if (parent_dir_path.dentry == mi->mi_index_dir) {
  		/* Can't create a file directly inside .index */
  		error = -EBUSY;
  		goto out;
  	}
7e7bfa94e   Paul Lawrence   ANDROID: Incremen...
436
437
438
439
440
  	if (parent_dir_path.dentry == mi->mi_incomplete_dir) {
  		/* Can't create a file directly inside .incomplete */
  		error = -EBUSY;
  		goto out;
  	}
8334d69e6   Paul Lawrence   ANDROID: Incremen...
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
  	/* Look up a dentry in the parent dir. It should be negative. */
  	named_file_dentry = incfs_lookup_dentry(parent_dir_path.dentry,
  					file_name);
  	if (!named_file_dentry) {
  		error = -EFAULT;
  		goto out;
  	}
  	if (IS_ERR(named_file_dentry)) {
  		error = PTR_ERR(named_file_dentry);
  		named_file_dentry = NULL;
  		goto out;
  	}
  	if (d_really_is_positive(named_file_dentry)) {
  		/* File with this path already exists. */
  		error = -EEXIST;
  		goto out;
  	}
7e7bfa94e   Paul Lawrence   ANDROID: Incremen...
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
  
  	/* Look up a dentry in the incomplete dir. It should be negative. */
  	incomplete_file_dentry = incfs_lookup_dentry(mi->mi_incomplete_dir,
  					file_id_str);
  	if (!incomplete_file_dentry) {
  		error = -EFAULT;
  		goto out;
  	}
  	if (IS_ERR(incomplete_file_dentry)) {
  		error = PTR_ERR(incomplete_file_dentry);
  		incomplete_file_dentry = NULL;
  		goto out;
  	}
  	if (d_really_is_positive(incomplete_file_dentry)) {
  		/* File with this path already exists. */
  		error = -EEXIST;
  		goto out;
  	}
8334d69e6   Paul Lawrence   ANDROID: Incremen...
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
  	/* Look up a dentry in the .index dir. It should be negative. */
  	index_file_dentry = incfs_lookup_dentry(mi->mi_index_dir, file_id_str);
  	if (!index_file_dentry) {
  		error = -EFAULT;
  		goto out;
  	}
  	if (IS_ERR(index_file_dentry)) {
  		error = PTR_ERR(index_file_dentry);
  		index_file_dentry = NULL;
  		goto out;
  	}
  	if (d_really_is_positive(index_file_dentry)) {
  		/* File with this ID already exists in index. */
  		error = -EEXIST;
  		goto out;
  	}
  
  	/* Creating a file in the .index dir. */
  	index_dir_inode = d_inode(mi->mi_index_dir);
  	inode_lock_nested(index_dir_inode, I_MUTEX_PARENT);
  	error = vfs_create(index_dir_inode, index_file_dentry, args.mode | 0222,
  			   true);
  	inode_unlock(index_dir_inode);
  
  	if (error)
  		goto out;
  	if (!d_really_is_positive(index_file_dentry)) {
  		error = -EINVAL;
  		goto out;
  	}
  
  	error = chmod(index_file_dentry, args.mode | 0222);
  	if (error) {
  		pr_debug("incfs: chmod err: %d
  ", error);
7e7bfa94e   Paul Lawrence   ANDROID: Incremen...
511
  		goto out;
8334d69e6   Paul Lawrence   ANDROID: Incremen...
512
513
514
515
516
517
518
519
  	}
  
  	/* Save the file's ID as an xattr for easy fetching in future. */
  	error = vfs_setxattr(index_file_dentry, INCFS_XATTR_ID_NAME,
  		file_id_str, strlen(file_id_str), XATTR_CREATE);
  	if (error) {
  		pr_debug("incfs: vfs_setxattr err:%d
  ", error);
7e7bfa94e   Paul Lawrence   ANDROID: Incremen...
520
  		goto out;
8334d69e6   Paul Lawrence   ANDROID: Incremen...
521
522
523
524
525
526
527
528
529
530
  	}
  
  	/* Save the file's size as an xattr for easy fetching in future. */
  	size_attr_value = cpu_to_le64(args.size);
  	error = vfs_setxattr(index_file_dentry, INCFS_XATTR_SIZE_NAME,
  		(char *)&size_attr_value, sizeof(size_attr_value),
  		XATTR_CREATE);
  	if (error) {
  		pr_debug("incfs: vfs_setxattr err:%d
  ", error);
7e7bfa94e   Paul Lawrence   ANDROID: Incremen...
531
  		goto out;
8334d69e6   Paul Lawrence   ANDROID: Incremen...
532
533
534
535
536
537
  	}
  
  	/* Save the file's attribute as an xattr */
  	if (args.file_attr_len && args.file_attr) {
  		if (args.file_attr_len > INCFS_MAX_FILE_ATTR_SIZE) {
  			error = -E2BIG;
7e7bfa94e   Paul Lawrence   ANDROID: Incremen...
538
  			goto out;
8334d69e6   Paul Lawrence   ANDROID: Incremen...
539
540
541
542
543
  		}
  
  		attr_value = kmalloc(args.file_attr_len, GFP_NOFS);
  		if (!attr_value) {
  			error = -ENOMEM;
7e7bfa94e   Paul Lawrence   ANDROID: Incremen...
544
  			goto out;
8334d69e6   Paul Lawrence   ANDROID: Incremen...
545
546
547
548
549
550
  		}
  
  		if (copy_from_user(attr_value,
  				u64_to_user_ptr(args.file_attr),
  				args.file_attr_len) > 0) {
  			error = -EFAULT;
7e7bfa94e   Paul Lawrence   ANDROID: Incremen...
551
  			goto out;
8334d69e6   Paul Lawrence   ANDROID: Incremen...
552
553
554
555
556
557
558
559
  		}
  
  		error = vfs_setxattr(index_file_dentry,
  				INCFS_XATTR_METADATA_NAME,
  				attr_value, args.file_attr_len,
  				XATTR_CREATE);
  
  		if (error)
7e7bfa94e   Paul Lawrence   ANDROID: Incremen...
560
  			goto out;
8334d69e6   Paul Lawrence   ANDROID: Incremen...
561
562
563
564
565
566
567
568
  	}
  
  	/* Initializing a newly created file. */
  	error = init_new_file(mi, index_file_dentry, &args.file_id, args.size,
  			      range(attr_value, args.file_attr_len),
  			      (u8 __user *)args.signature_info,
  			      args.signature_size);
  	if (error)
7e7bfa94e   Paul Lawrence   ANDROID: Incremen...
569
570
  		goto out;
  	index_linked = true;
8334d69e6   Paul Lawrence   ANDROID: Incremen...
571
572
573
  
  	/* Linking a file with its real name from the requested dir. */
  	error = incfs_link(index_file_dentry, named_file_dentry);
7e7bfa94e   Paul Lawrence   ANDROID: Incremen...
574
  	if (error)
8334d69e6   Paul Lawrence   ANDROID: Incremen...
575
  		goto out;
7e7bfa94e   Paul Lawrence   ANDROID: Incremen...
576
  	name_linked = true;
8334d69e6   Paul Lawrence   ANDROID: Incremen...
577

7e7bfa94e   Paul Lawrence   ANDROID: Incremen...
578
579
580
581
582
583
584
  	if (args.size) {
  		/* Linking a file with its incomplete entry */
  		error = incfs_link(index_file_dentry, incomplete_file_dentry);
  		if (error)
  			goto out;
  		incomplete_linked = true;
  	}
8334d69e6   Paul Lawrence   ANDROID: Incremen...
585
586
  
  out:
7e7bfa94e   Paul Lawrence   ANDROID: Incremen...
587
  	if (error) {
8334d69e6   Paul Lawrence   ANDROID: Incremen...
588
589
  		pr_debug("incfs: %s err:%d
  ", __func__, error);
7e7bfa94e   Paul Lawrence   ANDROID: Incremen...
590
591
592
593
594
595
596
  		if (index_linked)
  			incfs_unlink(index_file_dentry);
  		if (name_linked)
  			incfs_unlink(named_file_dentry);
  		if (incomplete_linked)
  			incfs_unlink(incomplete_file_dentry);
  	}
8334d69e6   Paul Lawrence   ANDROID: Incremen...
597
598
599
600
601
602
  
  	kfree(file_id_str);
  	kfree(file_name);
  	kfree(attr_value);
  	dput(named_file_dentry);
  	dput(index_file_dentry);
7e7bfa94e   Paul Lawrence   ANDROID: Incremen...
603
  	dput(incomplete_file_dentry);
8334d69e6   Paul Lawrence   ANDROID: Incremen...
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
  	path_put(&parent_dir_path);
  	if (locked)
  		mutex_unlock(&mi->mi_dir_struct_mutex);
  	return error;
  }
  
  static int init_new_mapped_file(struct mount_info *mi, struct dentry *dentry,
  			 incfs_uuid_t *uuid, u64 size, u64 offset)
  {
  	struct path path = {};
  	struct file *new_file;
  	int error = 0;
  	struct backing_file_context *bfc = NULL;
  
  	if (!mi || !dentry || !uuid)
  		return -EFAULT;
  
  	/* Resize newly created file to its true size. */
  	path = (struct path) {
  		.mnt = mi->mi_backing_dir_path.mnt,
  		.dentry = dentry
  	};
  	new_file = dentry_open(&path, O_RDWR | O_NOATIME | O_LARGEFILE,
a3c935a49   Paul Lawrence   ANDROID: Incremen...
627
  			       current_cred());
8334d69e6   Paul Lawrence   ANDROID: Incremen...
628
629
630
631
632
  
  	if (IS_ERR(new_file)) {
  		error = PTR_ERR(new_file);
  		goto out;
  	}
dedf46b2a   Paul Lawrence   ANDROID: Incremen...
633
  	bfc = incfs_alloc_bfc(mi, new_file);
8334d69e6   Paul Lawrence   ANDROID: Incremen...
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
  	fput(new_file);
  	if (IS_ERR(bfc)) {
  		error = PTR_ERR(bfc);
  		bfc = NULL;
  		goto out;
  	}
  
  	mutex_lock(&bfc->bc_mutex);
  	error = incfs_write_mapping_fh_to_backing_file(bfc, uuid, size, offset);
  	if (error)
  		goto out;
  
  out:
  	if (bfc) {
  		mutex_unlock(&bfc->bc_mutex);
  		incfs_free_bfc(bfc);
  	}
  
  	if (error)
  		pr_debug("incfs: %s error: %d
  ", __func__, error);
  	return error;
  }
  
  static long ioctl_create_mapped_file(struct mount_info *mi, void __user *arg)
  {
  	struct incfs_create_mapped_file_args __user *args_usr_ptr = arg;
  	struct incfs_create_mapped_file_args args = {};
  	char *file_name;
  	int error = 0;
  	struct path parent_dir_path = {};
  	char *source_file_name = NULL;
  	struct dentry *source_file_dentry = NULL;
  	u64 source_file_size;
  	struct dentry *file_dentry = NULL;
  	struct inode *parent_inode;
  	__le64 size_attr_value;
  
  	if (copy_from_user(&args, args_usr_ptr, sizeof(args)) > 0)
  		return -EINVAL;
  
  	file_name = strndup_user(u64_to_user_ptr(args.file_name), PATH_MAX);
  	if (IS_ERR(file_name)) {
  		error = PTR_ERR(file_name);
  		file_name = NULL;
  		goto out;
  	}
  
  	error = validate_name(file_name);
  	if (error)
  		goto out;
  
  	if (args.source_offset % INCFS_DATA_FILE_BLOCK_SIZE) {
  		error = -EINVAL;
  		goto out;
  	}
  
  	/* Validate file mapping is in range */
  	source_file_name = file_id_to_str(args.source_file_id);
  	if (!source_file_name) {
  		pr_warn("Failed to alloc source_file_name
  ");
  		error = -ENOMEM;
  		goto out;
  	}
  
  	source_file_dentry = incfs_lookup_dentry(mi->mi_index_dir,
  						       source_file_name);
  	if (!source_file_dentry) {
  		pr_warn("Source file does not exist
  ");
  		error = -EINVAL;
  		goto out;
  	}
  	if (IS_ERR(source_file_dentry)) {
  		pr_warn("Error opening source file
  ");
  		error = PTR_ERR(source_file_dentry);
  		source_file_dentry = NULL;
  		goto out;
  	}
  	if (!d_really_is_positive(source_file_dentry)) {
  		pr_warn("Source file dentry negative
  ");
  		error = -EINVAL;
  		goto out;
  	}
  
  	error = vfs_getxattr(source_file_dentry, INCFS_XATTR_SIZE_NAME,
  			     (char *)&size_attr_value, sizeof(size_attr_value));
  	if (error < 0)
  		goto out;
  
  	if (error != sizeof(size_attr_value)) {
  		pr_warn("Mapped file has no size attr
  ");
  		error = -EINVAL;
  		goto out;
  	}
  
  	source_file_size = le64_to_cpu(size_attr_value);
  	if (args.source_offset + args.size > source_file_size) {
  		pr_warn("Mapped file out of range
  ");
  		error = -EINVAL;
  		goto out;
  	}
  
  	/* Find a directory to put the file into. */
  	error = dir_relative_path_resolve(mi,
  			u64_to_user_ptr(args.directory_path),
  			&parent_dir_path);
  	if (error)
  		goto out;
  
  	if (parent_dir_path.dentry == mi->mi_index_dir) {
  		/* Can't create a file directly inside .index */
  		error = -EBUSY;
  		goto out;
  	}
  
  	/* Look up a dentry in the parent dir. It should be negative. */
  	file_dentry = incfs_lookup_dentry(parent_dir_path.dentry,
  					file_name);
  	if (!file_dentry) {
  		error = -EFAULT;
  		goto out;
  	}
  	if (IS_ERR(file_dentry)) {
  		error = PTR_ERR(file_dentry);
  		file_dentry = NULL;
  		goto out;
  	}
  	if (d_really_is_positive(file_dentry)) {
  		error = -EEXIST;
  		goto out;
  	}
  
  	parent_inode = d_inode(parent_dir_path.dentry);
  	inode_lock_nested(parent_inode, I_MUTEX_PARENT);
  	error = vfs_create(parent_inode, file_dentry, args.mode | 0222, true);
  	inode_unlock(parent_inode);
  	if (error)
  		goto out;
  
  	/* Save the file's size as an xattr for easy fetching in future. */
  	size_attr_value = cpu_to_le64(args.size);
  	error = vfs_setxattr(file_dentry, INCFS_XATTR_SIZE_NAME,
  		(char *)&size_attr_value, sizeof(size_attr_value),
  		XATTR_CREATE);
  	if (error) {
  		pr_debug("incfs: vfs_setxattr err:%d
  ", error);
  		goto delete_file;
  	}
  
  	error = init_new_mapped_file(mi, file_dentry, &args.source_file_id,
23516028f   Paul Lawrence   ANDROID: Incremen...
791
  			args.size, args.source_offset);
8334d69e6   Paul Lawrence   ANDROID: Incremen...
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
  	if (error)
  		goto delete_file;
  
  	goto out;
  
  delete_file:
  	incfs_unlink(file_dentry);
  
  out:
  	dput(file_dentry);
  	dput(source_file_dentry);
  	path_put(&parent_dir_path);
  	kfree(file_name);
  	kfree(source_file_name);
  	return error;
  }
142953a07   Paul Lawrence   ANDROID: Incremen...
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
  static long ioctl_get_read_timeouts(struct mount_info *mi, void __user *arg)
  {
  	struct incfs_get_read_timeouts_args __user *args_usr_ptr = arg;
  	struct incfs_get_read_timeouts_args args = {};
  	int error = 0;
  	struct incfs_per_uid_read_timeouts *buffer;
  	int size;
  
  	if (copy_from_user(&args, args_usr_ptr, sizeof(args)))
  		return -EINVAL;
  
  	if (args.timeouts_array_size_out > INCFS_DATA_FILE_BLOCK_SIZE)
  		return -EINVAL;
  
  	buffer = kzalloc(args.timeouts_array_size_out, GFP_NOFS);
  	if (!buffer)
  		return -ENOMEM;
  
  	spin_lock(&mi->mi_per_uid_read_timeouts_lock);
  	size = mi->mi_per_uid_read_timeouts_size;
  	if (args.timeouts_array_size < size)
  		error = -E2BIG;
  	else if (size)
  		memcpy(buffer, mi->mi_per_uid_read_timeouts, size);
  	spin_unlock(&mi->mi_per_uid_read_timeouts_lock);
  
  	args.timeouts_array_size_out = size;
  	if (!error && size)
  		if (copy_to_user(u64_to_user_ptr(args.timeouts_array), buffer,
  				 size))
  			error = -EFAULT;
  
  	if (!error || error == -E2BIG)
  		if (copy_to_user(args_usr_ptr, &args, sizeof(args)) > 0)
  			error = -EFAULT;
  
  	kfree(buffer);
  	return error;
  }
  
  static long ioctl_set_read_timeouts(struct mount_info *mi, void __user *arg)
  {
  	struct incfs_set_read_timeouts_args __user *args_usr_ptr = arg;
  	struct incfs_set_read_timeouts_args args = {};
  	int error = 0;
  	int size;
  	struct incfs_per_uid_read_timeouts *buffer = NULL, *tmp;
  	int i;
  
  	if (copy_from_user(&args, args_usr_ptr, sizeof(args)))
  		return -EINVAL;
  
  	size = args.timeouts_array_size;
  	if (size) {
  		if (size > INCFS_DATA_FILE_BLOCK_SIZE ||
  		    size % sizeof(*buffer) != 0)
  			return -EINVAL;
  
  		buffer = kzalloc(size, GFP_NOFS);
  		if (!buffer)
  			return -ENOMEM;
  
  		if (copy_from_user(buffer, u64_to_user_ptr(args.timeouts_array),
  				   size)) {
  			error = -EINVAL;
  			goto out;
  		}
  
  		for (i = 0; i < size / sizeof(*buffer); ++i) {
  			struct incfs_per_uid_read_timeouts *t = &buffer[i];
5ef8ab717   Paul Lawrence   ANDROID: Incremen...
878
  			if (t->min_pending_time_us > t->max_pending_time_us) {
142953a07   Paul Lawrence   ANDROID: Incremen...
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
  				error = -EINVAL;
  				goto out;
  			}
  		}
  	}
  
  	spin_lock(&mi->mi_per_uid_read_timeouts_lock);
  	mi->mi_per_uid_read_timeouts_size = size;
  	tmp = mi->mi_per_uid_read_timeouts;
  	mi->mi_per_uid_read_timeouts = buffer;
  	buffer = tmp;
  	spin_unlock(&mi->mi_per_uid_read_timeouts_lock);
  
  out:
  	kfree(buffer);
  	return error;
  }
8334d69e6   Paul Lawrence   ANDROID: Incremen...
896
897
898
899
900
901
902
903
904
905
906
907
  static long pending_reads_dispatch_ioctl(struct file *f, unsigned int req,
  					unsigned long arg)
  {
  	struct mount_info *mi = get_mount_info(file_superblock(f));
  
  	switch (req) {
  	case INCFS_IOC_CREATE_FILE:
  		return ioctl_create_file(mi, (void __user *)arg);
  	case INCFS_IOC_PERMIT_FILL:
  		return ioctl_permit_fill(f, (void __user *)arg);
  	case INCFS_IOC_CREATE_MAPPED_FILE:
  		return ioctl_create_mapped_file(mi, (void __user *)arg);
142953a07   Paul Lawrence   ANDROID: Incremen...
908
909
910
911
  	case INCFS_IOC_GET_READ_TIMEOUTS:
  		return ioctl_get_read_timeouts(mi, (void __user *)arg);
  	case INCFS_IOC_SET_READ_TIMEOUTS:
  		return ioctl_set_read_timeouts(mi, (void __user *)arg);
8334d69e6   Paul Lawrence   ANDROID: Incremen...
912
913
914
915
  	default:
  		return -EINVAL;
  	}
  }
b79605a90   Yurii Zubrytskyi   ANDROID: Incremen...
916
  static const struct file_operations incfs_pending_reads_file_ops = {
8334d69e6   Paul Lawrence   ANDROID: Incremen...
917
918
919
920
921
922
923
924
925
926
  	.read = pending_reads_read,
  	.poll = pending_reads_poll,
  	.open = pending_reads_open,
  	.release = pending_reads_release,
  	.llseek = noop_llseek,
  	.unlocked_ioctl = pending_reads_dispatch_ioctl,
  	.compat_ioctl = pending_reads_dispatch_ioctl
  };
  
  /*******************************************************************************
b79605a90   Yurii Zubrytskyi   ANDROID: Incremen...
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
   * .log pseudo file definition
   ******************************************************************************/
  #define INCFS_LOG_INODE 3
  static const char log_file_name[] = INCFS_LOG_FILENAME;
  
  /* State of an open .log file, unique for each file descriptor. */
  struct log_file_state {
  	struct read_log_state state;
  };
  
  static ssize_t log_read(struct file *f, char __user *buf, size_t len,
  			loff_t *ppos)
  {
  	struct log_file_state *log_state = f->private_data;
  	struct mount_info *mi = get_mount_info(file_superblock(f));
  	int total_reads_collected = 0;
  	int rl_size;
  	ssize_t result = 0;
  	bool report_uid;
  	unsigned long page = 0;
  	struct incfs_pending_read_info *reads_buf = NULL;
  	struct incfs_pending_read_info2 *reads_buf2 = NULL;
  	size_t record_size;
  	ssize_t reads_to_collect;
  	ssize_t reads_per_page;
  
  	if (!mi)
  		return -EFAULT;
  
  	report_uid = mi->mi_options.report_uid;
  	record_size = report_uid ? sizeof(*reads_buf2) : sizeof(*reads_buf);
  	reads_to_collect = len / record_size;
  	reads_per_page = PAGE_SIZE / record_size;
  
  	rl_size = READ_ONCE(mi->mi_log.rl_size);
  	if (rl_size == 0)
  		return 0;
  
  	page = __get_free_page(GFP_NOFS);
  	if (!page)
  		return -ENOMEM;
  
  	if (report_uid)
  		reads_buf2 = (struct incfs_pending_read_info2 *)page;
  	else
  		reads_buf = (struct incfs_pending_read_info *)page;
  
  	reads_to_collect = min_t(ssize_t, rl_size, reads_to_collect);
  	while (reads_to_collect > 0) {
  		struct read_log_state next_state;
  		int reads_collected;
  
  		memcpy(&next_state, &log_state->state, sizeof(next_state));
  		reads_collected = incfs_collect_logged_reads(
  			mi, &next_state, reads_buf, reads_buf2,
  			min_t(ssize_t, reads_to_collect, reads_per_page));
  		if (reads_collected <= 0) {
  			result = total_reads_collected ?
  					       total_reads_collected * record_size :
  					       reads_collected;
  			goto out;
  		}
  		if (copy_to_user(buf, (void *)page,
  				 reads_collected * record_size)) {
  			result = total_reads_collected ?
  					       total_reads_collected * record_size :
  					       -EFAULT;
  			goto out;
  		}
  
  		memcpy(&log_state->state, &next_state, sizeof(next_state));
  		total_reads_collected += reads_collected;
  		buf += reads_collected * record_size;
  		reads_to_collect -= reads_collected;
  	}
  
  	result = total_reads_collected * record_size;
  	*ppos = 0;
  out:
  	free_page(page);
  	return result;
  }
  
  static __poll_t log_poll(struct file *file, poll_table *wait)
  {
  	struct log_file_state *log_state = file->private_data;
  	struct mount_info *mi = get_mount_info(file_superblock(file));
  	int count;
  	__poll_t ret = 0;
  
  	poll_wait(file, &mi->mi_log.ml_notif_wq, wait);
  	count = incfs_get_uncollected_logs_count(mi, &log_state->state);
  	if (count >= mi->mi_options.read_log_wakeup_count)
  		ret = EPOLLIN | EPOLLRDNORM;
  
  	return ret;
  }
  
  static int log_open(struct inode *inode, struct file *file)
  {
  	struct log_file_state *log_state = NULL;
  	struct mount_info *mi = get_mount_info(file_superblock(file));
  
  	log_state = kzalloc(sizeof(*log_state), GFP_NOFS);
  	if (!log_state)
  		return -ENOMEM;
  
  	log_state->state = incfs_get_log_state(mi);
  	file->private_data = log_state;
  	return 0;
  }
  
  static int log_release(struct inode *inode, struct file *file)
  {
  	kfree(file->private_data);
  	return 0;
  }
  
  static const struct file_operations incfs_log_file_ops = {
  	.read = log_read,
  	.poll = log_poll,
  	.open = log_open,
  	.release = log_release,
  	.llseek = noop_llseek,
  };
  
  /*******************************************************************************
cb776f457   Paul Lawrence   ANDROID: Incremen...
1054
1055
   * .blocks_written pseudo file definition
   ******************************************************************************/
b79605a90   Yurii Zubrytskyi   ANDROID: Incremen...
1056
  #define INCFS_BLOCKS_WRITTEN_INODE 4
cb776f457   Paul Lawrence   ANDROID: Incremen...
1057
  static const char blocks_written_file_name[] = INCFS_BLOCKS_WRITTEN_FILENAME;
cb776f457   Paul Lawrence   ANDROID: Incremen...
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
  
  /* State of an open .blocks_written file, unique for each file descriptor. */
  struct blocks_written_file_state {
  	unsigned long blocks_written;
  };
  
  static ssize_t blocks_written_read(struct file *f, char __user *buf, size_t len,
  			loff_t *ppos)
  {
  	struct mount_info *mi = get_mount_info(file_superblock(f));
  	struct blocks_written_file_state *state = f->private_data;
  	unsigned long blocks_written;
  	char string[21];
  	int result = 0;
  
  	if (!mi)
  		return -EFAULT;
  
  	blocks_written = atomic_read(&mi->mi_blocks_written);
  	if (state->blocks_written == blocks_written)
  		return 0;
  
  	result = snprintf(string, sizeof(string), "%lu", blocks_written);
  	if (result > len)
  		result = len;
  	if (copy_to_user(buf, string, result))
  		return -EFAULT;
  
  	state->blocks_written = blocks_written;
  	return result;
  }
  
  static __poll_t blocks_written_poll(struct file *f, poll_table *wait)
  {
  	struct mount_info *mi = get_mount_info(file_superblock(f));
  	struct blocks_written_file_state *state = f->private_data;
  	unsigned long blocks_written;
  
  	if (!mi)
23516028f   Paul Lawrence   ANDROID: Incremen...
1097
  		return 0;
cb776f457   Paul Lawrence   ANDROID: Incremen...
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
  
  	poll_wait(f, &mi->mi_blocks_written_notif_wq, wait);
  	blocks_written = atomic_read(&mi->mi_blocks_written);
  	if (state->blocks_written == blocks_written)
  		return 0;
  
  	return EPOLLIN | EPOLLRDNORM;
  }
  
  static int blocks_written_open(struct inode *inode, struct file *file)
  {
  	struct blocks_written_file_state *state =
  		kzalloc(sizeof(*state), GFP_NOFS);
  
  	if (!state)
  		return -ENOMEM;
  
  	state->blocks_written = -1;
  	file->private_data = state;
  	return 0;
  }
  
  static int blocks_written_release(struct inode *inode, struct file *file)
  {
  	kfree(file->private_data);
  	return 0;
  }
  
  static const struct file_operations incfs_blocks_written_file_ops = {
  	.read = blocks_written_read,
  	.poll = blocks_written_poll,
  	.open = blocks_written_open,
  	.release = blocks_written_release,
  	.llseek = noop_llseek,
  };
  
  /*******************************************************************************
8334d69e6   Paul Lawrence   ANDROID: Incremen...
1135
1136
   * Generic inode lookup functionality
   ******************************************************************************/
b79605a90   Yurii Zubrytskyi   ANDROID: Incremen...
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
  
  const struct mem_range incfs_pseudo_file_names[] = {
  	{ .data = (u8 *)pending_reads_file_name,
  	  .len = ARRAY_SIZE(pending_reads_file_name) - 1 },
  	{ .data = (u8 *)log_file_name, .len = ARRAY_SIZE(log_file_name) - 1 },
  	{ .data = (u8 *)blocks_written_file_name,
  	  .len = ARRAY_SIZE(blocks_written_file_name) - 1 }
  };
  
  const unsigned long incfs_pseudo_file_inodes[] = { INCFS_PENDING_READS_INODE,
  						   INCFS_LOG_INODE,
  						   INCFS_BLOCKS_WRITTEN_INODE };
  
  static const struct file_operations *const pseudo_file_operations[] = {
  	&incfs_pending_reads_file_ops, &incfs_log_file_ops,
  	&incfs_blocks_written_file_ops
  };
  
  static bool is_pseudo_filename(struct mem_range name)
  {
  	int i = 0;
  
  	for (; i < ARRAY_SIZE(incfs_pseudo_file_names); ++i)
  		if (incfs_equal_ranges(incfs_pseudo_file_names[i], name))
  			return true;
  	return false;
  }
8334d69e6   Paul Lawrence   ANDROID: Incremen...
1164
1165
  static bool get_pseudo_inode(int ino, struct inode *inode)
  {
b79605a90   Yurii Zubrytskyi   ANDROID: Incremen...
1166
1167
1168
1169
1170
1171
1172
  	int i = 0;
  
  	for (; i < ARRAY_SIZE(incfs_pseudo_file_inodes); ++i)
  		if (ino == incfs_pseudo_file_inodes[i])
  			break;
  	if (i == ARRAY_SIZE(incfs_pseudo_file_inodes))
  		return false;
8334d69e6   Paul Lawrence   ANDROID: Incremen...
1173
1174
1175
1176
1177
1178
1179
1180
  	inode->i_ctime = (struct timespec64){};
  	inode->i_mtime = inode->i_ctime;
  	inode->i_atime = inode->i_ctime;
  	inode->i_size = 0;
  	inode->i_ino = ino;
  	inode->i_private = NULL;
  	inode_init_owner(inode, NULL, S_IFREG | READ_WRITE_FILE_MODE);
  	inode->i_op = &incfs_file_inode_ops;
b79605a90   Yurii Zubrytskyi   ANDROID: Incremen...
1181
1182
  	inode->i_fop = pseudo_file_operations[i];
  	return true;
8334d69e6   Paul Lawrence   ANDROID: Incremen...
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
  }
  
  struct inode_search {
  	unsigned long ino;
  };
  
  static int inode_test(struct inode *inode, void *opaque)
  {
  	struct inode_search *search = opaque;
  
  	return inode->i_ino == search->ino;
  }
  
  static int inode_set(struct inode *inode, void *opaque)
  {
  	struct inode_search *search = opaque;
  
  	if (get_pseudo_inode(search->ino, inode))
  		return 0;
  
  	/* Unknown inode requested. */
  	return -EINVAL;
  }
cb776f457   Paul Lawrence   ANDROID: Incremen...
1206
  static struct inode *fetch_inode(struct super_block *sb, unsigned long ino)
8334d69e6   Paul Lawrence   ANDROID: Incremen...
1207
1208
  {
  	struct inode_search search = {
cb776f457   Paul Lawrence   ANDROID: Incremen...
1209
  		.ino = ino
8334d69e6   Paul Lawrence   ANDROID: Incremen...
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
  	};
  	struct inode *inode = iget5_locked(sb, search.ino, inode_test,
  				inode_set, &search);
  
  	if (!inode)
  		return ERR_PTR(-ENOMEM);
  
  	if (inode->i_state & I_NEW)
  		unlock_new_inode(inode);
  
  	return inode;
  }
  
  int dir_lookup_pseudo_files(struct super_block *sb, struct dentry *dentry)
  {
  	struct mem_range name_range =
  			range((u8 *)dentry->d_name.name, dentry->d_name.len);
cb776f457   Paul Lawrence   ANDROID: Incremen...
1227
1228
  	unsigned long ino;
  	struct inode *inode;
b79605a90   Yurii Zubrytskyi   ANDROID: Incremen...
1229
  	int i = 0;
cb776f457   Paul Lawrence   ANDROID: Incremen...
1230

b79605a90   Yurii Zubrytskyi   ANDROID: Incremen...
1231
1232
1233
1234
  	for (; i < ARRAY_SIZE(incfs_pseudo_file_names); ++i)
  		if (incfs_equal_ranges(incfs_pseudo_file_names[i], name_range))
  			break;
  	if (i == ARRAY_SIZE(incfs_pseudo_file_names))
cb776f457   Paul Lawrence   ANDROID: Incremen...
1235
  		return -ENOENT;
8334d69e6   Paul Lawrence   ANDROID: Incremen...
1236

b79605a90   Yurii Zubrytskyi   ANDROID: Incremen...
1237
  	ino = incfs_pseudo_file_inodes[i];
cb776f457   Paul Lawrence   ANDROID: Incremen...
1238
1239
1240
  	inode = fetch_inode(sb, ino);
  	if (IS_ERR(inode))
  		return PTR_ERR(inode);
8334d69e6   Paul Lawrence   ANDROID: Incremen...
1241

cb776f457   Paul Lawrence   ANDROID: Incremen...
1242
1243
  	d_add(dentry, inode);
  	return 0;
8334d69e6   Paul Lawrence   ANDROID: Incremen...
1244
1245
1246
1247
  }
  
  int emit_pseudo_files(struct dir_context *ctx)
  {
b79605a90   Yurii Zubrytskyi   ANDROID: Incremen...
1248
  	loff_t i = ctx->pos;
8334d69e6   Paul Lawrence   ANDROID: Incremen...
1249

b79605a90   Yurii Zubrytskyi   ANDROID: Incremen...
1250
1251
1252
1253
  	for (; i < ARRAY_SIZE(incfs_pseudo_file_names); ++i) {
  		if (!dir_emit(ctx, incfs_pseudo_file_names[i].data,
  			      incfs_pseudo_file_names[i].len,
  			      incfs_pseudo_file_inodes[i], DT_REG))
8334d69e6   Paul Lawrence   ANDROID: Incremen...
1254
1255
1256
1257
  			return -EINVAL;
  
  		ctx->pos++;
  	}
8334d69e6   Paul Lawrence   ANDROID: Incremen...
1258
1259
  	return 0;
  }