inode.c 17 KB

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