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-acha @ Mon Jul 31 20:08:06 PDT 2023 Lines: 142 163 87.1 %
Date: 2023-07-31 20:08:07 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   295549515 : xfs_readlink(
      34             :         struct xfs_inode        *ip,
      35             :         char                    *link)
      36             : {
      37   295549515 :         struct xfs_mount        *mp = ip->i_mount;
      38   295549515 :         xfs_fsize_t             pathlen;
      39   295549515 :         int                     error;
      40             : 
      41   295549515 :         trace_xfs_readlink(ip);
      42             : 
      43   591109654 :         if (xfs_is_shutdown(mp))
      44             :                 return -EIO;
      45             : 
      46   295554798 :         xfs_ilock(ip, XFS_ILOCK_SHARED);
      47             : 
      48   295644114 :         pathlen = ip->i_disk_size;
      49   295644114 :         if (!pathlen)
      50           0 :                 goto out_corrupt;
      51             : 
      52   295644114 :         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   295644114 :         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    55580268 :                 if (XFS_IS_CORRUPT(ip->i_mount, !ip->i_df.if_u1.if_data))
      66           0 :                         goto out_corrupt;
      67             : 
      68   111160536 :                 memcpy(link, ip->i_df.if_u1.if_data, pathlen + 1);
      69    55580268 :                 error = 0;
      70             :         } else {
      71   240063846 :                 error = xfs_symlink_remote_read(ip, link);
      72             :         }
      73             : 
      74   295733136 :         xfs_iunlock(ip, XFS_ILOCK_SHARED);
      75   295733136 :         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   437723703 : 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   437723703 :         struct xfs_mount        *mp = dp->i_mount;
      92   437723703 :         struct xfs_trans        *tp = NULL;
      93   437723703 :         struct xfs_inode        *ip = NULL;
      94   437723703 :         int                     error = 0;
      95   437723703 :         int                     pathlen;
      96   437723703 :         bool                    unlock_dp_on_error = false;
      97   437723703 :         xfs_filblks_t           fs_blocks;
      98   437723703 :         prid_t                  prid;
      99   437723703 :         struct xfs_dquot        *udqp = NULL;
     100   437723703 :         struct xfs_dquot        *gdqp = NULL;
     101   437723703 :         struct xfs_dquot        *pdqp = NULL;
     102   437723703 :         uint                    resblks;
     103   437723703 :         xfs_ino_t               ino;
     104   437723703 :         struct xfs_parent_defer *parent;
     105             : 
     106   437723703 :         *ipp = NULL;
     107             : 
     108   437723703 :         trace_xfs_symlink(dp, link_name);
     109             : 
     110   875500212 :         if (xfs_is_shutdown(mp))
     111             :                 return -EIO;
     112             : 
     113             :         /*
     114             :          * Check component lengths of the target path name.
     115             :          */
     116   437750105 :         pathlen = strlen(target_path);
     117   437750105 :         if (pathlen >= XFS_SYMLINK_MAXLEN)      /* total string too long */
     118             :                 return -ENAMETOOLONG;
     119    30973482 :         ASSERT(pathlen > 0);
     120             : 
     121    30973482 :         prid = xfs_get_initial_prid(dp);
     122             : 
     123             :         /*
     124             :          * Make sure that we have allocated dquot(s) on disk.
     125             :          */
     126    30973482 :         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    30950871 :         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    30950854 :         if (pathlen <= XFS_LITINO(mp) && !xfs_has_parent(mp))
     140             :                 fs_blocks = 0;
     141             :         else
     142    30950719 :                 fs_blocks = xfs_symlink_blocks(mp, pathlen);
     143    30950609 :         resblks = xfs_symlink_space_res(mp, link_name->len, fs_blocks);
     144             : 
     145    30949886 :         error = xfs_parent_start(mp, &parent);
     146    30950298 :         if (error)
     147           0 :                 goto out_release_dquots;
     148             : 
     149    30950298 :         error = xfs_trans_alloc_icreate(mp, &M_RES(mp)->tr_symlink, udqp, gdqp,
     150             :                         pdqp, resblks, &tp);
     151    30950920 :         if (error)
     152       13683 :                 goto out_parent;
     153             : 
     154    30937237 :         xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
     155    30937206 :         unlock_dp_on_error = true;
     156             : 
     157             :         /*
     158             :          * Check whether the directory allows new symlinks or not.
     159             :          */
     160    30937206 :         if (dp->i_diflags & XFS_DIFLAG_NOSYMLINKS) {
     161           2 :                 error = -EPERM;
     162           2 :                 goto out_trans_cancel;
     163             :         }
     164             : 
     165             :         /*
     166             :          * Allocate an inode for the symlink.
     167             :          */
     168    30937204 :         error = xfs_dialloc(&tp, dp->i_ino, S_IFLNK, &ino);
     169    30937284 :         if (!error)
     170    30936651 :                 error = xfs_init_new_inode(idmap, tp, dp, ino,
     171    30936651 :                                 S_IFLNK | (mode & ~S_IFMT), 1, 0, prid,
     172             :                                 xfs_has_parent(mp), &ip);
     173    30936639 :         if (error)
     174           5 :                 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    30936634 :         xfs_trans_ijoin(tp, dp, 0);
     184             : 
     185             :         /*
     186             :          * Also attach the dquot(s) to it, if applicable.
     187             :          */
     188    30936273 :         xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
     189             : 
     190    30936395 :         resblks -= XFS_IALLOC_SPACE_RES(mp);
     191    30936395 :         error = xfs_symlink_write_target(tp, ip, target_path, pathlen,
     192             :                         fs_blocks, resblks);
     193    30933149 :         if (error)
     194           8 :                 goto out_trans_cancel;
     195    30933141 :         resblks -= fs_blocks;
     196    30933141 :         i_size_write(VFS_I(ip), ip->i_disk_size);
     197             : 
     198             :         /*
     199             :          * Create the directory entry for the symlink.
     200             :          */
     201    30933141 :         error = xfs_dir_createname(tp, dp, link_name, ip->i_ino, resblks);
     202    30934582 :         if (error)
     203           0 :                 goto out_trans_cancel;
     204    30934582 :         xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
     205    30933446 :         xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
     206             : 
     207    30936255 :         if (parent) {
     208    30935928 :                 error = xfs_parent_add(tp, parent, dp, link_name, ip);
     209    30934591 :                 if (error)
     210           0 :                         goto out_trans_cancel;
     211             :         }
     212             : 
     213    30934918 :         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    30933302 :         if (xfs_has_wsync(mp) || xfs_has_dirsync(mp))
     221           0 :                 xfs_trans_set_sync(tp);
     222             : 
     223    30933302 :         error = xfs_trans_commit(tp);
     224    30936170 :         if (error)
     225          11 :                 goto out_release_inode;
     226             : 
     227    30936159 :         xfs_qm_dqrele(udqp);
     228    30936919 :         xfs_qm_dqrele(gdqp);
     229    30936516 :         xfs_qm_dqrele(pdqp);
     230             : 
     231    30936661 :         *ipp = ip;
     232    30936661 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
     233    30936343 :         xfs_iunlock(dp, XFS_ILOCK_EXCL);
     234    30936410 :         xfs_parent_finish(mp, parent);
     235             :         return 0;
     236             : 
     237          15 : out_trans_cancel:
     238          15 :         xfs_trans_cancel(tp);
     239          26 : 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          26 :         if (ip) {
     246          19 :                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
     247          19 :                 xfs_finish_inode_setup(ip);
     248          19 :                 xfs_irele(ip);
     249             :         }
     250           7 : out_parent:
     251       13709 :         xfs_parent_finish(mp, parent);
     252       13709 : out_release_dquots:
     253       13709 :         xfs_qm_dqrele(udqp);
     254       13709 :         xfs_qm_dqrele(gdqp);
     255       13709 :         xfs_qm_dqrele(pdqp);
     256             : 
     257       13709 :         if (unlock_dp_on_error)
     258          26 :                 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    24181774 : xfs_inactive_symlink_rmt(
     273             :         struct xfs_inode        *ip)
     274             : {
     275    24181774 :         struct xfs_mount        *mp = ip->i_mount;
     276    24181774 :         struct xfs_trans        *tp;
     277    24181774 :         int                     error;
     278             : 
     279    24181774 :         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    24182442 :         ASSERT(ip->i_df.if_nextents > 0 && ip->i_df.if_nextents <= 2);
     288             : 
     289    24182442 :         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
     290    24183260 :         if (error)
     291             :                 return error;
     292             : 
     293    24183261 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
     294    24183224 :         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    24181409 :         ip->i_disk_size = 0;
     303    24181409 :         VFS_I(ip)->i_mode = (VFS_I(ip)->i_mode & ~S_IFMT) | S_IFREG;
     304    24181409 :         xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
     305             : 
     306    24183030 :         error = xfs_symlink_remote_truncate(tp, ip);
     307    24181524 :         if (error)
     308           0 :                 goto error_trans_cancel;
     309             : 
     310    24181524 :         error = xfs_trans_commit(tp);
     311    24183257 :         if (error) {
     312          14 :                 ASSERT(xfs_is_shutdown(mp));
     313           7 :                 goto error_unlock;
     314             :         }
     315             : 
     316             :         /*
     317             :          * Remove the memory for extent descriptions (just bookkeeping).
     318             :          */
     319    24183250 :         if (ip->i_df.if_bytes)
     320           0 :                 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
     321    24183250 :         ASSERT(ip->i_df.if_bytes == 0);
     322             : 
     323    24183250 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
     324    24183250 :         return 0;
     325             : 
     326             : error_trans_cancel:
     327           0 :         xfs_trans_cancel(tp);
     328           7 : error_unlock:
     329           7 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
     330           7 :         return error;
     331             : }
     332             : 
     333             : /*
     334             :  * xfs_inactive_symlink - free a symlink
     335             :  */
     336             : int
     337    30774760 : xfs_inactive_symlink(
     338             :         struct xfs_inode        *ip)
     339             : {
     340    30774760 :         struct xfs_mount        *mp = ip->i_mount;
     341    30774760 :         int                     pathlen;
     342             : 
     343    30774760 :         trace_xfs_inactive_symlink(ip);
     344             : 
     345    61549440 :         if (xfs_is_shutdown(mp))
     346             :                 return -EIO;
     347             : 
     348    30774712 :         xfs_ilock(ip, XFS_ILOCK_EXCL);
     349    30776086 :         pathlen = (int)ip->i_disk_size;
     350    30776086 :         ASSERT(pathlen);
     351             : 
     352    30776086 :         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    30776086 :         if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
     366     6594253 :                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
     367     6594253 :                 return 0;
     368             :         }
     369             : 
     370    24181833 :         xfs_iunlock(ip, XFS_ILOCK_EXCL);
     371             : 
     372             :         /* remove the remote symlink */
     373    24180750 :         return xfs_inactive_symlink_rmt(ip);
     374             : }

Generated by: LCOV version 1.14