~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/proc/generic.c

Version: ~ [ linux-5.9-rc6 ] ~ [ linux-5.8.10 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.66 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.146 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.198 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.236 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.236 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * proc/fs/generic.c --- generic routines for the proc-fs
  3  *
  4  * This file contains generic proc-fs routines for handling
  5  * directories and files.
  6  * 
  7  * Copyright (C) 1991, 1992 Linus Torvalds.
  8  * Copyright (C) 1997 Theodore Ts'o
  9  */
 10 
 11 #include <linux/errno.h>
 12 #include <linux/time.h>
 13 #include <linux/proc_fs.h>
 14 #include <linux/stat.h>
 15 #include <linux/mm.h>
 16 #include <linux/module.h>
 17 #include <linux/slab.h>
 18 #include <linux/printk.h>
 19 #include <linux/mount.h>
 20 #include <linux/init.h>
 21 #include <linux/idr.h>
 22 #include <linux/bitops.h>
 23 #include <linux/spinlock.h>
 24 #include <linux/completion.h>
 25 #include <asm/uaccess.h>
 26 
 27 #include "internal.h"
 28 
 29 DEFINE_SPINLOCK(proc_subdir_lock);
 30 
 31 static int proc_match(unsigned int len, const char *name, struct proc_dir_entry *de)
 32 {
 33         if (de->namelen != len)
 34                 return 0;
 35         return !memcmp(name, de->name, len);
 36 }
 37 
 38 static int proc_notify_change(struct dentry *dentry, struct iattr *iattr)
 39 {
 40         struct inode *inode = dentry->d_inode;
 41         struct proc_dir_entry *de = PDE(inode);
 42         int error;
 43 
 44         error = inode_change_ok(inode, iattr);
 45         if (error)
 46                 return error;
 47 
 48         setattr_copy(inode, iattr);
 49         mark_inode_dirty(inode);
 50 
 51         proc_set_user(de, inode->i_uid, inode->i_gid);
 52         de->mode = inode->i_mode;
 53         return 0;
 54 }
 55 
 56 static int proc_getattr(struct vfsmount *mnt, struct dentry *dentry,
 57                         struct kstat *stat)
 58 {
 59         struct inode *inode = dentry->d_inode;
 60         struct proc_dir_entry *de = PROC_I(inode)->pde;
 61         if (de && de->nlink)
 62                 set_nlink(inode, de->nlink);
 63 
 64         generic_fillattr(inode, stat);
 65         return 0;
 66 }
 67 
 68 static const struct inode_operations proc_file_inode_operations = {
 69         .setattr        = proc_notify_change,
 70 };
 71 
 72 /*
 73  * This function parses a name such as "tty/driver/serial", and
 74  * returns the struct proc_dir_entry for "/proc/tty/driver", and
 75  * returns "serial" in residual.
 76  */
 77 static int __xlate_proc_name(const char *name, struct proc_dir_entry **ret,
 78                              const char **residual)
 79 {
 80         const char              *cp = name, *next;
 81         struct proc_dir_entry   *de;
 82         unsigned int            len;
 83 
 84         de = *ret;
 85         if (!de)
 86                 de = &proc_root;
 87 
 88         while (1) {
 89                 next = strchr(cp, '/');
 90                 if (!next)
 91                         break;
 92 
 93                 len = next - cp;
 94                 for (de = de->subdir; de ; de = de->next) {
 95                         if (proc_match(len, cp, de))
 96                                 break;
 97                 }
 98                 if (!de) {
 99                         WARN(1, "name '%s'\n", name);
100                         return -ENOENT;
101                 }
102                 cp += len + 1;
103         }
104         *residual = cp;
105         *ret = de;
106         return 0;
107 }
108 
109 static int xlate_proc_name(const char *name, struct proc_dir_entry **ret,
110                            const char **residual)
111 {
112         int rv;
113 
114         spin_lock(&proc_subdir_lock);
115         rv = __xlate_proc_name(name, ret, residual);
116         spin_unlock(&proc_subdir_lock);
117         return rv;
118 }
119 
120 static DEFINE_IDA(proc_inum_ida);
121 static DEFINE_SPINLOCK(proc_inum_lock); /* protects the above */
122 
123 #define PROC_DYNAMIC_FIRST 0xF0000000U
124 
125 /*
126  * Return an inode number between PROC_DYNAMIC_FIRST and
127  * 0xffffffff, or zero on failure.
128  */
129 int proc_alloc_inum(unsigned int *inum)
130 {
131         unsigned int i;
132         int error;
133 
134 retry:
135         if (!ida_pre_get(&proc_inum_ida, GFP_KERNEL))
136                 return -ENOMEM;
137 
138         spin_lock_irq(&proc_inum_lock);
139         error = ida_get_new(&proc_inum_ida, &i);
140         spin_unlock_irq(&proc_inum_lock);
141         if (error == -EAGAIN)
142                 goto retry;
143         else if (error)
144                 return error;
145 
146         if (i > UINT_MAX - PROC_DYNAMIC_FIRST) {
147                 spin_lock_irq(&proc_inum_lock);
148                 ida_remove(&proc_inum_ida, i);
149                 spin_unlock_irq(&proc_inum_lock);
150                 return -ENOSPC;
151         }
152         *inum = PROC_DYNAMIC_FIRST + i;
153         return 0;
154 }
155 
156 void proc_free_inum(unsigned int inum)
157 {
158         unsigned long flags;
159         spin_lock_irqsave(&proc_inum_lock, flags);
160         ida_remove(&proc_inum_ida, inum - PROC_DYNAMIC_FIRST);
161         spin_unlock_irqrestore(&proc_inum_lock, flags);
162 }
163 
164 /*
165  * Don't create negative dentries here, return -ENOENT by hand
166  * instead.
167  */
168 struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *dir,
169                 struct dentry *dentry)
170 {
171         struct inode *inode;
172 
173         spin_lock(&proc_subdir_lock);
174         for (de = de->subdir; de ; de = de->next) {
175                 if (de->namelen != dentry->d_name.len)
176                         continue;
177                 if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
178                         pde_get(de);
179                         spin_unlock(&proc_subdir_lock);
180                         inode = proc_get_inode(dir->i_sb, de);
181                         if (!inode)
182                                 return ERR_PTR(-ENOMEM);
183                         d_set_d_op(dentry, &simple_dentry_operations);
184                         d_add(dentry, inode);
185                         return NULL;
186                 }
187         }
188         spin_unlock(&proc_subdir_lock);
189         return ERR_PTR(-ENOENT);
190 }
191 
192 struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry,
193                 unsigned int flags)
194 {
195         return proc_lookup_de(PDE(dir), dir, dentry);
196 }
197 
198 /*
199  * This returns non-zero if at EOF, so that the /proc
200  * root directory can use this and check if it should
201  * continue with the <pid> entries..
202  *
203  * Note that the VFS-layer doesn't care about the return
204  * value of the readdir() call, as long as it's non-negative
205  * for success..
206  */
207 int proc_readdir_de(struct proc_dir_entry *de, struct file *file,
208                     struct dir_context *ctx)
209 {
210         int i;
211 
212         if (!dir_emit_dots(file, ctx))
213                 return 0;
214 
215         spin_lock(&proc_subdir_lock);
216         de = de->subdir;
217         i = ctx->pos - 2;
218         for (;;) {
219                 if (!de) {
220                         spin_unlock(&proc_subdir_lock);
221                         return 0;
222                 }
223                 if (!i)
224                         break;
225                 de = de->next;
226                 i--;
227         }
228 
229         do {
230                 struct proc_dir_entry *next;
231                 pde_get(de);
232                 spin_unlock(&proc_subdir_lock);
233                 if (!dir_emit(ctx, de->name, de->namelen,
234                             de->low_ino, de->mode >> 12)) {
235                         pde_put(de);
236                         return 0;
237                 }
238                 spin_lock(&proc_subdir_lock);
239                 ctx->pos++;
240                 next = de->next;
241                 pde_put(de);
242                 de = next;
243         } while (de);
244         spin_unlock(&proc_subdir_lock);
245         return 1;
246 }
247 
248 int proc_readdir(struct file *file, struct dir_context *ctx)
249 {
250         struct inode *inode = file_inode(file);
251 
252         return proc_readdir_de(PDE(inode), file, ctx);
253 }
254 
255 /*
256  * These are the generic /proc directory operations. They
257  * use the in-memory "struct proc_dir_entry" tree to parse
258  * the /proc directory.
259  */
260 static const struct file_operations proc_dir_operations = {
261         .llseek                 = generic_file_llseek,
262         .read                   = generic_read_dir,
263         .iterate                = proc_readdir,
264 };
265 
266 /*
267  * proc directories can do almost nothing..
268  */
269 static const struct inode_operations proc_dir_inode_operations = {
270         .lookup         = proc_lookup,
271         .getattr        = proc_getattr,
272         .setattr        = proc_notify_change,
273 };
274 
275 static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
276 {
277         struct proc_dir_entry *tmp;
278         int ret;
279         
280         ret = proc_alloc_inum(&dp->low_ino);
281         if (ret)
282                 return ret;
283 
284         if (S_ISDIR(dp->mode)) {
285                 dp->proc_fops = &proc_dir_operations;
286                 dp->proc_iops = &proc_dir_inode_operations;
287                 dir->nlink++;
288         } else if (S_ISLNK(dp->mode)) {
289                 dp->proc_iops = &proc_link_inode_operations;
290         } else if (S_ISREG(dp->mode)) {
291                 BUG_ON(dp->proc_fops == NULL);
292                 dp->proc_iops = &proc_file_inode_operations;
293         } else {
294                 WARN_ON(1);
295                 return -EINVAL;
296         }
297 
298         spin_lock(&proc_subdir_lock);
299 
300         for (tmp = dir->subdir; tmp; tmp = tmp->next)
301                 if (strcmp(tmp->name, dp->name) == 0) {
302                         WARN(1, "proc_dir_entry '%s/%s' already registered\n",
303                                 dir->name, dp->name);
304                         break;
305                 }
306 
307         dp->next = dir->subdir;
308         dp->parent = dir;
309         dir->subdir = dp;
310         spin_unlock(&proc_subdir_lock);
311 
312         return 0;
313 }
314 
315 static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
316                                           const char *name,
317                                           umode_t mode,
318                                           nlink_t nlink)
319 {
320         struct proc_dir_entry *ent = NULL;
321         const char *fn = name;
322         unsigned int len;
323 
324         /* make sure name is valid */
325         if (!name || !strlen(name))
326                 goto out;
327 
328         if (xlate_proc_name(name, parent, &fn) != 0)
329                 goto out;
330 
331         /* At this point there must not be any '/' characters beyond *fn */
332         if (strchr(fn, '/'))
333                 goto out;
334 
335         len = strlen(fn);
336 
337         ent = kzalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
338         if (!ent)
339                 goto out;
340 
341         memcpy(ent->name, fn, len + 1);
342         ent->namelen = len;
343         ent->mode = mode;
344         ent->nlink = nlink;
345         atomic_set(&ent->count, 1);
346         spin_lock_init(&ent->pde_unload_lock);
347         INIT_LIST_HEAD(&ent->pde_openers);
348 out:
349         return ent;
350 }
351 
352 struct proc_dir_entry *proc_symlink(const char *name,
353                 struct proc_dir_entry *parent, const char *dest)
354 {
355         struct proc_dir_entry *ent;
356 
357         ent = __proc_create(&parent, name,
358                           (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
359 
360         if (ent) {
361                 ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
362                 if (ent->data) {
363                         strcpy((char*)ent->data,dest);
364                         if (proc_register(parent, ent) < 0) {
365                                 kfree(ent->data);
366                                 kfree(ent);
367                                 ent = NULL;
368                         }
369                 } else {
370                         kfree(ent);
371                         ent = NULL;
372                 }
373         }
374         return ent;
375 }
376 EXPORT_SYMBOL(proc_symlink);
377 
378 struct proc_dir_entry *proc_mkdir_data(const char *name, umode_t mode,
379                 struct proc_dir_entry *parent, void *data)
380 {
381         struct proc_dir_entry *ent;
382 
383         if (mode == 0)
384                 mode = S_IRUGO | S_IXUGO;
385 
386         ent = __proc_create(&parent, name, S_IFDIR | mode, 2);
387         if (ent) {
388                 ent->data = data;
389                 if (proc_register(parent, ent) < 0) {
390                         kfree(ent);
391                         ent = NULL;
392                 }
393         }
394         return ent;
395 }
396 EXPORT_SYMBOL_GPL(proc_mkdir_data);
397 
398 struct proc_dir_entry *proc_mkdir_mode(const char *name, umode_t mode,
399                                        struct proc_dir_entry *parent)
400 {
401         return proc_mkdir_data(name, mode, parent, NULL);
402 }
403 EXPORT_SYMBOL(proc_mkdir_mode);
404 
405 struct proc_dir_entry *proc_mkdir(const char *name,
406                 struct proc_dir_entry *parent)
407 {
408         return proc_mkdir_data(name, 0, parent, NULL);
409 }
410 EXPORT_SYMBOL(proc_mkdir);
411 
412 struct proc_dir_entry *proc_create_data(const char *name, umode_t mode,
413                                         struct proc_dir_entry *parent,
414                                         const struct file_operations *proc_fops,
415                                         void *data)
416 {
417         struct proc_dir_entry *pde;
418         if ((mode & S_IFMT) == 0)
419                 mode |= S_IFREG;
420 
421         if (!S_ISREG(mode)) {
422                 WARN_ON(1);     /* use proc_mkdir() */
423                 return NULL;
424         }
425 
426         if ((mode & S_IALLUGO) == 0)
427                 mode |= S_IRUGO;
428         pde = __proc_create(&parent, name, mode, 1);
429         if (!pde)
430                 goto out;
431         pde->proc_fops = proc_fops;
432         pde->data = data;
433         if (proc_register(parent, pde) < 0)
434                 goto out_free;
435         return pde;
436 out_free:
437         kfree(pde);
438 out:
439         return NULL;
440 }
441 EXPORT_SYMBOL(proc_create_data);
442  
443 void proc_set_size(struct proc_dir_entry *de, loff_t size)
444 {
445         de->size = size;
446 }
447 EXPORT_SYMBOL(proc_set_size);
448 
449 void proc_set_user(struct proc_dir_entry *de, kuid_t uid, kgid_t gid)
450 {
451         de->uid = uid;
452         de->gid = gid;
453 }
454 EXPORT_SYMBOL(proc_set_user);
455 
456 static void free_proc_entry(struct proc_dir_entry *de)
457 {
458         proc_free_inum(de->low_ino);
459 
460         if (S_ISLNK(de->mode))
461                 kfree(de->data);
462         kfree(de);
463 }
464 
465 void pde_put(struct proc_dir_entry *pde)
466 {
467         if (atomic_dec_and_test(&pde->count))
468                 free_proc_entry(pde);
469 }
470 
471 /*
472  * Remove a /proc entry and free it if it's not currently in use.
473  */
474 void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
475 {
476         struct proc_dir_entry **p;
477         struct proc_dir_entry *de = NULL;
478         const char *fn = name;
479         unsigned int len;
480 
481         spin_lock(&proc_subdir_lock);
482         if (__xlate_proc_name(name, &parent, &fn) != 0) {
483                 spin_unlock(&proc_subdir_lock);
484                 return;
485         }
486         len = strlen(fn);
487 
488         for (p = &parent->subdir; *p; p=&(*p)->next ) {
489                 if (proc_match(len, fn, *p)) {
490                         de = *p;
491                         *p = de->next;
492                         de->next = NULL;
493                         break;
494                 }
495         }
496         spin_unlock(&proc_subdir_lock);
497         if (!de) {
498                 WARN(1, "name '%s'\n", name);
499                 return;
500         }
501 
502         proc_entry_rundown(de);
503 
504         if (S_ISDIR(de->mode))
505                 parent->nlink--;
506         de->nlink = 0;
507         WARN(de->subdir, "%s: removing non-empty directory "
508                          "'%s/%s', leaking at least '%s'\n", __func__,
509                          de->parent->name, de->name, de->subdir->name);
510         pde_put(de);
511 }
512 EXPORT_SYMBOL(remove_proc_entry);
513 
514 int remove_proc_subtree(const char *name, struct proc_dir_entry *parent)
515 {
516         struct proc_dir_entry **p;
517         struct proc_dir_entry *root = NULL, *de, *next;
518         const char *fn = name;
519         unsigned int len;
520 
521         spin_lock(&proc_subdir_lock);
522         if (__xlate_proc_name(name, &parent, &fn) != 0) {
523                 spin_unlock(&proc_subdir_lock);
524                 return -ENOENT;
525         }
526         len = strlen(fn);
527 
528         for (p = &parent->subdir; *p; p=&(*p)->next ) {
529                 if (proc_match(len, fn, *p)) {
530                         root = *p;
531                         *p = root->next;
532                         root->next = NULL;
533                         break;
534                 }
535         }
536         if (!root) {
537                 spin_unlock(&proc_subdir_lock);
538                 return -ENOENT;
539         }
540         de = root;
541         while (1) {
542                 next = de->subdir;
543                 if (next) {
544                         de->subdir = next->next;
545                         next->next = NULL;
546                         de = next;
547                         continue;
548                 }
549                 spin_unlock(&proc_subdir_lock);
550 
551                 proc_entry_rundown(de);
552                 next = de->parent;
553                 if (S_ISDIR(de->mode))
554                         next->nlink--;
555                 de->nlink = 0;
556                 if (de == root)
557                         break;
558                 pde_put(de);
559 
560                 spin_lock(&proc_subdir_lock);
561                 de = next;
562         }
563         pde_put(root);
564         return 0;
565 }
566 EXPORT_SYMBOL(remove_proc_subtree);
567 
568 void *proc_get_parent_data(const struct inode *inode)
569 {
570         struct proc_dir_entry *de = PDE(inode);
571         return de->parent->data;
572 }
573 EXPORT_SYMBOL_GPL(proc_get_parent_data);
574 
575 void proc_remove(struct proc_dir_entry *de)
576 {
577         if (de)
578                 remove_proc_subtree(de->name, de->parent);
579 }
580 EXPORT_SYMBOL(proc_remove);
581 
582 void *PDE_DATA(const struct inode *inode)
583 {
584         return __PDE_DATA(inode);
585 }
586 EXPORT_SYMBOL(PDE_DATA);
587 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp