pseudo_files.c 33 KB

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