LCOV - code coverage report
Current view: top level - fs/xfs - xfs_symlink.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc4-xfsx @ Mon Jul 31 20:08:34 PDT 2023 Lines: 135 154 87.7 %
Date: 2023-07-31 20:08:34 Functions: 4 4 100.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0
       2             : /*
       3             :  * Copyright (c) 2000-2006 Silicon Graphics, Inc.
       4             :  * Copyright (c) 2012-2013 Red Hat, Inc.
       5             :  * All rights reserved.
       6             :  */
       7             : #include "xfs.h"
       8             : #include "xfs_shared.h"
       9             : #include "xfs_fs.h"
      10             : #include "xfs_format.h"
      11             : #include "xfs_log_format.h"
      12             : #include "xfs_trans_resv.h"
      13             : #include "xfs_bit.h"
      14             : #include "xfs_mount.h"
      15             : #include "xfs_dir2.h"
      16             : #include "xfs_inode.h"
      17             : #include "xfs_bmap.h"
      18             : #include "xfs_bmap_btree.h"
      19             : #include "xfs_quota.h"
      20             : #include "xfs_symlink.h"
      21             : #include "xfs_trans_space.h"
      22             : #include "xfs_trace.h"
      23             : #include "xfs_trans.h"
      24             : #include "xfs_ialloc.h"
      25             : #include "xfs_error.h"
      26             : #include "xfs_health.h"
      27             : #include "xfs_symlink_remote.h"
      28             : #include "xfs_parent.h"
      29             : #include "xfs_defer.h"
      30             : 
      31             : /* ----- Kernel only functions below ----- */
      32             : int
      33   289357088 : xfs_readlink(
      34             :         struct xfs_inode        *ip,
      35             :         char                    *link)
      36             : {
      37   289357088 :         struct xfs_mount        *mp = ip->i_mount;
      38   289357088 :         xfs_fsize_t             pathlen;
      39   289357088 :         int                     error;
      40             : 
      41   289357088 :         trace_xfs_readlink(ip);
      42             : 
      43   578364454 :         if (xfs_is_shutdown(mp))
      44             :                 return -EIO;
      45             : 
      46   289182189 :         xfs_ilock(ip, XFS_ILOCK_SHARED);
      47             : 
      48   289497216 :         pathlen = ip->i_disk_size;
      49   289497216 :         if (!pathlen)
      50           0 :                 goto out_corrupt;
      51             : 
      52   289497216 :         if (pathlen < 0 || pathlen > XFS_SYMLINK_MAXLEN) {
      53           0 :                 xfs_alert(mp, "%s: inode (%llu) bad symlink length (%lld)",
      54             :                          __func__, (unsigned long long) ip->i_ino,
      55             :                          (long long) pathlen);
      56           0 :                 ASSERT(0);
      57           0 :                 goto out_corrupt;
      58             :         }
      59             : 
      60   289497216 :         if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
      61             :                 /*
      62             :                  * The VFS crashes on a NULL pointer, so return -EFSCORRUPTED
      63             :                  * if if_data is junk.
      64             :                  */
      65    55218453 :                 if (XFS_IS_CORRUPT(ip->i_mount, !ip->i_df.if_u1.if_data))
      66           0 :                         goto out_corrupt;
      67             : 
      68   110436906 :                 memcpy(link, ip->i_df.if_u1.if_data, pathlen + 1);
      69    55218453 :                 error = 0;
      70             :         } else {
      71   234278763 :                 error = xfs_symlink_remote_read(ip, link);
      72             :         }
      73             : 
      74   289521817 :         xfs_iunlock(ip, XFS_ILOCK_SHARED);
      75   289521817 :         return error;
      76           0 :  out_corrupt:
      77           0 :         xfs_iunlock(ip, XFS_ILOCK_SHARED);
      78           0 :         xfs_inode_mark_sick(ip, XFS_SICK_INO_SYMLINK);
      79           0 :         return -EFSCORRUPTED;
      80             : }
      81             : 
      82             : int
      83   639640233 : xfs_symlink(
      84             :         struct mnt_idmap        *idmap,
      85             :         struct xfs_inode        *dp,
      86             :         struct xfs_name         *link_name,
      87             :         const char              *target_path,
      88             :         umode_t                 mode,
      89             :         struct xfs_inode        **ipp)
      90             : {
      91   639640233 :         struct xfs_icreate_args args = {
      92             :                 .nlink          = 1,
      93             :         };
      94   639640233 :         struct xfs_dir_update   du = {
      95             :                 .dp             = dp,
      96             :                 .name           = link_name,
      97             :         };
      98   639640233 :         struct xfs_mount        *mp = dp->i_mount;
      99   639640233 :         struct xfs_trans        *tp = NULL;
     100   639640233 :         int                     error = 0;
     101   639640233 :         int                     pathlen;
     102   639640233 :         bool                    unlock_dp_on_error = false;
     103   639640233 :         xfs_filblks_t           fs_blocks;
     104   639640233 :         struct xfs_dquot        *udqp;
     105   639640233 :         struct xfs_dquot        *gdqp;
     106   639640233 :         struct xfs_dquot        *pdqp;
     107   639640233 :         uint                    resblks;
     108   639640233 :         xfs_ino_t               ino;
     109             : 
     110   639640233 :         *ipp = NULL;
     111             : 
     112   639640233 :         trace_xfs_symlink(dp, link_name);
     113             : 
     114  1278377570 :         if (xfs_is_shutdown(mp))
     115             :                 return -EIO;
     116             : 
     117             :         /*
     118             :          * Check component lengths of the target path name.
     119             :          */
     120   639188784 :         pathlen = strlen(target_path);
     121   639188784 :         if (pathlen >= XFS_SYMLINK_MAXLEN)      /* total string too long */
     122             :                 return -ENAMETOOLONG;
     123    30805689 :         ASSERT(pathlen > 0);
     124             : 
     125    30805689 :         xfs_icreate_args_inherit(&args, dp, idmap, S_IFLNK | (mode & ~S_IFMT),
     126             :                         xfs_has_parent(mp));
     127             : 
     128             :         /*
     129             :          * Make sure that we have allocated dquot(s) on disk.
     130             :          */
     131    31744731 :         error = xfs_icreate_dqalloc(&args, &udqp, &gdqp, &pdqp);
     132    31741149 :         if (error)
     133             :                 return error;
     134             : 
     135             :         /*
     136             :          * The symlink will fit into the inode data fork?
     137             :          * If there are no parent pointers, then there wont't be any attributes.
     138             :          * So we get the whole variable part, and do not need to reserve extra
     139             :          * blocks.  Otherwise, we need to reserve the blocks.
     140             :          */
     141    31741147 :         if (pathlen <= XFS_LITINO(mp) && !xfs_has_parent(mp))
     142             :                 fs_blocks = 0;
     143             :         else
     144    31555093 :                 fs_blocks = xfs_symlink_blocks(mp, pathlen);
     145    31728541 :         resblks = xfs_symlink_space_res(mp, link_name->len, fs_blocks);
     146             : 
     147    31715401 :         error = xfs_parent_start(mp, &du.parent);
     148    31715685 :         if (error)
     149           0 :                 goto out_release_dquots;
     150             : 
     151    31715685 :         error = xfs_trans_alloc_icreate(mp, &M_RES(mp)->tr_symlink, udqp, gdqp,
     152             :                         pdqp, resblks, &tp);
     153    31748187 :         if (error)
     154       42221 :                 goto out_parent;
     155             : 
     156    31705966 :         xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
     157    31704490 :         unlock_dp_on_error = true;
     158             : 
     159             :         /*
     160             :          * Check whether the directory allows new symlinks or not.
     161             :          */
     162    31704490 :         if (dp->i_diflags & XFS_DIFLAG_NOSYMLINKS) {
     163          11 :                 error = -EPERM;
     164          11 :                 goto out_trans_cancel;
     165             :         }
     166             : 
     167             :         /*
     168             :          * Allocate an inode for the symlink.
     169             :          */
     170    31704479 :         error = xfs_dialloc(&tp, dp, S_IFLNK, &ino);
     171    31699712 :         if (!error)
     172    31700062 :                 error = xfs_icreate(tp, ino, &args, &du.ip);
     173    31688886 :         if (error)
     174           9 :                 goto out_trans_cancel;
     175             : 
     176             :         /*
     177             :          * Now we join the directory inode to the transaction.  We do not do it
     178             :          * earlier because xfs_dir_ialloc might commit the previous transaction
     179             :          * (and release all the locks).  An error from here on will result in
     180             :          * the transaction cancel unlocking dp so don't do it explicitly in the
     181             :          * error path.
     182             :          */
     183    31688877 :         xfs_trans_ijoin(tp, dp, 0);
     184             : 
     185             :         /*
     186             :          * Also attach the dquot(s) to it, if applicable.
     187             :          */
     188    31683972 :         xfs_qm_vop_create_dqattach(tp, du.ip, udqp, gdqp, pdqp);
     189             : 
     190    31653328 :         resblks -= XFS_IALLOC_SPACE_RES(mp);
     191    31653328 :         error = xfs_symlink_write_target(tp, du.ip, target_path, pathlen,
     192             :                         fs_blocks, resblks);
     193    31645669 :         if (error)
     194           9 :                 goto out_trans_cancel;
     195    31645660 :         resblks -= fs_blocks;
     196    31645660 :         i_size_write(VFS_I(du.ip), du.ip->i_disk_size);
     197             : 
     198             :         /*
     199             :          * Create the directory entry for the symlink.
     200             :          */
     201    31645660 :         error = xfs_dir_create_child(tp, resblks, &du);
     202    31650270 :         if (error)
     203           2 :                 goto out_trans_cancel;
     204             : 
     205             :         /*
     206             :          * If this is a synchronous mount, make sure that the
     207             :          * symlink transaction goes to disk before returning to
     208             :          * the user.
     209             :          */
     210    31650268 :         if (xfs_has_wsync(mp) || xfs_has_dirsync(mp))
     211           0 :                 xfs_trans_set_sync(tp);
     212             : 
     213    31650268 :         error = xfs_trans_commit(tp);
     214    31708455 :         if (error)
     215          13 :                 goto out_release_inode;
     216             : 
     217    31708442 :         xfs_qm_dqrele(udqp);
     218    31707941 :         xfs_qm_dqrele(gdqp);
     219    31706142 :         xfs_qm_dqrele(pdqp);
     220             : 
     221    31706636 :         *ipp = du.ip;
     222    31706636 :         xfs_iunlock(du.ip, XFS_ILOCK_EXCL);
     223    31681224 :         xfs_iunlock(dp, XFS_ILOCK_EXCL);
     224    31681871 :         xfs_parent_finish(mp, du.parent);
     225             :         return 0;
     226             : 
     227          31 : out_trans_cancel:
     228          31 :         xfs_trans_cancel(tp);
     229          44 : out_release_inode:
     230             :         /*
     231             :          * Wait until after the current transaction is aborted to finish the
     232             :          * setup of the inode and release the inode.  This prevents recursive
     233             :          * transactions and deadlocks from xfs_inactive.
     234             :          */
     235          44 :         if (du.ip) {
     236          24 :                 xfs_iunlock(du.ip, XFS_ILOCK_EXCL);
     237          24 :                 xfs_finish_inode_setup(du.ip);
     238          24 :                 xfs_irele(du.ip);
     239             :         }
     240          20 : out_parent:
     241       42265 :         xfs_parent_finish(mp, du.parent);
     242       42266 : out_release_dquots:
     243       42266 :         xfs_qm_dqrele(udqp);
     244       42266 :         xfs_qm_dqrele(gdqp);
     245       42266 :         xfs_qm_dqrele(pdqp);
     246             : 
     247       42266 :         if (unlock_dp_on_error)
     248          44 :                 xfs_iunlock(dp, XFS_ILOCK_EXCL);
     249             :         return error;
     250             : }
     251             : 
     252             : /*
     253             :  * Free a symlink that has blocks associated with it.
     254             :  *
     255             :  * Note: zero length symlinks are not allowed to exist. When we set the size to
     256             :  * zero, also change it to a regular file so that it does not get written to
     257             :  * disk as a zero length symlink. The inode is on the unlinked list already, so
     258             :  * userspace cannot find this inode anymore, so this change is not user visible
     259             :  * but allows us to catch corrupt zero-length symlinks in the verifiers.
     260             :  */
     261             : STATIC int
     262    23747122 : xfs_inactive_symlink_rmt(
     263             :         struct xfs_inode        *ip)
     264             : {
     265    23747122 :         struct xfs_mount        *mp = ip->i_mount;
     266    23747122 :         struct xfs_trans        *tp;
     267    23747122 :         int                     error;
     268             : 
     269    23747122 :         ASSERT(!xfs_need_iread_extents(&ip->i_df));
     270             :         /*
     271             :          * We're freeing a symlink that has some
     272             :          * blocks allocated to it.  Free the
     273             :          * blocks here.  We know that we've got
     274             :          * either 1 or 2 extents and that we can
     275             :          * free them all in one bunmapi call.
     276             :          */
     277    23744929 :         ASSERT(ip->i_df.if_nextents > 0 && ip->i_df.if_nextents <= 2);
     278             : 
     279    23744929 :         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
     280    23749282 :         if (error)
     281             :                 return error;
     282             : 
     283    23750540 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
     284    23751146 :         xfs_trans_ijoin(tp, ip, 0);
     285             : 
     286             :         /*
     287             :          * Lock the inode, fix the size, turn it into a regular file and join it
     288             :          * to the transaction.  Hold it so in the normal path, we still have it
     289             :          * locked for the second transaction.  In the error paths we need it
     290             :          * held so the cancel won't rele it, see below.
     291             :          */
     292    23753389 :         ip->i_disk_size = 0;
     293    23753389 :         VFS_I(ip)->i_mode = (VFS_I(ip)->i_mode & ~S_IFMT) | S_IFREG;
     294    23753389 :         xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
     295             : 
     296    23769810 :         error = xfs_symlink_remote_truncate(tp, ip);
     297    23764725 :         if (error)
     298           0 :                 goto error_trans_cancel;
     299             : 
     300    23764725 :         error = xfs_trans_commit(tp);
     301    23797824 :         if (error) {
     302          20 :                 ASSERT(xfs_is_shutdown(mp));
     303          10 :                 goto error_unlock;
     304             :         }
     305             : 
     306             :         /*
     307             :          * Remove the memory for extent descriptions (just bookkeeping).
     308             :          */
     309    23797814 :         if (ip->i_df.if_bytes)
     310           0 :                 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
     311    23797814 :         ASSERT(ip->i_df.if_bytes == 0);
     312             : 
     313    23797814 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
     314    23797814 :         return 0;
     315             : 
     316             : error_trans_cancel:
     317           0 :         xfs_trans_cancel(tp);
     318          10 : error_unlock:
     319          10 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
     320          10 :         return error;
     321             : }
     322             : 
     323             : /*
     324             :  * xfs_inactive_symlink - free a symlink
     325             :  */
     326             : int
     327    29491687 : xfs_inactive_symlink(
     328             :         struct xfs_inode        *ip)
     329             : {
     330    29491687 :         struct xfs_mount        *mp = ip->i_mount;
     331    29491687 :         int                     pathlen;
     332             : 
     333    29491687 :         trace_xfs_inactive_symlink(ip);
     334             : 
     335    58887096 :         if (xfs_is_shutdown(mp))
     336             :                 return -EIO;
     337             : 
     338    29443532 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
     339    29518259 :         pathlen = (int)ip->i_disk_size;
     340    29518259 :         ASSERT(pathlen);
     341             : 
     342    29518259 :         if (pathlen <= 0 || pathlen > XFS_SYMLINK_MAXLEN) {
     343           0 :                 xfs_alert(mp, "%s: inode (0x%llx) bad symlink length (%d)",
     344             :                          __func__, (unsigned long long)ip->i_ino, pathlen);
     345           0 :                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
     346           0 :                 ASSERT(0);
     347           0 :                 xfs_inode_mark_sick(ip, XFS_SICK_INO_SYMLINK);
     348           0 :                 return -EFSCORRUPTED;
     349             :         }
     350             : 
     351             :         /*
     352             :          * Inline fork state gets removed by xfs_difree() so we have nothing to
     353             :          * do here in that case.
     354             :          */
     355    29518259 :         if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
     356     5769117 :                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
     357     5769117 :                 return 0;
     358             :         }
     359             : 
     360    23749142 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
     361             : 
     362             :         /* remove the remote symlink */
     363    23761529 :         return xfs_inactive_symlink_rmt(ip);
     364             : }

Generated by: LCOV version 1.14