LCOV - code coverage report
Current view: top level - fs/xfs - xfs_symlink.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc3-achx @ Mon Jul 31 20:08:12 PDT 2023 Lines: 143 163 87.7 %
Date: 2023-07-31 20:08:12 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   280178742 : xfs_readlink(
      34             :         struct xfs_inode        *ip,
      35             :         char                    *link)
      36             : {
      37   280178742 :         struct xfs_mount        *mp = ip->i_mount;
      38   280178742 :         xfs_fsize_t             pathlen;
      39   280178742 :         int                     error;
      40             : 
      41   280178742 :         trace_xfs_readlink(ip);
      42             : 
      43   559968524 :         if (xfs_is_shutdown(mp))
      44             :                 return -EIO;
      45             : 
      46   279984231 :         xfs_ilock(ip, XFS_ILOCK_SHARED);
      47             : 
      48   280172639 :         pathlen = ip->i_disk_size;
      49   280172639 :         if (!pathlen)
      50           0 :                 goto out_corrupt;
      51             : 
      52   280172639 :         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   280172639 :         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    53474916 :                 if (XFS_IS_CORRUPT(ip->i_mount, !ip->i_df.if_u1.if_data))
      66           0 :                         goto out_corrupt;
      67             : 
      68   106949832 :                 memcpy(link, ip->i_df.if_u1.if_data, pathlen + 1);
      69    53474916 :                 error = 0;
      70             :         } else {
      71   226697723 :                 error = xfs_symlink_remote_read(ip, link);
      72             :         }
      73             : 
      74   280324278 :         xfs_iunlock(ip, XFS_ILOCK_SHARED);
      75   280324278 :         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   597460430 : 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   597460430 :         struct xfs_mount        *mp = dp->i_mount;
      92   597460430 :         struct xfs_trans        *tp = NULL;
      93   597460430 :         struct xfs_inode        *ip = NULL;
      94   597460430 :         int                     error = 0;
      95   597460430 :         int                     pathlen;
      96   597460430 :         bool                    unlock_dp_on_error = false;
      97   597460430 :         xfs_filblks_t           fs_blocks;
      98   597460430 :         prid_t                  prid;
      99   597460430 :         struct xfs_dquot        *udqp = NULL;
     100   597460430 :         struct xfs_dquot        *gdqp = NULL;
     101   597460430 :         struct xfs_dquot        *pdqp = NULL;
     102   597460430 :         uint                    resblks;
     103   597460430 :         xfs_ino_t               ino;
     104   597460430 :         struct xfs_parent_defer *parent;
     105             : 
     106   597460430 :         *ipp = NULL;
     107             : 
     108   597460430 :         trace_xfs_symlink(dp, link_name);
     109             : 
     110  1194300266 :         if (xfs_is_shutdown(mp))
     111             :                 return -EIO;
     112             : 
     113             :         /*
     114             :          * Check component lengths of the target path name.
     115             :          */
     116   597150132 :         pathlen = strlen(target_path);
     117   597150132 :         if (pathlen >= XFS_SYMLINK_MAXLEN)      /* total string too long */
     118             :                 return -ENAMETOOLONG;
     119    29875172 :         ASSERT(pathlen > 0);
     120             : 
     121    29875172 :         prid = xfs_get_initial_prid(dp);
     122             : 
     123             :         /*
     124             :          * Make sure that we have allocated dquot(s) on disk.
     125             :          */
     126    29875172 :         error = xfs_qm_vop_dqalloc(dp, mapped_fsuid(idmap, &init_user_ns),
     127             :                         mapped_fsgid(idmap, &init_user_ns), prid,
     128             :                         XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
     129             :                         &udqp, &gdqp, &pdqp);
     130    30539278 :         if (error)
     131             :                 return error;
     132             : 
     133             :         /*
     134             :          * The symlink will fit into the inode data fork?
     135             :          * If there are no parent pointers, then there wont't be any attributes.
     136             :          * So we get the whole variable part, and do not need to reserve extra
     137             :          * blocks.  Otherwise, we need to reserve the blocks.
     138             :          */
     139    30539277 :         if (pathlen <= XFS_LITINO(mp) && !xfs_has_parent(mp))
     140             :                 fs_blocks = 0;
     141             :         else
     142    30374264 :                 fs_blocks = xfs_symlink_blocks(mp, pathlen);
     143    30534572 :         resblks = xfs_symlink_space_res(mp, link_name->len, fs_blocks);
     144             : 
     145    30521199 :         error = xfs_parent_start(mp, &parent);
     146    30520334 :         if (error)
     147           0 :                 goto out_release_dquots;
     148             : 
     149    30520334 :         error = xfs_trans_alloc_icreate(mp, &M_RES(mp)->tr_symlink, udqp, gdqp,
     150             :                         pdqp, resblks, &tp);
     151    30531208 :         if (error)
     152       30969 :                 goto out_parent;
     153             : 
     154    30500239 :         xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
     155    30498178 :         unlock_dp_on_error = true;
     156             : 
     157             :         /*
     158             :          * Check whether the directory allows new symlinks or not.
     159             :          */
     160    30498178 :         if (dp->i_diflags & XFS_DIFLAG_NOSYMLINKS) {
     161          11 :                 error = -EPERM;
     162          11 :                 goto out_trans_cancel;
     163             :         }
     164             : 
     165             :         /*
     166             :          * Allocate an inode for the symlink.
     167             :          */
     168    30498167 :         error = xfs_dialloc(&tp, dp->i_ino, S_IFLNK, &ino);
     169    30500040 :         if (!error)
     170    30509599 :                 error = xfs_init_new_inode(idmap, tp, dp, ino,
     171    30509599 :                                 S_IFLNK | (mode & ~S_IFMT), 1, 0, prid,
     172             :                                 xfs_has_parent(mp), &ip);
     173    30482488 :         if (error)
     174          29 :                 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    30482459 :         xfs_trans_ijoin(tp, dp, 0);
     184             : 
     185             :         /*
     186             :          * Also attach the dquot(s) to it, if applicable.
     187             :          */
     188    30505040 :         xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
     189             : 
     190    30486315 :         resblks -= XFS_IALLOC_SPACE_RES(mp);
     191    30486315 :         error = xfs_symlink_write_target(tp, ip, target_path, pathlen,
     192             :                         fs_blocks, resblks);
     193    30464109 :         if (error)
     194           7 :                 goto out_trans_cancel;
     195    30464102 :         resblks -= fs_blocks;
     196    30464102 :         i_size_write(VFS_I(ip), ip->i_disk_size);
     197             : 
     198             :         /*
     199             :          * Create the directory entry for the symlink.
     200             :          */
     201    30464102 :         error = xfs_dir_createname(tp, dp, link_name, ip->i_ino, resblks);
     202    30488363 :         if (error)
     203           2 :                 goto out_trans_cancel;
     204    30488361 :         xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
     205    30470257 :         xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
     206             : 
     207    30503995 :         if (parent) {
     208    30024676 :                 error = xfs_parent_add(tp, parent, dp, link_name, ip);
     209    29991292 :                 if (error)
     210           0 :                         goto out_trans_cancel;
     211             :         }
     212             : 
     213    30470611 :         xfs_dir_update_hook(dp, ip, 1, link_name);
     214             : 
     215             :         /*
     216             :          * If this is a synchronous mount, make sure that the
     217             :          * symlink transaction goes to disk before returning to
     218             :          * the user.
     219             :          */
     220    30456607 :         if (xfs_has_wsync(mp) || xfs_has_dirsync(mp))
     221           0 :                 xfs_trans_set_sync(tp);
     222             : 
     223    30456607 :         error = xfs_trans_commit(tp);
     224    30459052 :         if (error)
     225          11 :                 goto out_release_inode;
     226             : 
     227    30459041 :         xfs_qm_dqrele(udqp);
     228    30425227 :         xfs_qm_dqrele(gdqp);
     229    30439047 :         xfs_qm_dqrele(pdqp);
     230             : 
     231    30451531 :         *ipp = ip;
     232    30451531 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
     233    30431383 :         xfs_iunlock(dp, XFS_ILOCK_EXCL);
     234    30475463 :         xfs_parent_finish(mp, parent);
     235             :         return 0;
     236             : 
     237          49 : out_trans_cancel:
     238          49 :         xfs_trans_cancel(tp);
     239          60 : out_release_inode:
     240             :         /*
     241             :          * Wait until after the current transaction is aborted to finish the
     242             :          * setup of the inode and release the inode.  This prevents recursive
     243             :          * transactions and deadlocks from xfs_inactive.
     244             :          */
     245          60 :         if (ip) {
     246          20 :                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
     247          20 :                 xfs_finish_inode_setup(ip);
     248          20 :                 xfs_irele(ip);
     249             :         }
     250          40 : out_parent:
     251       31029 :         xfs_parent_finish(mp, parent);
     252       31029 : out_release_dquots:
     253       31029 :         xfs_qm_dqrele(udqp);
     254       31030 :         xfs_qm_dqrele(gdqp);
     255       31030 :         xfs_qm_dqrele(pdqp);
     256             : 
     257       31030 :         if (unlock_dp_on_error)
     258          60 :                 xfs_iunlock(dp, XFS_ILOCK_EXCL);
     259             :         return error;
     260             : }
     261             : 
     262             : /*
     263             :  * Free a symlink that has blocks associated with it.
     264             :  *
     265             :  * Note: zero length symlinks are not allowed to exist. When we set the size to
     266             :  * zero, also change it to a regular file so that it does not get written to
     267             :  * disk as a zero length symlink. The inode is on the unlinked list already, so
     268             :  * userspace cannot find this inode anymore, so this change is not user visible
     269             :  * but allows us to catch corrupt zero-length symlinks in the verifiers.
     270             :  */
     271             : STATIC int
     272    23026196 : xfs_inactive_symlink_rmt(
     273             :         struct xfs_inode        *ip)
     274             : {
     275    23026196 :         struct xfs_mount        *mp = ip->i_mount;
     276    23026196 :         struct xfs_trans        *tp;
     277    23026196 :         int                     error;
     278             : 
     279    23026196 :         ASSERT(!xfs_need_iread_extents(&ip->i_df));
     280             :         /*
     281             :          * We're freeing a symlink that has some
     282             :          * blocks allocated to it.  Free the
     283             :          * blocks here.  We know that we've got
     284             :          * either 1 or 2 extents and that we can
     285             :          * free them all in one bunmapi call.
     286             :          */
     287    23025636 :         ASSERT(ip->i_df.if_nextents > 0 && ip->i_df.if_nextents <= 2);
     288             : 
     289    23025636 :         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
     290    23023700 :         if (error)
     291             :                 return error;
     292             : 
     293    23024960 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
     294    23024501 :         xfs_trans_ijoin(tp, ip, 0);
     295             : 
     296             :         /*
     297             :          * Lock the inode, fix the size, turn it into a regular file and join it
     298             :          * to the transaction.  Hold it so in the normal path, we still have it
     299             :          * locked for the second transaction.  In the error paths we need it
     300             :          * held so the cancel won't rele it, see below.
     301             :          */
     302    23030651 :         ip->i_disk_size = 0;
     303    23030651 :         VFS_I(ip)->i_mode = (VFS_I(ip)->i_mode & ~S_IFMT) | S_IFREG;
     304    23030651 :         xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
     305             : 
     306    23039222 :         error = xfs_symlink_remote_truncate(tp, ip);
     307    23040672 :         if (error)
     308           0 :                 goto error_trans_cancel;
     309             : 
     310    23040672 :         error = xfs_trans_commit(tp);
     311    23042561 :         if (error) {
     312          16 :                 ASSERT(xfs_is_shutdown(mp));
     313           8 :                 goto error_unlock;
     314             :         }
     315             : 
     316             :         /*
     317             :          * Remove the memory for extent descriptions (just bookkeeping).
     318             :          */
     319    23042553 :         if (ip->i_df.if_bytes)
     320           0 :                 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
     321    23042553 :         ASSERT(ip->i_df.if_bytes == 0);
     322             : 
     323    23042553 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
     324    23042553 :         return 0;
     325             : 
     326             : error_trans_cancel:
     327           0 :         xfs_trans_cancel(tp);
     328           8 : error_unlock:
     329           8 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
     330           8 :         return error;
     331             : }
     332             : 
     333             : /*
     334             :  * xfs_inactive_symlink - free a symlink
     335             :  */
     336             : int
     337    29873676 : xfs_inactive_symlink(
     338             :         struct xfs_inode        *ip)
     339             : {
     340    29873676 :         struct xfs_mount        *mp = ip->i_mount;
     341    29873676 :         int                     pathlen;
     342             : 
     343    29873676 :         trace_xfs_inactive_symlink(ip);
     344             : 
     345    59702908 :         if (xfs_is_shutdown(mp))
     346             :                 return -EIO;
     347             : 
     348    29851445 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
     349    29892617 :         pathlen = (int)ip->i_disk_size;
     350    29892617 :         ASSERT(pathlen);
     351             : 
     352    29892617 :         if (pathlen <= 0 || pathlen > XFS_SYMLINK_MAXLEN) {
     353           0 :                 xfs_alert(mp, "%s: inode (0x%llx) bad symlink length (%d)",
     354             :                          __func__, (unsigned long long)ip->i_ino, pathlen);
     355           0 :                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
     356           0 :                 ASSERT(0);
     357           0 :                 xfs_inode_mark_sick(ip, XFS_SICK_INO_SYMLINK);
     358           0 :                 return -EFSCORRUPTED;
     359             :         }
     360             : 
     361             :         /*
     362             :          * Inline fork state gets removed by xfs_difree() so we have nothing to
     363             :          * do here in that case.
     364             :          */
     365    29892617 :         if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
     366     6866182 :                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
     367     6866182 :                 return 0;
     368             :         }
     369             : 
     370    23026435 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
     371             : 
     372             :         /* remove the remote symlink */
     373    23030424 :         return xfs_inactive_symlink_rmt(ip);
     374             : }

Generated by: LCOV version 1.14