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

TOMOYO Linux Cross Reference
Linux/fs/kernfs/symlink.c

Version: ~ [ linux-5.5 ] ~ [ linux-5.4.15 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.98 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.167 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.211 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.211 ] ~ [ 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.81 ] ~ [ 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-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ 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  * fs/kernfs/symlink.c - kernfs symlink implementation
  3  *
  4  * Copyright (c) 2001-3 Patrick Mochel
  5  * Copyright (c) 2007 SUSE Linux Products GmbH
  6  * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
  7  *
  8  * This file is released under the GPLv2.
  9  */
 10 
 11 #include <linux/fs.h>
 12 #include <linux/gfp.h>
 13 #include <linux/namei.h>
 14 
 15 #include "kernfs-internal.h"
 16 
 17 /**
 18  * kernfs_create_link - create a symlink
 19  * @parent: directory to create the symlink in
 20  * @name: name of the symlink
 21  * @target: target node for the symlink to point to
 22  *
 23  * Returns the created node on success, ERR_PTR() value on error.
 24  */
 25 struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
 26                                        const char *name,
 27                                        struct kernfs_node *target)
 28 {
 29         struct kernfs_node *kn;
 30         int error;
 31 
 32         kn = kernfs_new_node(parent, name, S_IFLNK|S_IRWXUGO, KERNFS_LINK);
 33         if (!kn)
 34                 return ERR_PTR(-ENOMEM);
 35 
 36         if (kernfs_ns_enabled(parent))
 37                 kn->ns = target->ns;
 38         kn->symlink.target_kn = target;
 39         kernfs_get(target);     /* ref owned by symlink */
 40 
 41         error = kernfs_add_one(kn);
 42         if (!error)
 43                 return kn;
 44 
 45         kernfs_put(kn);
 46         return ERR_PTR(error);
 47 }
 48 
 49 static int kernfs_get_target_path(struct kernfs_node *parent,
 50                                   struct kernfs_node *target, char *path)
 51 {
 52         struct kernfs_node *base, *kn;
 53         char *s = path;
 54         int len = 0;
 55 
 56         /* go up to the root, stop at the base */
 57         base = parent;
 58         while (base->parent) {
 59                 kn = target->parent;
 60                 while (kn->parent && base != kn)
 61                         kn = kn->parent;
 62 
 63                 if (base == kn)
 64                         break;
 65 
 66                 strcpy(s, "../");
 67                 s += 3;
 68                 base = base->parent;
 69         }
 70 
 71         /* determine end of target string for reverse fillup */
 72         kn = target;
 73         while (kn->parent && kn != base) {
 74                 len += strlen(kn->name) + 1;
 75                 kn = kn->parent;
 76         }
 77 
 78         /* check limits */
 79         if (len < 2)
 80                 return -EINVAL;
 81         len--;
 82         if ((s - path) + len > PATH_MAX)
 83                 return -ENAMETOOLONG;
 84 
 85         /* reverse fillup of target string from target to base */
 86         kn = target;
 87         while (kn->parent && kn != base) {
 88                 int slen = strlen(kn->name);
 89 
 90                 len -= slen;
 91                 strncpy(s + len, kn->name, slen);
 92                 if (len)
 93                         s[--len] = '/';
 94 
 95                 kn = kn->parent;
 96         }
 97 
 98         return 0;
 99 }
100 
101 static int kernfs_getlink(struct dentry *dentry, char *path)
102 {
103         struct kernfs_node *kn = dentry->d_fsdata;
104         struct kernfs_node *parent = kn->parent;
105         struct kernfs_node *target = kn->symlink.target_kn;
106         int error;
107 
108         mutex_lock(&kernfs_mutex);
109         error = kernfs_get_target_path(parent, target, path);
110         mutex_unlock(&kernfs_mutex);
111 
112         return error;
113 }
114 
115 static void *kernfs_iop_follow_link(struct dentry *dentry, struct nameidata *nd)
116 {
117         int error = -ENOMEM;
118         unsigned long page = get_zeroed_page(GFP_KERNEL);
119         if (page) {
120                 error = kernfs_getlink(dentry, (char *) page);
121                 if (error < 0)
122                         free_page((unsigned long)page);
123         }
124         nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
125         return NULL;
126 }
127 
128 static void kernfs_iop_put_link(struct dentry *dentry, struct nameidata *nd,
129                                 void *cookie)
130 {
131         char *page = nd_get_link(nd);
132         if (!IS_ERR(page))
133                 free_page((unsigned long)page);
134 }
135 
136 const struct inode_operations kernfs_symlink_iops = {
137         .setxattr       = kernfs_iop_setxattr,
138         .removexattr    = kernfs_iop_removexattr,
139         .getxattr       = kernfs_iop_getxattr,
140         .listxattr      = kernfs_iop_listxattr,
141         .readlink       = generic_readlink,
142         .follow_link    = kernfs_iop_follow_link,
143         .put_link       = kernfs_iop_put_link,
144         .setattr        = kernfs_iop_setattr,
145         .getattr        = kernfs_iop_getattr,
146         .permission     = kernfs_iop_permission,
147 };
148 

~ [ 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