inode.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/proc/inode.c
  4. *
  5. * Copyright (C) 1991, 1992 Linus Torvalds
  6. */
  7. #include <linux/cache.h>
  8. #include <linux/time.h>
  9. #include <linux/proc_fs.h>
  10. #include <linux/kernel.h>
  11. #include <linux/pid_namespace.h>
  12. #include <linux/mm.h>
  13. #include <linux/string.h>
  14. #include <linux/stat.h>
  15. #include <linux/completion.h>
  16. #include <linux/poll.h>
  17. #include <linux/printk.h>
  18. #include <linux/file.h>
  19. #include <linux/limits.h>
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/sysctl.h>
  23. #include <linux/seq_file.h>
  24. #include <linux/slab.h>
  25. #include <linux/mount.h>
  26. #include <linux/bug.h>
  27. #include "internal.h"
  28. static void proc_evict_inode(struct inode *inode)
  29. {
  30. struct ctl_table_header *head;
  31. struct proc_inode *ei = PROC_I(inode);
  32. truncate_inode_pages_final(&inode->i_data);
  33. clear_inode(inode);
  34. /* Stop tracking associated processes */
  35. if (ei->pid)
  36. proc_pid_evict_inode(ei);
  37. head = ei->sysctl;
  38. if (head) {
  39. WRITE_ONCE(ei->sysctl, NULL);
  40. proc_sys_evict_inode(inode, head);
  41. }
  42. }
  43. static struct kmem_cache *proc_inode_cachep __ro_after_init;
  44. static struct kmem_cache *pde_opener_cache __ro_after_init;
  45. static struct inode *proc_alloc_inode(struct super_block *sb)
  46. {
  47. struct proc_inode *ei;
  48. ei = alloc_inode_sb(sb, proc_inode_cachep, GFP_KERNEL);
  49. if (!ei)
  50. return NULL;
  51. ei->pid = NULL;
  52. ei->fd = 0;
  53. ei->op.proc_get_link = NULL;
  54. ei->pde = NULL;
  55. ei->sysctl = NULL;
  56. ei->sysctl_entry = NULL;
  57. INIT_HLIST_NODE(&ei->sibling_inodes);
  58. ei->ns_ops = NULL;
  59. return &ei->vfs_inode;
  60. }
  61. static void proc_free_inode(struct inode *inode)
  62. {
  63. struct proc_inode *ei = PROC_I(inode);
  64. if (ei->pid)
  65. put_pid(ei->pid);
  66. /* Let go of any associated proc directory entry */
  67. if (ei->pde)
  68. pde_put(ei->pde);
  69. kmem_cache_free(proc_inode_cachep, PROC_I(inode));
  70. }
  71. static void init_once(void *foo)
  72. {
  73. struct proc_inode *ei = (struct proc_inode *) foo;
  74. inode_init_once(&ei->vfs_inode);
  75. }
  76. void __init proc_init_kmemcache(void)
  77. {
  78. proc_inode_cachep = kmem_cache_create("proc_inode_cache",
  79. sizeof(struct proc_inode),
  80. 0, (SLAB_RECLAIM_ACCOUNT|
  81. SLAB_ACCOUNT|
  82. SLAB_PANIC),
  83. init_once);
  84. pde_opener_cache =
  85. kmem_cache_create("pde_opener", sizeof(struct pde_opener), 0,
  86. SLAB_ACCOUNT|SLAB_PANIC, NULL);
  87. proc_dir_entry_cache = kmem_cache_create_usercopy(
  88. "proc_dir_entry", SIZEOF_PDE, 0, SLAB_PANIC,
  89. offsetof(struct proc_dir_entry, inline_name),
  90. SIZEOF_PDE_INLINE_NAME, NULL);
  91. BUILD_BUG_ON(sizeof(struct proc_dir_entry) >= SIZEOF_PDE);
  92. }
  93. void proc_invalidate_siblings_dcache(struct hlist_head *inodes, spinlock_t *lock)
  94. {
  95. struct hlist_node *node;
  96. struct super_block *old_sb = NULL;
  97. rcu_read_lock();
  98. while ((node = hlist_first_rcu(inodes))) {
  99. struct proc_inode *ei = hlist_entry(node, struct proc_inode, sibling_inodes);
  100. struct super_block *sb;
  101. struct inode *inode;
  102. spin_lock(lock);
  103. hlist_del_init_rcu(&ei->sibling_inodes);
  104. spin_unlock(lock);
  105. inode = &ei->vfs_inode;
  106. sb = inode->i_sb;
  107. if ((sb != old_sb) && !atomic_inc_not_zero(&sb->s_active))
  108. continue;
  109. inode = igrab(inode);
  110. rcu_read_unlock();
  111. if (sb != old_sb) {
  112. if (old_sb)
  113. deactivate_super(old_sb);
  114. old_sb = sb;
  115. }
  116. if (unlikely(!inode)) {
  117. rcu_read_lock();
  118. continue;
  119. }
  120. if (S_ISDIR(inode->i_mode)) {
  121. struct dentry *dir = d_find_any_alias(inode);
  122. if (dir) {
  123. d_invalidate(dir);
  124. dput(dir);
  125. }
  126. } else {
  127. struct dentry *dentry;
  128. while ((dentry = d_find_alias(inode))) {
  129. d_invalidate(dentry);
  130. dput(dentry);
  131. }
  132. }
  133. iput(inode);
  134. rcu_read_lock();
  135. }
  136. rcu_read_unlock();
  137. if (old_sb)
  138. deactivate_super(old_sb);
  139. }
  140. static inline const char *hidepid2str(enum proc_hidepid v)
  141. {
  142. switch (v) {
  143. case HIDEPID_OFF: return "off";
  144. case HIDEPID_NO_ACCESS: return "noaccess";
  145. case HIDEPID_INVISIBLE: return "invisible";
  146. case HIDEPID_NOT_PTRACEABLE: return "ptraceable";
  147. }
  148. WARN_ONCE(1, "bad hide_pid value: %d\n", v);
  149. return "unknown";
  150. }
  151. static int proc_show_options(struct seq_file *seq, struct dentry *root)
  152. {
  153. struct proc_fs_info *fs_info = proc_sb_info(root->d_sb);
  154. if (!gid_eq(fs_info->pid_gid, GLOBAL_ROOT_GID))
  155. seq_printf(seq, ",gid=%u", from_kgid_munged(&init_user_ns, fs_info->pid_gid));
  156. if (fs_info->hide_pid != HIDEPID_OFF)
  157. seq_printf(seq, ",hidepid=%s", hidepid2str(fs_info->hide_pid));
  158. if (fs_info->pidonly != PROC_PIDONLY_OFF)
  159. seq_printf(seq, ",subset=pid");
  160. return 0;
  161. }
  162. const struct super_operations proc_sops = {
  163. .alloc_inode = proc_alloc_inode,
  164. .free_inode = proc_free_inode,
  165. .drop_inode = inode_just_drop,
  166. .evict_inode = proc_evict_inode,
  167. .statfs = simple_statfs,
  168. .show_options = proc_show_options,
  169. };
  170. enum {BIAS = -1U<<31};
  171. static inline int use_pde(struct proc_dir_entry *pde)
  172. {
  173. return likely(atomic_inc_unless_negative(&pde->in_use));
  174. }
  175. static void unuse_pde(struct proc_dir_entry *pde)
  176. {
  177. if (unlikely(atomic_dec_return(&pde->in_use) == BIAS))
  178. complete(pde->pde_unload_completion);
  179. }
  180. /*
  181. * At most 2 contexts can enter this function: the one doing the last
  182. * close on the descriptor and whoever is deleting PDE itself.
  183. *
  184. * First to enter calls ->proc_release hook and signals its completion
  185. * to the second one which waits and then does nothing.
  186. *
  187. * PDE is locked on entry, unlocked on exit.
  188. */
  189. static void close_pdeo(struct proc_dir_entry *pde, struct pde_opener *pdeo)
  190. __releases(&pde->pde_unload_lock)
  191. {
  192. /*
  193. * close() (proc_reg_release()) can't delete an entry and proceed:
  194. * ->release hook needs to be available at the right moment.
  195. *
  196. * rmmod (remove_proc_entry() et al) can't delete an entry and proceed:
  197. * "struct file" needs to be available at the right moment.
  198. */
  199. if (pdeo->closing) {
  200. /* somebody else is doing that, just wait */
  201. DECLARE_COMPLETION_ONSTACK(c);
  202. pdeo->c = &c;
  203. spin_unlock(&pde->pde_unload_lock);
  204. wait_for_completion(&c);
  205. } else {
  206. struct file *file;
  207. struct completion *c;
  208. pdeo->closing = true;
  209. spin_unlock(&pde->pde_unload_lock);
  210. file = pdeo->file;
  211. pde->proc_ops->proc_release(file_inode(file), file);
  212. spin_lock(&pde->pde_unload_lock);
  213. /* Strictly after ->proc_release, see above. */
  214. list_del(&pdeo->lh);
  215. c = pdeo->c;
  216. spin_unlock(&pde->pde_unload_lock);
  217. if (unlikely(c))
  218. complete(c);
  219. kmem_cache_free(pde_opener_cache, pdeo);
  220. }
  221. }
  222. void proc_entry_rundown(struct proc_dir_entry *de)
  223. {
  224. DECLARE_COMPLETION_ONSTACK(c);
  225. /* Wait until all existing callers into module are done. */
  226. de->pde_unload_completion = &c;
  227. if (atomic_add_return(BIAS, &de->in_use) != BIAS)
  228. wait_for_completion(&c);
  229. /* ->pde_openers list can't grow from now on. */
  230. spin_lock(&de->pde_unload_lock);
  231. while (!list_empty(&de->pde_openers)) {
  232. struct pde_opener *pdeo;
  233. pdeo = list_first_entry(&de->pde_openers, struct pde_opener, lh);
  234. close_pdeo(de, pdeo);
  235. spin_lock(&de->pde_unload_lock);
  236. }
  237. spin_unlock(&de->pde_unload_lock);
  238. }
  239. static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
  240. {
  241. struct proc_dir_entry *pde = PDE(file_inode(file));
  242. loff_t rv = -EINVAL;
  243. if (pde_is_permanent(pde)) {
  244. return pde->proc_ops->proc_lseek(file, offset, whence);
  245. } else if (use_pde(pde)) {
  246. rv = pde->proc_ops->proc_lseek(file, offset, whence);
  247. unuse_pde(pde);
  248. }
  249. return rv;
  250. }
  251. static ssize_t proc_reg_read_iter(struct kiocb *iocb, struct iov_iter *iter)
  252. {
  253. struct proc_dir_entry *pde = PDE(file_inode(iocb->ki_filp));
  254. ssize_t ret;
  255. if (pde_is_permanent(pde))
  256. return pde->proc_ops->proc_read_iter(iocb, iter);
  257. if (!use_pde(pde))
  258. return -EIO;
  259. ret = pde->proc_ops->proc_read_iter(iocb, iter);
  260. unuse_pde(pde);
  261. return ret;
  262. }
  263. static ssize_t pde_read(struct proc_dir_entry *pde, struct file *file, char __user *buf, size_t count, loff_t *ppos)
  264. {
  265. const auto read = pde->proc_ops->proc_read;
  266. if (read)
  267. return read(file, buf, count, ppos);
  268. return -EIO;
  269. }
  270. static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  271. {
  272. struct proc_dir_entry *pde = PDE(file_inode(file));
  273. ssize_t rv = -EIO;
  274. if (pde_is_permanent(pde)) {
  275. return pde_read(pde, file, buf, count, ppos);
  276. } else if (use_pde(pde)) {
  277. rv = pde_read(pde, file, buf, count, ppos);
  278. unuse_pde(pde);
  279. }
  280. return rv;
  281. }
  282. static ssize_t pde_write(struct proc_dir_entry *pde, struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  283. {
  284. const auto write = pde->proc_ops->proc_write;
  285. if (write)
  286. return write(file, buf, count, ppos);
  287. return -EIO;
  288. }
  289. static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  290. {
  291. struct proc_dir_entry *pde = PDE(file_inode(file));
  292. ssize_t rv = -EIO;
  293. if (pde_is_permanent(pde)) {
  294. return pde_write(pde, file, buf, count, ppos);
  295. } else if (use_pde(pde)) {
  296. rv = pde_write(pde, file, buf, count, ppos);
  297. unuse_pde(pde);
  298. }
  299. return rv;
  300. }
  301. static __poll_t pde_poll(struct proc_dir_entry *pde, struct file *file, struct poll_table_struct *pts)
  302. {
  303. const auto poll = pde->proc_ops->proc_poll;
  304. if (poll)
  305. return poll(file, pts);
  306. return DEFAULT_POLLMASK;
  307. }
  308. static __poll_t proc_reg_poll(struct file *file, struct poll_table_struct *pts)
  309. {
  310. struct proc_dir_entry *pde = PDE(file_inode(file));
  311. __poll_t rv = DEFAULT_POLLMASK;
  312. if (pde_is_permanent(pde)) {
  313. return pde_poll(pde, file, pts);
  314. } else if (use_pde(pde)) {
  315. rv = pde_poll(pde, file, pts);
  316. unuse_pde(pde);
  317. }
  318. return rv;
  319. }
  320. static long pde_ioctl(struct proc_dir_entry *pde, struct file *file, unsigned int cmd, unsigned long arg)
  321. {
  322. const auto ioctl = pde->proc_ops->proc_ioctl;
  323. if (ioctl)
  324. return ioctl(file, cmd, arg);
  325. return -ENOTTY;
  326. }
  327. static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  328. {
  329. struct proc_dir_entry *pde = PDE(file_inode(file));
  330. long rv = -ENOTTY;
  331. if (pde_is_permanent(pde)) {
  332. return pde_ioctl(pde, file, cmd, arg);
  333. } else if (use_pde(pde)) {
  334. rv = pde_ioctl(pde, file, cmd, arg);
  335. unuse_pde(pde);
  336. }
  337. return rv;
  338. }
  339. #ifdef CONFIG_COMPAT
  340. static long pde_compat_ioctl(struct proc_dir_entry *pde, struct file *file, unsigned int cmd, unsigned long arg)
  341. {
  342. const auto compat_ioctl = pde->proc_ops->proc_compat_ioctl;
  343. if (compat_ioctl)
  344. return compat_ioctl(file, cmd, arg);
  345. return -ENOTTY;
  346. }
  347. static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  348. {
  349. struct proc_dir_entry *pde = PDE(file_inode(file));
  350. long rv = -ENOTTY;
  351. if (pde_is_permanent(pde)) {
  352. return pde_compat_ioctl(pde, file, cmd, arg);
  353. } else if (use_pde(pde)) {
  354. rv = pde_compat_ioctl(pde, file, cmd, arg);
  355. unuse_pde(pde);
  356. }
  357. return rv;
  358. }
  359. #endif
  360. static int pde_mmap(struct proc_dir_entry *pde, struct file *file, struct vm_area_struct *vma)
  361. {
  362. const auto mmap = pde->proc_ops->proc_mmap;
  363. if (mmap)
  364. return mmap(file, vma);
  365. return -EIO;
  366. }
  367. static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
  368. {
  369. struct proc_dir_entry *pde = PDE(file_inode(file));
  370. int rv = -EIO;
  371. if (pde_is_permanent(pde)) {
  372. return pde_mmap(pde, file, vma);
  373. } else if (use_pde(pde)) {
  374. rv = pde_mmap(pde, file, vma);
  375. unuse_pde(pde);
  376. }
  377. return rv;
  378. }
  379. static unsigned long
  380. pde_get_unmapped_area(struct proc_dir_entry *pde, struct file *file, unsigned long orig_addr,
  381. unsigned long len, unsigned long pgoff,
  382. unsigned long flags)
  383. {
  384. if (pde->proc_ops->proc_get_unmapped_area)
  385. return pde->proc_ops->proc_get_unmapped_area(file, orig_addr, len, pgoff, flags);
  386. #ifdef CONFIG_MMU
  387. return mm_get_unmapped_area(file, orig_addr, len, pgoff, flags);
  388. #endif
  389. return orig_addr;
  390. }
  391. static unsigned long
  392. proc_reg_get_unmapped_area(struct file *file, unsigned long orig_addr,
  393. unsigned long len, unsigned long pgoff,
  394. unsigned long flags)
  395. {
  396. struct proc_dir_entry *pde = PDE(file_inode(file));
  397. unsigned long rv = -EIO;
  398. if (pde_is_permanent(pde)) {
  399. return pde_get_unmapped_area(pde, file, orig_addr, len, pgoff, flags);
  400. } else if (use_pde(pde)) {
  401. rv = pde_get_unmapped_area(pde, file, orig_addr, len, pgoff, flags);
  402. unuse_pde(pde);
  403. }
  404. return rv;
  405. }
  406. static int proc_reg_open(struct inode *inode, struct file *file)
  407. {
  408. struct proc_dir_entry *pde = PDE(inode);
  409. int rv = 0;
  410. typeof_member(struct proc_ops, proc_open) open;
  411. struct pde_opener *pdeo;
  412. if (!pde_has_proc_lseek(pde))
  413. file->f_mode &= ~FMODE_LSEEK;
  414. if (pde_is_permanent(pde)) {
  415. open = pde->proc_ops->proc_open;
  416. if (open)
  417. rv = open(inode, file);
  418. return rv;
  419. }
  420. /*
  421. * Ensure that
  422. * 1) PDE's ->release hook will be called no matter what
  423. * either normally by close()/->release, or forcefully by
  424. * rmmod/remove_proc_entry.
  425. *
  426. * 2) rmmod isn't blocked by opening file in /proc and sitting on
  427. * the descriptor (including "rmmod foo </proc/foo" scenario).
  428. *
  429. * Save every "struct file" with custom ->release hook.
  430. */
  431. if (!use_pde(pde))
  432. return -ENOENT;
  433. const auto release = pde->proc_ops->proc_release;
  434. if (release) {
  435. pdeo = kmem_cache_alloc(pde_opener_cache, GFP_KERNEL);
  436. if (!pdeo) {
  437. rv = -ENOMEM;
  438. goto out_unuse;
  439. }
  440. }
  441. open = pde->proc_ops->proc_open;
  442. if (open)
  443. rv = open(inode, file);
  444. if (release) {
  445. if (rv == 0) {
  446. /* To know what to release. */
  447. pdeo->file = file;
  448. pdeo->closing = false;
  449. pdeo->c = NULL;
  450. spin_lock(&pde->pde_unload_lock);
  451. list_add(&pdeo->lh, &pde->pde_openers);
  452. spin_unlock(&pde->pde_unload_lock);
  453. } else
  454. kmem_cache_free(pde_opener_cache, pdeo);
  455. }
  456. out_unuse:
  457. unuse_pde(pde);
  458. return rv;
  459. }
  460. static int proc_reg_release(struct inode *inode, struct file *file)
  461. {
  462. struct proc_dir_entry *pde = PDE(inode);
  463. struct pde_opener *pdeo;
  464. if (pde_is_permanent(pde)) {
  465. const auto release = pde->proc_ops->proc_release;
  466. if (release)
  467. return release(inode, file);
  468. return 0;
  469. }
  470. spin_lock(&pde->pde_unload_lock);
  471. list_for_each_entry(pdeo, &pde->pde_openers, lh) {
  472. if (pdeo->file == file) {
  473. close_pdeo(pde, pdeo);
  474. return 0;
  475. }
  476. }
  477. spin_unlock(&pde->pde_unload_lock);
  478. return 0;
  479. }
  480. static const struct file_operations proc_reg_file_ops = {
  481. .llseek = proc_reg_llseek,
  482. .read = proc_reg_read,
  483. .write = proc_reg_write,
  484. .poll = proc_reg_poll,
  485. .unlocked_ioctl = proc_reg_unlocked_ioctl,
  486. .mmap = proc_reg_mmap,
  487. .get_unmapped_area = proc_reg_get_unmapped_area,
  488. .open = proc_reg_open,
  489. .release = proc_reg_release,
  490. };
  491. static const struct file_operations proc_iter_file_ops = {
  492. .llseek = proc_reg_llseek,
  493. .read_iter = proc_reg_read_iter,
  494. .write = proc_reg_write,
  495. .splice_read = copy_splice_read,
  496. .poll = proc_reg_poll,
  497. .unlocked_ioctl = proc_reg_unlocked_ioctl,
  498. .mmap = proc_reg_mmap,
  499. .get_unmapped_area = proc_reg_get_unmapped_area,
  500. .open = proc_reg_open,
  501. .release = proc_reg_release,
  502. };
  503. #ifdef CONFIG_COMPAT
  504. static const struct file_operations proc_reg_file_ops_compat = {
  505. .llseek = proc_reg_llseek,
  506. .read = proc_reg_read,
  507. .write = proc_reg_write,
  508. .poll = proc_reg_poll,
  509. .unlocked_ioctl = proc_reg_unlocked_ioctl,
  510. .compat_ioctl = proc_reg_compat_ioctl,
  511. .mmap = proc_reg_mmap,
  512. .get_unmapped_area = proc_reg_get_unmapped_area,
  513. .open = proc_reg_open,
  514. .release = proc_reg_release,
  515. };
  516. static const struct file_operations proc_iter_file_ops_compat = {
  517. .llseek = proc_reg_llseek,
  518. .read_iter = proc_reg_read_iter,
  519. .splice_read = copy_splice_read,
  520. .write = proc_reg_write,
  521. .poll = proc_reg_poll,
  522. .unlocked_ioctl = proc_reg_unlocked_ioctl,
  523. .compat_ioctl = proc_reg_compat_ioctl,
  524. .mmap = proc_reg_mmap,
  525. .get_unmapped_area = proc_reg_get_unmapped_area,
  526. .open = proc_reg_open,
  527. .release = proc_reg_release,
  528. };
  529. #endif
  530. static void proc_put_link(void *p)
  531. {
  532. unuse_pde(p);
  533. }
  534. static const char *proc_get_link(struct dentry *dentry,
  535. struct inode *inode,
  536. struct delayed_call *done)
  537. {
  538. struct proc_dir_entry *pde = PDE(inode);
  539. if (!use_pde(pde))
  540. return ERR_PTR(-EINVAL);
  541. set_delayed_call(done, proc_put_link, pde);
  542. return pde->data;
  543. }
  544. const struct inode_operations proc_link_inode_operations = {
  545. .get_link = proc_get_link,
  546. };
  547. struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de)
  548. {
  549. struct inode *inode = new_inode(sb);
  550. if (!inode) {
  551. pde_put(de);
  552. return NULL;
  553. }
  554. inode->i_private = de->data;
  555. inode->i_ino = de->low_ino;
  556. simple_inode_init_ts(inode);
  557. PROC_I(inode)->pde = de;
  558. if (is_empty_pde(de)) {
  559. make_empty_dir_inode(inode);
  560. return inode;
  561. }
  562. if (de->mode) {
  563. inode->i_mode = de->mode;
  564. inode->i_uid = de->uid;
  565. inode->i_gid = de->gid;
  566. }
  567. if (de->size)
  568. inode->i_size = de->size;
  569. if (de->nlink)
  570. set_nlink(inode, de->nlink);
  571. if (S_ISREG(inode->i_mode)) {
  572. inode->i_op = de->proc_iops;
  573. if (pde_has_proc_read_iter(de))
  574. inode->i_fop = &proc_iter_file_ops;
  575. else
  576. inode->i_fop = &proc_reg_file_ops;
  577. #ifdef CONFIG_COMPAT
  578. if (pde_has_proc_compat_ioctl(de)) {
  579. if (pde_has_proc_read_iter(de))
  580. inode->i_fop = &proc_iter_file_ops_compat;
  581. else
  582. inode->i_fop = &proc_reg_file_ops_compat;
  583. }
  584. #endif
  585. } else if (S_ISDIR(inode->i_mode)) {
  586. inode->i_op = de->proc_iops;
  587. inode->i_fop = de->proc_dir_ops;
  588. } else if (S_ISLNK(inode->i_mode)) {
  589. inode->i_op = de->proc_iops;
  590. inode->i_fop = NULL;
  591. } else {
  592. BUG();
  593. }
  594. return inode;
  595. }