LCOV - code coverage report
Current view: top level - fs/kernfs - symlink.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 11 60 18.3 %
Date: 2023-04-06 08:38:28 Functions: 1 4 25.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0-only
       2             : /*
       3             :  * fs/kernfs/symlink.c - kernfs symlink implementation
       4             :  *
       5             :  * Copyright (c) 2001-3 Patrick Mochel
       6             :  * Copyright (c) 2007 SUSE Linux Products GmbH
       7             :  * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
       8             :  */
       9             : 
      10             : #include <linux/fs.h>
      11             : #include <linux/gfp.h>
      12             : #include <linux/namei.h>
      13             : 
      14             : #include "kernfs-internal.h"
      15             : 
      16             : /**
      17             :  * kernfs_create_link - create a symlink
      18             :  * @parent: directory to create the symlink in
      19             :  * @name: name of the symlink
      20             :  * @target: target node for the symlink to point to
      21             :  *
      22             :  * Return: the created node on success, ERR_PTR() value on error.
      23             :  * Ownership of the link matches ownership of the target.
      24             :  */
      25        1733 : 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        1733 :         kuid_t uid = GLOBAL_ROOT_UID;
      32        1733 :         kgid_t gid = GLOBAL_ROOT_GID;
      33             : 
      34        1733 :         if (target->iattr) {
      35           0 :                 uid = target->iattr->ia_uid;
      36           0 :                 gid = target->iattr->ia_gid;
      37             :         }
      38             : 
      39        1733 :         kn = kernfs_new_node(parent, name, S_IFLNK|0777, uid, gid, KERNFS_LINK);
      40        1733 :         if (!kn)
      41             :                 return ERR_PTR(-ENOMEM);
      42             : 
      43        1733 :         if (kernfs_ns_enabled(parent))
      44           0 :                 kn->ns = target->ns;
      45        1733 :         kn->symlink.target_kn = target;
      46        1733 :         kernfs_get(target);     /* ref owned by symlink */
      47             : 
      48        1733 :         error = kernfs_add_one(kn);
      49        1733 :         if (!error)
      50             :                 return kn;
      51             : 
      52           0 :         kernfs_put(kn);
      53           0 :         return ERR_PTR(error);
      54             : }
      55             : 
      56           0 : static int kernfs_get_target_path(struct kernfs_node *parent,
      57             :                                   struct kernfs_node *target, char *path)
      58             : {
      59             :         struct kernfs_node *base, *kn;
      60           0 :         char *s = path;
      61           0 :         int len = 0;
      62             : 
      63             :         /* go up to the root, stop at the base */
      64           0 :         base = parent;
      65           0 :         while (base->parent) {
      66           0 :                 kn = target->parent;
      67           0 :                 while (kn->parent && base != kn)
      68             :                         kn = kn->parent;
      69             : 
      70           0 :                 if (base == kn)
      71             :                         break;
      72             : 
      73           0 :                 if ((s - path) + 3 >= PATH_MAX)
      74             :                         return -ENAMETOOLONG;
      75             : 
      76           0 :                 strcpy(s, "../");
      77           0 :                 s += 3;
      78           0 :                 base = base->parent;
      79             :         }
      80             : 
      81             :         /* determine end of target string for reverse fillup */
      82             :         kn = target;
      83           0 :         while (kn->parent && kn != base) {
      84           0 :                 len += strlen(kn->name) + 1;
      85           0 :                 kn = kn->parent;
      86             :         }
      87             : 
      88             :         /* check limits */
      89           0 :         if (len < 2)
      90             :                 return -EINVAL;
      91           0 :         len--;
      92           0 :         if ((s - path) + len >= PATH_MAX)
      93             :                 return -ENAMETOOLONG;
      94             : 
      95             :         /* reverse fillup of target string from target to base */
      96             :         kn = target;
      97           0 :         while (kn->parent && kn != base) {
      98           0 :                 int slen = strlen(kn->name);
      99             : 
     100           0 :                 len -= slen;
     101           0 :                 memcpy(s + len, kn->name, slen);
     102           0 :                 if (len)
     103           0 :                         s[--len] = '/';
     104             : 
     105           0 :                 kn = kn->parent;
     106             :         }
     107             : 
     108             :         return 0;
     109             : }
     110             : 
     111           0 : static int kernfs_getlink(struct inode *inode, char *path)
     112             : {
     113           0 :         struct kernfs_node *kn = inode->i_private;
     114           0 :         struct kernfs_node *parent = kn->parent;
     115           0 :         struct kernfs_node *target = kn->symlink.target_kn;
     116           0 :         struct kernfs_root *root = kernfs_root(parent);
     117             :         int error;
     118             : 
     119           0 :         down_read(&root->kernfs_rwsem);
     120           0 :         error = kernfs_get_target_path(parent, target, path);
     121           0 :         up_read(&root->kernfs_rwsem);
     122             : 
     123           0 :         return error;
     124             : }
     125             : 
     126           0 : static const char *kernfs_iop_get_link(struct dentry *dentry,
     127             :                                        struct inode *inode,
     128             :                                        struct delayed_call *done)
     129             : {
     130             :         char *body;
     131             :         int error;
     132             : 
     133           0 :         if (!dentry)
     134             :                 return ERR_PTR(-ECHILD);
     135           0 :         body = kzalloc(PAGE_SIZE, GFP_KERNEL);
     136           0 :         if (!body)
     137             :                 return ERR_PTR(-ENOMEM);
     138           0 :         error = kernfs_getlink(inode, body);
     139           0 :         if (unlikely(error < 0)) {
     140           0 :                 kfree(body);
     141           0 :                 return ERR_PTR(error);
     142             :         }
     143           0 :         set_delayed_call(done, kfree_link, body);
     144           0 :         return body;
     145             : }
     146             : 
     147             : const struct inode_operations kernfs_symlink_iops = {
     148             :         .listxattr      = kernfs_iop_listxattr,
     149             :         .get_link       = kernfs_iop_get_link,
     150             :         .setattr        = kernfs_iop_setattr,
     151             :         .getattr        = kernfs_iop_getattr,
     152             :         .permission     = kernfs_iop_permission,
     153             : };

Generated by: LCOV version 1.14