inode.c 17 KB

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