pseudo_files.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2020 Google LLC
  4. */
  5. #include <linux/file.h>
  6. #include <linux/fs.h>
  7. #include <linux/fsnotify.h>
  8. #include <linux/namei.h>
  9. #include <linux/poll.h>
  10. #include <linux/syscalls.h>
  11. #include <linux/fdtable.h>
  12. #include <uapi/linux/incrementalfs.h>
  13. #include "pseudo_files.h"
  14. #include "data_mgmt.h"
  15. #include "format.h"
  16. #include "integrity.h"
  17. #include "vfs.h"
  18. #define READ_WRITE_FILE_MODE 0666
  19. static bool is_pseudo_filename(struct mem_range name);
  20. /*******************************************************************************
  21. * .pending_reads pseudo file definition
  22. ******************************************************************************/
  23. #define INCFS_PENDING_READS_INODE 2
  24. static const char pending_reads_file_name[] = INCFS_PENDING_READS_FILENAME;
  25. /* State of an open .pending_reads file, unique for each file descriptor. */
  26. struct pending_reads_state {
  27. /* A serial number of the last pending read obtained from this file. */
  28. int last_pending_read_sn;
  29. };
  30. static ssize_t pending_reads_read(struct file *f, char __user *buf, size_t len,
  31. loff_t *ppos)
  32. {
  33. struct pending_reads_state *pr_state = f->private_data;
  34. struct mount_info *mi = get_mount_info(file_superblock(f));
  35. bool report_uid;
  36. unsigned long page = 0;
  37. struct incfs_pending_read_info *reads_buf = NULL;
  38. struct incfs_pending_read_info2 *reads_buf2 = NULL;
  39. size_t record_size;
  40. size_t reads_to_collect;
  41. int last_known_read_sn = READ_ONCE(pr_state->last_pending_read_sn);
  42. int new_max_sn = last_known_read_sn;
  43. int reads_collected = 0;
  44. ssize_t result = 0;
  45. if (!mi)
  46. return -EFAULT;
  47. report_uid = mi->mi_options.report_uid;
  48. record_size = report_uid ? sizeof(*reads_buf2) : sizeof(*reads_buf);
  49. reads_to_collect = len / record_size;
  50. if (!incfs_fresh_pending_reads_exist(mi, last_known_read_sn))
  51. return 0;
  52. page = get_zeroed_page(GFP_NOFS);
  53. if (!page)
  54. return -ENOMEM;
  55. if (report_uid)
  56. reads_buf2 = (struct incfs_pending_read_info2 *) page;
  57. else
  58. reads_buf = (struct incfs_pending_read_info *) page;
  59. reads_to_collect =
  60. min_t(size_t, PAGE_SIZE / record_size, reads_to_collect);
  61. reads_collected = incfs_collect_pending_reads(mi, last_known_read_sn,
  62. reads_buf, reads_buf2, reads_to_collect,
  63. &new_max_sn);
  64. if (reads_collected < 0) {
  65. result = reads_collected;
  66. goto out;
  67. }
  68. /*
  69. * Just to make sure that we don't accidentally copy more data
  70. * to reads buffer than userspace can handle.
  71. */
  72. reads_collected = min_t(size_t, reads_collected, reads_to_collect);
  73. result = reads_collected * record_size;
  74. /* Copy reads info to the userspace buffer */
  75. if (copy_to_user(buf, (void *)page, result)) {
  76. result = -EFAULT;
  77. goto out;
  78. }
  79. WRITE_ONCE(pr_state->last_pending_read_sn, new_max_sn);
  80. *ppos = 0;
  81. out:
  82. free_page(page);
  83. return result;
  84. }
  85. static __poll_t pending_reads_poll(struct file *file, poll_table *wait)
  86. {
  87. struct pending_reads_state *state = file->private_data;
  88. struct mount_info *mi = get_mount_info(file_superblock(file));
  89. __poll_t ret = 0;
  90. poll_wait(file, &mi->mi_pending_reads_notif_wq, wait);
  91. if (incfs_fresh_pending_reads_exist(mi,
  92. state->last_pending_read_sn))
  93. ret = EPOLLIN | EPOLLRDNORM;
  94. return ret;
  95. }
  96. static int pending_reads_open(struct inode *inode, struct file *file)
  97. {
  98. struct pending_reads_state *state = NULL;
  99. state = kzalloc(sizeof(*state), GFP_NOFS);
  100. if (!state)
  101. return -ENOMEM;
  102. file->private_data = state;
  103. return 0;
  104. }
  105. static int pending_reads_release(struct inode *inode, struct file *file)
  106. {
  107. kfree(file->private_data);
  108. return 0;
  109. }
  110. static long ioctl_permit_fill(struct file *f, void __user *arg)
  111. {
  112. struct incfs_permit_fill __user *usr_permit_fill = arg;
  113. struct incfs_permit_fill permit_fill;
  114. long error = 0;
  115. struct file *file = NULL;
  116. struct incfs_file_data *fd;
  117. if (copy_from_user(&permit_fill, usr_permit_fill, sizeof(permit_fill)))
  118. return -EFAULT;
  119. file = fget(permit_fill.file_descriptor);
  120. if (IS_ERR_OR_NULL(file)) {
  121. if (!file)
  122. return -ENOENT;
  123. return PTR_ERR(file);
  124. }
  125. if (file->f_op != &incfs_file_ops) {
  126. error = -EPERM;
  127. goto out;
  128. }
  129. if (file->f_inode->i_sb != f->f_inode->i_sb) {
  130. error = -EPERM;
  131. goto out;
  132. }
  133. fd = file->private_data;
  134. switch (fd->fd_fill_permission) {
  135. case CANT_FILL:
  136. fd->fd_fill_permission = CAN_FILL;
  137. break;
  138. case CAN_FILL:
  139. pr_debug("CAN_FILL already set");
  140. break;
  141. default:
  142. pr_warn("Invalid file private data");
  143. error = -EFAULT;
  144. goto out;
  145. }
  146. out:
  147. fput(file);
  148. return error;
  149. }
  150. static int chmod(struct dentry *dentry, umode_t mode)
  151. {
  152. struct inode *inode = dentry->d_inode;
  153. struct inode *delegated_inode = NULL;
  154. struct iattr newattrs;
  155. int error;
  156. retry_deleg:
  157. inode_lock(inode);
  158. newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
  159. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  160. error = notify_change(&init_user_ns, dentry, &newattrs, &delegated_inode);
  161. inode_unlock(inode);
  162. if (delegated_inode) {
  163. error = break_deleg_wait(&delegated_inode);
  164. if (!error)
  165. goto retry_deleg;
  166. }
  167. return error;
  168. }
  169. static bool incfs_equal_ranges(struct mem_range lhs, struct mem_range rhs)
  170. {
  171. if (lhs.len != rhs.len)
  172. return false;
  173. return memcmp(lhs.data, rhs.data, lhs.len) == 0;
  174. }
  175. static int validate_name(char *file_name)
  176. {
  177. struct mem_range name = range(file_name, strlen(file_name));
  178. int i = 0;
  179. if (name.len > INCFS_MAX_NAME_LEN)
  180. return -ENAMETOOLONG;
  181. if (is_pseudo_filename(name))
  182. return -EINVAL;
  183. for (i = 0; i < name.len; i++)
  184. if (name.data[i] == '/')
  185. return -EINVAL;
  186. return 0;
  187. }
  188. static int dir_relative_path_resolve(
  189. struct mount_info *mi,
  190. const char __user *relative_path,
  191. struct path *result_path,
  192. struct path *base_path)
  193. {
  194. int dir_fd = get_unused_fd_flags(0);
  195. struct file *dir_f = NULL;
  196. int error = 0;
  197. if (!base_path)
  198. base_path = &mi->mi_backing_dir_path;
  199. if (dir_fd < 0)
  200. return dir_fd;
  201. dir_f = dentry_open(base_path, O_RDONLY | O_NOATIME, current_cred());
  202. if (IS_ERR(dir_f)) {
  203. error = PTR_ERR(dir_f);
  204. goto out;
  205. }
  206. fd_install(dir_fd, dir_f);
  207. if (!relative_path) {
  208. /* No relative path given, just return the base dir. */
  209. *result_path = *base_path;
  210. path_get(result_path);
  211. goto out;
  212. }
  213. error = user_path_at_empty(dir_fd, relative_path,
  214. LOOKUP_FOLLOW | LOOKUP_DIRECTORY, result_path, NULL);
  215. out:
  216. close_fd(dir_fd);
  217. if (error)
  218. pr_debug("Error: %d\n", error);
  219. return error;
  220. }
  221. static struct mem_range incfs_copy_signature_info_from_user(u8 __user *original,
  222. u64 size)
  223. {
  224. u8 *result;
  225. if (!original)
  226. return range(NULL, 0);
  227. if (size > INCFS_MAX_SIGNATURE_SIZE)
  228. return range(ERR_PTR(-EFAULT), 0);
  229. result = kzalloc(size, GFP_NOFS | __GFP_COMP);
  230. if (!result)
  231. return range(ERR_PTR(-ENOMEM), 0);
  232. if (copy_from_user(result, original, size)) {
  233. kfree(result);
  234. return range(ERR_PTR(-EFAULT), 0);
  235. }
  236. return range(result, size);
  237. }
  238. static int init_new_file(struct mount_info *mi, struct dentry *dentry,
  239. incfs_uuid_t *uuid, u64 size, struct mem_range attr,
  240. u8 __user *user_signature_info, u64 signature_size)
  241. {
  242. struct path path = {};
  243. struct file *new_file;
  244. int error = 0;
  245. struct backing_file_context *bfc = NULL;
  246. u32 block_count;
  247. struct mem_range raw_signature = { NULL };
  248. struct mtree *hash_tree = NULL;
  249. if (!mi || !dentry || !uuid)
  250. return -EFAULT;
  251. /* Resize newly created file to its true size. */
  252. path = (struct path) {
  253. .mnt = mi->mi_backing_dir_path.mnt,
  254. .dentry = dentry
  255. };
  256. new_file = dentry_open(&path, O_RDWR | O_NOATIME | O_LARGEFILE,
  257. current_cred());
  258. if (IS_ERR(new_file)) {
  259. error = PTR_ERR(new_file);
  260. goto out;
  261. }
  262. bfc = incfs_alloc_bfc(mi, new_file);
  263. fput(new_file);
  264. if (IS_ERR(bfc)) {
  265. error = PTR_ERR(bfc);
  266. bfc = NULL;
  267. goto out;
  268. }
  269. mutex_lock(&bfc->bc_mutex);
  270. error = incfs_write_fh_to_backing_file(bfc, uuid, size);
  271. if (error)
  272. goto out;
  273. block_count = (u32)get_blocks_count_for_size(size);
  274. if (user_signature_info) {
  275. raw_signature = incfs_copy_signature_info_from_user(
  276. user_signature_info, signature_size);
  277. if (IS_ERR(raw_signature.data)) {
  278. error = PTR_ERR(raw_signature.data);
  279. raw_signature.data = NULL;
  280. goto out;
  281. }
  282. hash_tree = incfs_alloc_mtree(raw_signature, block_count);
  283. if (IS_ERR(hash_tree)) {
  284. error = PTR_ERR(hash_tree);
  285. hash_tree = NULL;
  286. goto out;
  287. }
  288. error = incfs_write_signature_to_backing_file(bfc,
  289. raw_signature, hash_tree->hash_tree_area_size,
  290. NULL, NULL);
  291. if (error)
  292. goto out;
  293. block_count += get_blocks_count_for_size(
  294. hash_tree->hash_tree_area_size);
  295. }
  296. if (block_count)
  297. error = incfs_write_blockmap_to_backing_file(bfc, block_count);
  298. if (error)
  299. goto out;
  300. out:
  301. if (bfc) {
  302. mutex_unlock(&bfc->bc_mutex);
  303. incfs_free_bfc(bfc);
  304. }
  305. incfs_free_mtree(hash_tree);
  306. kfree(raw_signature.data);
  307. if (error)
  308. pr_debug("incfs: %s error: %d\n", __func__, error);
  309. return error;
  310. }
  311. static void notify_create(struct file *pending_reads_file,
  312. const char __user *dir_name, const char *file_name,
  313. const char *file_id_str, bool incomplete_file)
  314. {
  315. struct mount_info *mi =
  316. get_mount_info(file_superblock(pending_reads_file));
  317. struct path base_path = {
  318. .mnt = pending_reads_file->f_path.mnt,
  319. .dentry = pending_reads_file->f_path.dentry->d_parent,
  320. };
  321. struct path dir_path = {};
  322. struct dentry *file = NULL;
  323. struct dentry *dir = NULL;
  324. int error;
  325. error = dir_relative_path_resolve(mi, dir_name, &dir_path, &base_path);
  326. if (error)
  327. goto out;
  328. file = incfs_lookup_dentry(dir_path.dentry, file_name);
  329. if (IS_ERR(file)) {
  330. error = PTR_ERR(file);
  331. file = NULL;
  332. goto out;
  333. }
  334. fsnotify_create(d_inode(dir_path.dentry), file);
  335. if (file_id_str) {
  336. dir = incfs_lookup_dentry(base_path.dentry, INCFS_INDEX_NAME);
  337. if (IS_ERR(dir)) {
  338. error = PTR_ERR(dir);
  339. dir = NULL;
  340. goto out;
  341. }
  342. dput(file);
  343. file = incfs_lookup_dentry(dir, file_id_str);
  344. if (IS_ERR(file)) {
  345. error = PTR_ERR(file);
  346. file = NULL;
  347. goto out;
  348. }
  349. fsnotify_create(d_inode(dir), file);
  350. if (incomplete_file) {
  351. dput(dir);
  352. dir = incfs_lookup_dentry(base_path.dentry,
  353. INCFS_INCOMPLETE_NAME);
  354. if (IS_ERR(dir)) {
  355. error = PTR_ERR(dir);
  356. dir = NULL;
  357. goto out;
  358. }
  359. dput(file);
  360. file = incfs_lookup_dentry(dir, file_id_str);
  361. if (IS_ERR(file)) {
  362. error = PTR_ERR(file);
  363. file = NULL;
  364. goto out;
  365. }
  366. fsnotify_create(d_inode(dir), file);
  367. }
  368. }
  369. out:
  370. if (error)
  371. pr_warn("%s failed with error %d\n", __func__, error);
  372. dput(dir);
  373. dput(file);
  374. path_put(&dir_path);
  375. }
  376. static long ioctl_create_file(struct file *file,
  377. struct incfs_new_file_args __user *usr_args)
  378. {
  379. struct mount_info *mi = get_mount_info(file_superblock(file));
  380. struct incfs_new_file_args args;
  381. char *file_id_str = NULL;
  382. struct dentry *index_file_dentry = NULL;
  383. struct dentry *named_file_dentry = NULL;
  384. struct dentry *incomplete_file_dentry = NULL;
  385. struct path parent_dir_path = {};
  386. struct inode *index_dir_inode = NULL;
  387. __le64 size_attr_value = 0;
  388. char *file_name = NULL;
  389. char *attr_value = NULL;
  390. int error = 0;
  391. bool locked = false;
  392. bool index_linked = false;
  393. bool name_linked = false;
  394. bool incomplete_linked = false;
  395. if (!mi || !mi->mi_index_dir || !mi->mi_incomplete_dir) {
  396. error = -EFAULT;
  397. goto out;
  398. }
  399. if (copy_from_user(&args, usr_args, sizeof(args)) > 0) {
  400. error = -EFAULT;
  401. goto out;
  402. }
  403. file_name = strndup_user(u64_to_user_ptr(args.file_name), PATH_MAX);
  404. if (IS_ERR(file_name)) {
  405. error = PTR_ERR(file_name);
  406. file_name = NULL;
  407. goto out;
  408. }
  409. error = validate_name(file_name);
  410. if (error)
  411. goto out;
  412. file_id_str = file_id_to_str(args.file_id);
  413. if (!file_id_str) {
  414. error = -ENOMEM;
  415. goto out;
  416. }
  417. error = mutex_lock_interruptible(&mi->mi_dir_struct_mutex);
  418. if (error)
  419. goto out;
  420. locked = true;
  421. /* Find a directory to put the file into. */
  422. error = dir_relative_path_resolve(mi,
  423. u64_to_user_ptr(args.directory_path),
  424. &parent_dir_path, NULL);
  425. if (error)
  426. goto out;
  427. if (parent_dir_path.dentry == mi->mi_index_dir) {
  428. /* Can't create a file directly inside .index */
  429. error = -EBUSY;
  430. goto out;
  431. }
  432. if (parent_dir_path.dentry == mi->mi_incomplete_dir) {
  433. /* Can't create a file directly inside .incomplete */
  434. error = -EBUSY;
  435. goto out;
  436. }
  437. /* Look up a dentry in the parent dir. It should be negative. */
  438. named_file_dentry = incfs_lookup_dentry(parent_dir_path.dentry,
  439. file_name);
  440. if (!named_file_dentry) {
  441. error = -EFAULT;
  442. goto out;
  443. }
  444. if (IS_ERR(named_file_dentry)) {
  445. error = PTR_ERR(named_file_dentry);
  446. named_file_dentry = NULL;
  447. goto out;
  448. }
  449. if (d_really_is_positive(named_file_dentry)) {
  450. /* File with this path already exists. */
  451. error = -EEXIST;
  452. goto out;
  453. }
  454. /* Look up a dentry in the incomplete dir. It should be negative. */
  455. incomplete_file_dentry = incfs_lookup_dentry(mi->mi_incomplete_dir,
  456. file_id_str);
  457. if (!incomplete_file_dentry) {
  458. error = -EFAULT;
  459. goto out;
  460. }
  461. if (IS_ERR(incomplete_file_dentry)) {
  462. error = PTR_ERR(incomplete_file_dentry);
  463. incomplete_file_dentry = NULL;
  464. goto out;
  465. }
  466. if (d_really_is_positive(incomplete_file_dentry)) {
  467. /* File with this path already exists. */
  468. error = -EEXIST;
  469. goto out;
  470. }
  471. /* Look up a dentry in the .index dir. It should be negative. */
  472. index_file_dentry = incfs_lookup_dentry(mi->mi_index_dir, file_id_str);
  473. if (!index_file_dentry) {
  474. error = -EFAULT;
  475. goto out;
  476. }
  477. if (IS_ERR(index_file_dentry)) {
  478. error = PTR_ERR(index_file_dentry);
  479. index_file_dentry = NULL;
  480. goto out;
  481. }
  482. if (d_really_is_positive(index_file_dentry)) {
  483. /* File with this ID already exists in index. */
  484. error = -EEXIST;
  485. goto out;
  486. }
  487. /* Creating a file in the .index dir. */
  488. index_dir_inode = d_inode(mi->mi_index_dir);
  489. inode_lock_nested(index_dir_inode, I_MUTEX_PARENT);
  490. error = vfs_create(&init_user_ns, index_dir_inode, index_file_dentry,
  491. args.mode | 0222, true);
  492. inode_unlock(index_dir_inode);
  493. if (error)
  494. goto out;
  495. if (!d_really_is_positive(index_file_dentry)) {
  496. error = -EINVAL;
  497. goto out;
  498. }
  499. error = chmod(index_file_dentry, args.mode | 0222);
  500. if (error) {
  501. pr_debug("incfs: chmod err: %d\n", error);
  502. goto out;
  503. }
  504. /* Save the file's ID as an xattr for easy fetching in future. */
  505. error = vfs_setxattr(&init_user_ns, index_file_dentry, INCFS_XATTR_ID_NAME,
  506. file_id_str, strlen(file_id_str), XATTR_CREATE);
  507. if (error) {
  508. pr_debug("incfs: vfs_setxattr err:%d\n", error);
  509. goto out;
  510. }
  511. /* Save the file's size as an xattr for easy fetching in future. */
  512. size_attr_value = cpu_to_le64(args.size);
  513. error = vfs_setxattr(&init_user_ns, index_file_dentry, INCFS_XATTR_SIZE_NAME,
  514. (char *)&size_attr_value, sizeof(size_attr_value),
  515. XATTR_CREATE);
  516. if (error) {
  517. pr_debug("incfs: vfs_setxattr err:%d\n", error);
  518. goto out;
  519. }
  520. /* Save the file's attribute as an xattr */
  521. if (args.file_attr_len && args.file_attr) {
  522. if (args.file_attr_len > INCFS_MAX_FILE_ATTR_SIZE) {
  523. error = -E2BIG;
  524. goto out;
  525. }
  526. attr_value = kmalloc(args.file_attr_len, GFP_NOFS);
  527. if (!attr_value) {
  528. error = -ENOMEM;
  529. goto out;
  530. }
  531. if (copy_from_user(attr_value,
  532. u64_to_user_ptr(args.file_attr),
  533. args.file_attr_len) > 0) {
  534. error = -EFAULT;
  535. goto out;
  536. }
  537. error = vfs_setxattr(&init_user_ns, index_file_dentry,
  538. INCFS_XATTR_METADATA_NAME,
  539. attr_value, args.file_attr_len,
  540. XATTR_CREATE);
  541. if (error)
  542. goto out;
  543. }
  544. /* Initializing a newly created file. */
  545. error = init_new_file(mi, index_file_dentry, &args.file_id, args.size,
  546. range(attr_value, args.file_attr_len),
  547. u64_to_user_ptr(args.signature_info),
  548. args.signature_size);
  549. if (error)
  550. goto out;
  551. index_linked = true;
  552. /* Linking a file with its real name from the requested dir. */
  553. error = incfs_link(index_file_dentry, named_file_dentry);
  554. if (error)
  555. goto out;
  556. name_linked = true;
  557. if (args.size) {
  558. /* Linking a file with its incomplete entry */
  559. error = incfs_link(index_file_dentry, incomplete_file_dentry);
  560. if (error)
  561. goto out;
  562. incomplete_linked = true;
  563. }
  564. notify_create(file, u64_to_user_ptr(args.directory_path), file_name,
  565. file_id_str, args.size != 0);
  566. out:
  567. if (error) {
  568. pr_debug("incfs: %s err:%d\n", __func__, error);
  569. if (index_linked)
  570. incfs_unlink(index_file_dentry);
  571. if (name_linked)
  572. incfs_unlink(named_file_dentry);
  573. if (incomplete_linked)
  574. incfs_unlink(incomplete_file_dentry);
  575. }
  576. kfree(file_id_str);
  577. kfree(file_name);
  578. kfree(attr_value);
  579. dput(named_file_dentry);
  580. dput(index_file_dentry);
  581. dput(incomplete_file_dentry);
  582. path_put(&parent_dir_path);
  583. if (locked)
  584. mutex_unlock(&mi->mi_dir_struct_mutex);
  585. return error;
  586. }
  587. static int init_new_mapped_file(struct mount_info *mi, struct dentry *dentry,
  588. incfs_uuid_t *uuid, u64 size, u64 offset)
  589. {
  590. struct path path = {};
  591. struct file *new_file;
  592. int error = 0;
  593. struct backing_file_context *bfc = NULL;
  594. if (!mi || !dentry || !uuid)
  595. return -EFAULT;
  596. /* Resize newly created file to its true size. */
  597. path = (struct path) {
  598. .mnt = mi->mi_backing_dir_path.mnt,
  599. .dentry = dentry
  600. };
  601. new_file = dentry_open(&path, O_RDWR | O_NOATIME | O_LARGEFILE,
  602. current_cred());
  603. if (IS_ERR(new_file)) {
  604. error = PTR_ERR(new_file);
  605. goto out;
  606. }
  607. bfc = incfs_alloc_bfc(mi, new_file);
  608. fput(new_file);
  609. if (IS_ERR(bfc)) {
  610. error = PTR_ERR(bfc);
  611. bfc = NULL;
  612. goto out;
  613. }
  614. mutex_lock(&bfc->bc_mutex);
  615. error = incfs_write_mapping_fh_to_backing_file(bfc, uuid, size, offset);
  616. if (error)
  617. goto out;
  618. out:
  619. if (bfc) {
  620. mutex_unlock(&bfc->bc_mutex);
  621. incfs_free_bfc(bfc);
  622. }
  623. if (error)
  624. pr_debug("incfs: %s error: %d\n", __func__, error);
  625. return error;
  626. }
  627. static long ioctl_create_mapped_file(struct file *file, void __user *arg)
  628. {
  629. struct mount_info *mi = get_mount_info(file_superblock(file));
  630. struct incfs_create_mapped_file_args __user *args_usr_ptr = arg;
  631. struct incfs_create_mapped_file_args args = {};
  632. char *file_name;
  633. int error = 0;
  634. struct path parent_dir_path = {};
  635. char *source_file_name = NULL;
  636. struct dentry *source_file_dentry = NULL;
  637. u64 source_file_size;
  638. struct dentry *file_dentry = NULL;
  639. struct inode *parent_inode;
  640. __le64 size_attr_value;
  641. if (copy_from_user(&args, args_usr_ptr, sizeof(args)) > 0)
  642. return -EINVAL;
  643. file_name = strndup_user(u64_to_user_ptr(args.file_name), PATH_MAX);
  644. if (IS_ERR(file_name)) {
  645. error = PTR_ERR(file_name);
  646. file_name = NULL;
  647. goto out;
  648. }
  649. error = validate_name(file_name);
  650. if (error)
  651. goto out;
  652. if (args.source_offset % INCFS_DATA_FILE_BLOCK_SIZE) {
  653. error = -EINVAL;
  654. goto out;
  655. }
  656. /* Validate file mapping is in range */
  657. source_file_name = file_id_to_str(args.source_file_id);
  658. if (!source_file_name) {
  659. pr_warn("Failed to alloc source_file_name\n");
  660. error = -ENOMEM;
  661. goto out;
  662. }
  663. source_file_dentry = incfs_lookup_dentry(mi->mi_index_dir,
  664. source_file_name);
  665. if (!source_file_dentry) {
  666. pr_warn("Source file does not exist\n");
  667. error = -EINVAL;
  668. goto out;
  669. }
  670. if (IS_ERR(source_file_dentry)) {
  671. pr_warn("Error opening source file\n");
  672. error = PTR_ERR(source_file_dentry);
  673. source_file_dentry = NULL;
  674. goto out;
  675. }
  676. if (!d_really_is_positive(source_file_dentry)) {
  677. pr_warn("Source file dentry negative\n");
  678. error = -EINVAL;
  679. goto out;
  680. }
  681. error = vfs_getxattr(&init_user_ns, source_file_dentry, INCFS_XATTR_SIZE_NAME,
  682. (char *)&size_attr_value, sizeof(size_attr_value));
  683. if (error < 0)
  684. goto out;
  685. if (error != sizeof(size_attr_value)) {
  686. pr_warn("Mapped file has no size attr\n");
  687. error = -EINVAL;
  688. goto out;
  689. }
  690. source_file_size = le64_to_cpu(size_attr_value);
  691. if (args.source_offset + args.size > source_file_size) {
  692. pr_warn("Mapped file out of range\n");
  693. error = -EINVAL;
  694. goto out;
  695. }
  696. /* Find a directory to put the file into. */
  697. error = dir_relative_path_resolve(mi,
  698. u64_to_user_ptr(args.directory_path),
  699. &parent_dir_path, NULL);
  700. if (error)
  701. goto out;
  702. if (parent_dir_path.dentry == mi->mi_index_dir) {
  703. /* Can't create a file directly inside .index */
  704. error = -EBUSY;
  705. goto out;
  706. }
  707. /* Look up a dentry in the parent dir. It should be negative. */
  708. file_dentry = incfs_lookup_dentry(parent_dir_path.dentry,
  709. file_name);
  710. if (!file_dentry) {
  711. error = -EFAULT;
  712. goto out;
  713. }
  714. if (IS_ERR(file_dentry)) {
  715. error = PTR_ERR(file_dentry);
  716. file_dentry = NULL;
  717. goto out;
  718. }
  719. if (d_really_is_positive(file_dentry)) {
  720. error = -EEXIST;
  721. goto out;
  722. }
  723. parent_inode = d_inode(parent_dir_path.dentry);
  724. inode_lock_nested(parent_inode, I_MUTEX_PARENT);
  725. error = vfs_create(&init_user_ns, parent_inode, file_dentry,
  726. args.mode | 0222, true);
  727. inode_unlock(parent_inode);
  728. if (error)
  729. goto out;
  730. error = chmod(file_dentry, args.mode | 0222);
  731. if (error) {
  732. pr_debug("incfs: chmod err: %d\n", error);
  733. goto delete_file;
  734. }
  735. /* Save the file's size as an xattr for easy fetching in future. */
  736. size_attr_value = cpu_to_le64(args.size);
  737. error = vfs_setxattr(&init_user_ns, file_dentry, INCFS_XATTR_SIZE_NAME,
  738. (char *)&size_attr_value, sizeof(size_attr_value),
  739. XATTR_CREATE);
  740. if (error) {
  741. pr_debug("incfs: vfs_setxattr err:%d\n", error);
  742. goto delete_file;
  743. }
  744. error = init_new_mapped_file(mi, file_dentry, &args.source_file_id,
  745. args.size, args.source_offset);
  746. if (error)
  747. goto delete_file;
  748. notify_create(file, u64_to_user_ptr(args.directory_path), file_name,
  749. NULL, false);
  750. goto out;
  751. delete_file:
  752. incfs_unlink(file_dentry);
  753. out:
  754. dput(file_dentry);
  755. dput(source_file_dentry);
  756. path_put(&parent_dir_path);
  757. kfree(file_name);
  758. kfree(source_file_name);
  759. return error;
  760. }
  761. static long ioctl_get_read_timeouts(struct mount_info *mi, void __user *arg)
  762. {
  763. struct incfs_get_read_timeouts_args __user *args_usr_ptr = arg;
  764. struct incfs_get_read_timeouts_args args = {};
  765. int error = 0;
  766. struct incfs_per_uid_read_timeouts *buffer;
  767. int size;
  768. if (copy_from_user(&args, args_usr_ptr, sizeof(args)))
  769. return -EINVAL;
  770. if (args.timeouts_array_size > INCFS_DATA_FILE_BLOCK_SIZE)
  771. return -EINVAL;
  772. buffer = kzalloc(args.timeouts_array_size, GFP_NOFS);
  773. if (!buffer)
  774. return -ENOMEM;
  775. spin_lock(&mi->mi_per_uid_read_timeouts_lock);
  776. size = mi->mi_per_uid_read_timeouts_size;
  777. if (args.timeouts_array_size < size)
  778. error = -E2BIG;
  779. else if (size)
  780. memcpy(buffer, mi->mi_per_uid_read_timeouts, size);
  781. spin_unlock(&mi->mi_per_uid_read_timeouts_lock);
  782. args.timeouts_array_size_out = size;
  783. if (!error && size)
  784. if (copy_to_user(u64_to_user_ptr(args.timeouts_array), buffer,
  785. size))
  786. error = -EFAULT;
  787. if (!error || error == -E2BIG)
  788. if (copy_to_user(args_usr_ptr, &args, sizeof(args)) > 0)
  789. error = -EFAULT;
  790. kfree(buffer);
  791. return error;
  792. }
  793. static long ioctl_set_read_timeouts(struct mount_info *mi, void __user *arg)
  794. {
  795. struct incfs_set_read_timeouts_args __user *args_usr_ptr = arg;
  796. struct incfs_set_read_timeouts_args args = {};
  797. int error = 0;
  798. int size;
  799. struct incfs_per_uid_read_timeouts *buffer = NULL, *tmp;
  800. int i;
  801. if (copy_from_user(&args, args_usr_ptr, sizeof(args)))
  802. return -EINVAL;
  803. size = args.timeouts_array_size;
  804. if (size) {
  805. if (size > INCFS_DATA_FILE_BLOCK_SIZE ||
  806. size % sizeof(*buffer) != 0)
  807. return -EINVAL;
  808. buffer = kzalloc(size, GFP_NOFS);
  809. if (!buffer)
  810. return -ENOMEM;
  811. if (copy_from_user(buffer, u64_to_user_ptr(args.timeouts_array),
  812. size)) {
  813. error = -EINVAL;
  814. goto out;
  815. }
  816. for (i = 0; i < size / sizeof(*buffer); ++i) {
  817. struct incfs_per_uid_read_timeouts *t = &buffer[i];
  818. if (t->min_pending_time_us > t->max_pending_time_us) {
  819. error = -EINVAL;
  820. goto out;
  821. }
  822. }
  823. }
  824. spin_lock(&mi->mi_per_uid_read_timeouts_lock);
  825. mi->mi_per_uid_read_timeouts_size = size;
  826. tmp = mi->mi_per_uid_read_timeouts;
  827. mi->mi_per_uid_read_timeouts = buffer;
  828. buffer = tmp;
  829. spin_unlock(&mi->mi_per_uid_read_timeouts_lock);
  830. out:
  831. kfree(buffer);
  832. return error;
  833. }
  834. static long ioctl_get_last_read_error(struct mount_info *mi, void __user *arg)
  835. {
  836. struct incfs_get_last_read_error_args __user *args_usr_ptr = arg;
  837. struct incfs_get_last_read_error_args args = {};
  838. int error;
  839. error = mutex_lock_interruptible(&mi->mi_le_mutex);
  840. if (error)
  841. return error;
  842. args.file_id_out = mi->mi_le_file_id;
  843. args.time_us_out = mi->mi_le_time_us;
  844. args.page_out = mi->mi_le_page;
  845. args.errno_out = mi->mi_le_errno;
  846. args.uid_out = mi->mi_le_uid;
  847. mutex_unlock(&mi->mi_le_mutex);
  848. if (copy_to_user(args_usr_ptr, &args, sizeof(args)) > 0)
  849. error = -EFAULT;
  850. return error;
  851. }
  852. static long pending_reads_dispatch_ioctl(struct file *f, unsigned int req,
  853. unsigned long arg)
  854. {
  855. struct mount_info *mi = get_mount_info(file_superblock(f));
  856. switch (req) {
  857. case INCFS_IOC_CREATE_FILE:
  858. return ioctl_create_file(f, (void __user *)arg);
  859. case INCFS_IOC_PERMIT_FILL:
  860. return ioctl_permit_fill(f, (void __user *)arg);
  861. case INCFS_IOC_CREATE_MAPPED_FILE:
  862. return ioctl_create_mapped_file(f, (void __user *)arg);
  863. case INCFS_IOC_GET_READ_TIMEOUTS:
  864. return ioctl_get_read_timeouts(mi, (void __user *)arg);
  865. case INCFS_IOC_SET_READ_TIMEOUTS:
  866. return ioctl_set_read_timeouts(mi, (void __user *)arg);
  867. case INCFS_IOC_GET_LAST_READ_ERROR:
  868. return ioctl_get_last_read_error(mi, (void __user *)arg);
  869. default:
  870. return -EINVAL;
  871. }
  872. }
  873. static const struct file_operations incfs_pending_reads_file_ops = {
  874. .read = pending_reads_read,
  875. .poll = pending_reads_poll,
  876. .open = pending_reads_open,
  877. .release = pending_reads_release,
  878. .llseek = noop_llseek,
  879. .unlocked_ioctl = pending_reads_dispatch_ioctl,
  880. .compat_ioctl = pending_reads_dispatch_ioctl
  881. };
  882. /*******************************************************************************
  883. * .log pseudo file definition
  884. ******************************************************************************/
  885. #define INCFS_LOG_INODE 3
  886. static const char log_file_name[] = INCFS_LOG_FILENAME;
  887. /* State of an open .log file, unique for each file descriptor. */
  888. struct log_file_state {
  889. struct read_log_state state;
  890. };
  891. static ssize_t log_read(struct file *f, char __user *buf, size_t len,
  892. loff_t *ppos)
  893. {
  894. struct log_file_state *log_state = f->private_data;
  895. struct mount_info *mi = get_mount_info(file_superblock(f));
  896. int total_reads_collected = 0;
  897. int rl_size;
  898. ssize_t result = 0;
  899. bool report_uid;
  900. unsigned long page = 0;
  901. struct incfs_pending_read_info *reads_buf = NULL;
  902. struct incfs_pending_read_info2 *reads_buf2 = NULL;
  903. size_t record_size;
  904. ssize_t reads_to_collect;
  905. ssize_t reads_per_page;
  906. if (!mi)
  907. return -EFAULT;
  908. report_uid = mi->mi_options.report_uid;
  909. record_size = report_uid ? sizeof(*reads_buf2) : sizeof(*reads_buf);
  910. reads_to_collect = len / record_size;
  911. reads_per_page = PAGE_SIZE / record_size;
  912. rl_size = READ_ONCE(mi->mi_log.rl_size);
  913. if (rl_size == 0)
  914. return 0;
  915. page = __get_free_page(GFP_NOFS);
  916. if (!page)
  917. return -ENOMEM;
  918. if (report_uid)
  919. reads_buf2 = (struct incfs_pending_read_info2 *)page;
  920. else
  921. reads_buf = (struct incfs_pending_read_info *)page;
  922. reads_to_collect = min_t(ssize_t, rl_size, reads_to_collect);
  923. while (reads_to_collect > 0) {
  924. struct read_log_state next_state;
  925. int reads_collected;
  926. memcpy(&next_state, &log_state->state, sizeof(next_state));
  927. reads_collected = incfs_collect_logged_reads(
  928. mi, &next_state, reads_buf, reads_buf2,
  929. min_t(ssize_t, reads_to_collect, reads_per_page));
  930. if (reads_collected <= 0) {
  931. result = total_reads_collected ?
  932. total_reads_collected * record_size :
  933. reads_collected;
  934. goto out;
  935. }
  936. if (copy_to_user(buf, (void *)page,
  937. reads_collected * record_size)) {
  938. result = total_reads_collected ?
  939. total_reads_collected * record_size :
  940. -EFAULT;
  941. goto out;
  942. }
  943. memcpy(&log_state->state, &next_state, sizeof(next_state));
  944. total_reads_collected += reads_collected;
  945. buf += reads_collected * record_size;
  946. reads_to_collect -= reads_collected;
  947. }
  948. result = total_reads_collected * record_size;
  949. *ppos = 0;
  950. out:
  951. free_page(page);
  952. return result;
  953. }
  954. static __poll_t log_poll(struct file *file, poll_table *wait)
  955. {
  956. struct log_file_state *log_state = file->private_data;
  957. struct mount_info *mi = get_mount_info(file_superblock(file));
  958. int count;
  959. __poll_t ret = 0;
  960. poll_wait(file, &mi->mi_log.ml_notif_wq, wait);
  961. count = incfs_get_uncollected_logs_count(mi, &log_state->state);
  962. if (count >= mi->mi_options.read_log_wakeup_count)
  963. ret = EPOLLIN | EPOLLRDNORM;
  964. return ret;
  965. }
  966. static int log_open(struct inode *inode, struct file *file)
  967. {
  968. struct log_file_state *log_state = NULL;
  969. struct mount_info *mi = get_mount_info(file_superblock(file));
  970. log_state = kzalloc(sizeof(*log_state), GFP_NOFS);
  971. if (!log_state)
  972. return -ENOMEM;
  973. log_state->state = incfs_get_log_state(mi);
  974. file->private_data = log_state;
  975. return 0;
  976. }
  977. static int log_release(struct inode *inode, struct file *file)
  978. {
  979. kfree(file->private_data);
  980. return 0;
  981. }
  982. static const struct file_operations incfs_log_file_ops = {
  983. .read = log_read,
  984. .poll = log_poll,
  985. .open = log_open,
  986. .release = log_release,
  987. .llseek = noop_llseek,
  988. };
  989. /*******************************************************************************
  990. * .blocks_written pseudo file definition
  991. ******************************************************************************/
  992. #define INCFS_BLOCKS_WRITTEN_INODE 4
  993. static const char blocks_written_file_name[] = INCFS_BLOCKS_WRITTEN_FILENAME;
  994. /* State of an open .blocks_written file, unique for each file descriptor. */
  995. struct blocks_written_file_state {
  996. unsigned long blocks_written;
  997. };
  998. static ssize_t blocks_written_read(struct file *f, char __user *buf, size_t len,
  999. loff_t *ppos)
  1000. {
  1001. struct mount_info *mi = get_mount_info(file_superblock(f));
  1002. struct blocks_written_file_state *state = f->private_data;
  1003. unsigned long blocks_written;
  1004. char string[21];
  1005. int result = 0;
  1006. if (!mi)
  1007. return -EFAULT;
  1008. blocks_written = atomic_read(&mi->mi_blocks_written);
  1009. if (state->blocks_written == blocks_written)
  1010. return 0;
  1011. result = snprintf(string, sizeof(string), "%lu", blocks_written);
  1012. if (result > len)
  1013. result = len;
  1014. if (copy_to_user(buf, string, result))
  1015. return -EFAULT;
  1016. state->blocks_written = blocks_written;
  1017. return result;
  1018. }
  1019. static __poll_t blocks_written_poll(struct file *f, poll_table *wait)
  1020. {
  1021. struct mount_info *mi = get_mount_info(file_superblock(f));
  1022. struct blocks_written_file_state *state = f->private_data;
  1023. unsigned long blocks_written;
  1024. if (!mi)
  1025. return 0;
  1026. poll_wait(f, &mi->mi_blocks_written_notif_wq, wait);
  1027. blocks_written = atomic_read(&mi->mi_blocks_written);
  1028. if (state->blocks_written == blocks_written)
  1029. return 0;
  1030. return EPOLLIN | EPOLLRDNORM;
  1031. }
  1032. static int blocks_written_open(struct inode *inode, struct file *file)
  1033. {
  1034. struct blocks_written_file_state *state =
  1035. kzalloc(sizeof(*state), GFP_NOFS);
  1036. if (!state)
  1037. return -ENOMEM;
  1038. state->blocks_written = -1;
  1039. file->private_data = state;
  1040. return 0;
  1041. }
  1042. static int blocks_written_release(struct inode *inode, struct file *file)
  1043. {
  1044. kfree(file->private_data);
  1045. return 0;
  1046. }
  1047. static const struct file_operations incfs_blocks_written_file_ops = {
  1048. .read = blocks_written_read,
  1049. .poll = blocks_written_poll,
  1050. .open = blocks_written_open,
  1051. .release = blocks_written_release,
  1052. .llseek = noop_llseek,
  1053. };
  1054. /*******************************************************************************
  1055. * Generic inode lookup functionality
  1056. ******************************************************************************/
  1057. const struct mem_range incfs_pseudo_file_names[] = {
  1058. { .data = (u8 *)pending_reads_file_name,
  1059. .len = ARRAY_SIZE(pending_reads_file_name) - 1 },
  1060. { .data = (u8 *)log_file_name, .len = ARRAY_SIZE(log_file_name) - 1 },
  1061. { .data = (u8 *)blocks_written_file_name,
  1062. .len = ARRAY_SIZE(blocks_written_file_name) - 1 }
  1063. };
  1064. const unsigned long incfs_pseudo_file_inodes[] = { INCFS_PENDING_READS_INODE,
  1065. INCFS_LOG_INODE,
  1066. INCFS_BLOCKS_WRITTEN_INODE };
  1067. static const struct file_operations *const pseudo_file_operations[] = {
  1068. &incfs_pending_reads_file_ops, &incfs_log_file_ops,
  1069. &incfs_blocks_written_file_ops
  1070. };
  1071. static bool is_pseudo_filename(struct mem_range name)
  1072. {
  1073. int i = 0;
  1074. for (; i < ARRAY_SIZE(incfs_pseudo_file_names); ++i)
  1075. if (incfs_equal_ranges(incfs_pseudo_file_names[i], name))
  1076. return true;
  1077. return false;
  1078. }
  1079. static bool get_pseudo_inode(int ino, struct inode *inode)
  1080. {
  1081. int i = 0;
  1082. for (; i < ARRAY_SIZE(incfs_pseudo_file_inodes); ++i)
  1083. if (ino == incfs_pseudo_file_inodes[i])
  1084. break;
  1085. if (i == ARRAY_SIZE(incfs_pseudo_file_inodes))
  1086. return false;
  1087. inode->i_ctime = (struct timespec64){};
  1088. inode->i_mtime = inode->i_ctime;
  1089. inode->i_atime = inode->i_ctime;
  1090. inode->i_size = 0;
  1091. inode->i_ino = ino;
  1092. inode->i_private = NULL;
  1093. inode_init_owner(&init_user_ns, inode, NULL, S_IFREG | READ_WRITE_FILE_MODE);
  1094. inode->i_op = &incfs_file_inode_ops;
  1095. inode->i_fop = pseudo_file_operations[i];
  1096. return true;
  1097. }
  1098. struct inode_search {
  1099. unsigned long ino;
  1100. };
  1101. static int inode_test(struct inode *inode, void *opaque)
  1102. {
  1103. struct inode_search *search = opaque;
  1104. return inode->i_ino == search->ino;
  1105. }
  1106. static int inode_set(struct inode *inode, void *opaque)
  1107. {
  1108. struct inode_search *search = opaque;
  1109. if (get_pseudo_inode(search->ino, inode))
  1110. return 0;
  1111. /* Unknown inode requested. */
  1112. return -EINVAL;
  1113. }
  1114. static struct inode *fetch_inode(struct super_block *sb, unsigned long ino)
  1115. {
  1116. struct inode_search search = {
  1117. .ino = ino
  1118. };
  1119. struct inode *inode = iget5_locked(sb, search.ino, inode_test,
  1120. inode_set, &search);
  1121. if (!inode)
  1122. return ERR_PTR(-ENOMEM);
  1123. if (inode->i_state & I_NEW)
  1124. unlock_new_inode(inode);
  1125. return inode;
  1126. }
  1127. int dir_lookup_pseudo_files(struct super_block *sb, struct dentry *dentry)
  1128. {
  1129. struct mem_range name_range =
  1130. range((u8 *)dentry->d_name.name, dentry->d_name.len);
  1131. unsigned long ino;
  1132. struct inode *inode;
  1133. int i = 0;
  1134. for (; i < ARRAY_SIZE(incfs_pseudo_file_names); ++i)
  1135. if (incfs_equal_ranges(incfs_pseudo_file_names[i], name_range))
  1136. break;
  1137. if (i == ARRAY_SIZE(incfs_pseudo_file_names))
  1138. return -ENOENT;
  1139. ino = incfs_pseudo_file_inodes[i];
  1140. inode = fetch_inode(sb, ino);
  1141. if (IS_ERR(inode))
  1142. return PTR_ERR(inode);
  1143. d_add(dentry, inode);
  1144. return 0;
  1145. }
  1146. int emit_pseudo_files(struct dir_context *ctx)
  1147. {
  1148. loff_t i = ctx->pos;
  1149. for (; i < ARRAY_SIZE(incfs_pseudo_file_names); ++i) {
  1150. if (!dir_emit(ctx, incfs_pseudo_file_names[i].data,
  1151. incfs_pseudo_file_names[i].len,
  1152. incfs_pseudo_file_inodes[i], DT_REG))
  1153. return -EINVAL;
  1154. ctx->pos++;
  1155. }
  1156. return 0;
  1157. }