2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3 * Copyright (c) 2012-2013 Red Hat, Inc.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "xfs_format.h"
24 #include "xfs_trans.h"
27 #include "xfs_mount.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_dir2_format.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dinode.h"
34 #include "xfs_inode.h"
35 #include "xfs_inode_item.h"
36 #include "xfs_itable.h"
37 #include "xfs_ialloc.h"
38 #include "xfs_alloc.h"
40 #include "xfs_error.h"
41 #include "xfs_quota.h"
42 #include "xfs_utils.h"
43 #include "xfs_trans_space.h"
44 #include "xfs_log_priv.h"
45 #include "xfs_trace.h"
46 #include "xfs_symlink.h"
47 #include "xfs_cksum.h"
48 #include "xfs_buf_item.h"
52 * Each contiguous block has a header, so it is not just a simple pathlen
60 int buflen = XFS_SYMLINK_BUF_SPACE(mp, mp->m_sb.sb_blocksize);
62 return (pathlen + buflen - 1) / buflen;
73 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
75 if (!xfs_sb_version_hascrc(&mp->m_sb))
78 dsl->sl_magic = cpu_to_be32(XFS_SYMLINK_MAGIC);
79 dsl->sl_offset = cpu_to_be32(offset);
80 dsl->sl_bytes = cpu_to_be32(size);
81 uuid_copy(&dsl->sl_uuid, &mp->m_sb.sb_uuid);
82 dsl->sl_owner = cpu_to_be64(ino);
83 dsl->sl_blkno = cpu_to_be64(bp->b_bn);
84 bp->b_ops = &xfs_symlink_buf_ops;
86 return sizeof(struct xfs_dsymlink_hdr);
90 * Checking of the symlink header is split into two parts. the verifier does
91 * CRC, location and bounds checking, the unpacking function checks the path
92 * parameters and owner.
102 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
104 if (offset != be32_to_cpu(dsl->sl_offset))
106 if (size != be32_to_cpu(dsl->sl_bytes))
108 if (ino != be64_to_cpu(dsl->sl_owner))
119 struct xfs_mount *mp = bp->b_target->bt_mount;
120 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
122 if (!xfs_sb_version_hascrc(&mp->m_sb))
124 if (dsl->sl_magic != cpu_to_be32(XFS_SYMLINK_MAGIC))
126 if (!uuid_equal(&dsl->sl_uuid, &mp->m_sb.sb_uuid))
128 if (bp->b_bn != be64_to_cpu(dsl->sl_blkno))
130 if (be32_to_cpu(dsl->sl_offset) +
131 be32_to_cpu(dsl->sl_bytes) >= MAXPATHLEN)
133 if (dsl->sl_owner == 0)
140 xfs_symlink_read_verify(
143 struct xfs_mount *mp = bp->b_target->bt_mount;
145 /* no verification of non-crc buffers */
146 if (!xfs_sb_version_hascrc(&mp->m_sb))
149 if (!xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
150 offsetof(struct xfs_dsymlink_hdr, sl_crc)) ||
151 !xfs_symlink_verify(bp)) {
152 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
153 xfs_buf_ioerror(bp, EFSCORRUPTED);
158 xfs_symlink_write_verify(
161 struct xfs_mount *mp = bp->b_target->bt_mount;
162 struct xfs_buf_log_item *bip = bp->b_fspriv;
164 /* no verification of non-crc buffers */
165 if (!xfs_sb_version_hascrc(&mp->m_sb))
168 if (!xfs_symlink_verify(bp)) {
169 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
170 xfs_buf_ioerror(bp, EFSCORRUPTED);
175 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
176 dsl->sl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
178 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
179 offsetof(struct xfs_dsymlink_hdr, sl_crc));
182 const struct xfs_buf_ops xfs_symlink_buf_ops = {
183 .verify_read = xfs_symlink_read_verify,
184 .verify_write = xfs_symlink_write_verify,
188 xfs_symlink_local_to_remote(
189 struct xfs_trans *tp,
191 struct xfs_inode *ip,
192 struct xfs_ifork *ifp)
194 struct xfs_mount *mp = ip->i_mount;
197 if (!xfs_sb_version_hascrc(&mp->m_sb)) {
199 memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes);
204 * As this symlink fits in an inode literal area, it must also fit in
205 * the smallest buffer the filesystem supports.
207 ASSERT(BBTOB(bp->b_length) >=
208 ifp->if_bytes + sizeof(struct xfs_dsymlink_hdr));
210 bp->b_ops = &xfs_symlink_buf_ops;
213 buf += xfs_symlink_hdr_set(mp, ip->i_ino, 0, ifp->if_bytes, bp);
214 memcpy(buf, ifp->if_u1.if_data, ifp->if_bytes);
217 /* ----- Kernel only functions below ----- */
220 struct xfs_inode *ip,
223 struct xfs_mount *mp = ip->i_mount;
224 struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS];
228 int pathlen = ip->i_d.di_size;
229 int nmaps = XFS_SYMLINK_MAPS;
236 fsblocks = xfs_symlink_blocks(mp, pathlen);
237 error = xfs_bmapi_read(ip, 0, fsblocks, mval, &nmaps, 0);
242 for (n = 0; n < nmaps; n++) {
243 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
244 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
246 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0,
247 &xfs_symlink_buf_ops);
249 return XFS_ERROR(ENOMEM);
252 xfs_buf_ioerror_alert(bp, __func__);
256 byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
257 if (pathlen < byte_cnt)
260 cur_chunk = bp->b_addr;
261 if (xfs_sb_version_hascrc(&mp->m_sb)) {
262 if (!xfs_symlink_hdr_ok(mp, ip->i_ino, offset,
264 error = EFSCORRUPTED;
266 "symlink header does not match required off/len/owner (0x%x/Ox%x,0x%llx)",
267 offset, byte_cnt, ip->i_ino);
273 cur_chunk += sizeof(struct xfs_dsymlink_hdr);
276 memcpy(link + offset, bp->b_addr, byte_cnt);
283 ASSERT(pathlen == 0);
285 link[ip->i_d.di_size] = '\0';
294 struct xfs_inode *ip,
297 struct xfs_mount *mp = ip->i_mount;
301 trace_xfs_readlink(ip);
303 if (XFS_FORCED_SHUTDOWN(mp))
304 return XFS_ERROR(EIO);
306 xfs_ilock(ip, XFS_ILOCK_SHARED);
308 pathlen = ip->i_d.di_size;
312 if (pathlen < 0 || pathlen > MAXPATHLEN) {
313 xfs_alert(mp, "%s: inode (%llu) bad symlink length (%lld)",
314 __func__, (unsigned long long) ip->i_ino,
315 (long long) pathlen);
317 error = XFS_ERROR(EFSCORRUPTED);
322 if (ip->i_df.if_flags & XFS_IFINLINE) {
323 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
324 link[pathlen] = '\0';
326 error = xfs_readlink_bmap(ip, link);
330 xfs_iunlock(ip, XFS_ILOCK_SHARED);
336 struct xfs_inode *dp,
337 struct xfs_name *link_name,
338 const char *target_path,
340 struct xfs_inode **ipp)
342 struct xfs_mount *mp = dp->i_mount;
343 struct xfs_trans *tp = NULL;
344 struct xfs_inode *ip = NULL;
347 struct xfs_bmap_free free_list;
348 xfs_fsblock_t first_block;
349 bool unlock_dp_on_error = false;
352 xfs_fileoff_t first_fsb;
353 xfs_filblks_t fs_blocks;
355 struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS];
357 const char *cur_chunk;
362 struct xfs_dquot *udqp = NULL;
363 struct xfs_dquot *gdqp = NULL;
364 struct xfs_dquot *pdqp = NULL;
369 trace_xfs_symlink(dp, link_name);
371 if (XFS_FORCED_SHUTDOWN(mp))
372 return XFS_ERROR(EIO);
375 * Check component lengths of the target path name.
377 pathlen = strlen(target_path);
378 if (pathlen >= MAXPATHLEN) /* total string too long */
379 return XFS_ERROR(ENAMETOOLONG);
382 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
383 prid = xfs_get_projid(dp);
385 prid = XFS_PROJID_DEFAULT;
388 * Make sure that we have allocated dquot(s) on disk.
390 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
391 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp, &pdqp);
395 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
396 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
398 * The symlink will fit into the inode data fork?
399 * There can't be any attributes so we get the whole variable part.
401 if (pathlen <= XFS_LITINO(mp, dp->i_d.di_version))
404 fs_blocks = xfs_symlink_blocks(mp, pathlen);
405 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
406 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
407 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
408 if (error == ENOSPC && fs_blocks == 0) {
410 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
411 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
418 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
419 unlock_dp_on_error = true;
422 * Check whether the directory allows new symlinks or not.
424 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
425 error = XFS_ERROR(EPERM);
430 * Reserve disk quota : blocks and inode.
432 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
433 pdqp, resblks, 1, 0);
438 * Check for ability to enter directory entry, if no space reserved.
440 error = xfs_dir_canenter(tp, dp, link_name, resblks);
444 * Initialize the bmap freelist prior to calling either
445 * bmapi or the directory create code.
447 xfs_bmap_init(&free_list, &first_block);
450 * Allocate an inode for the symlink.
452 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT), 1, 0,
453 prid, resblks > 0, &ip, NULL);
461 * An error after we've joined dp to the transaction will result in the
462 * transaction cancel unlocking dp so don't do it explicitly in the
465 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
466 unlock_dp_on_error = false;
469 * Also attach the dquot(s) to it, if applicable.
471 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
474 resblks -= XFS_IALLOC_SPACE_RES(mp);
476 * If the symlink will fit into the inode, write it inline.
478 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
479 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
480 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
481 ip->i_d.di_size = pathlen;
484 * The inode was initially created in extent format.
486 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
487 ip->i_df.if_flags |= XFS_IFINLINE;
489 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
490 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
496 nmaps = XFS_SYMLINK_MAPS;
498 error = xfs_bmapi_write(tp, ip, first_fsb, fs_blocks,
499 XFS_BMAPI_METADATA, &first_block, resblks,
500 mval, &nmaps, &free_list);
505 resblks -= fs_blocks;
506 ip->i_d.di_size = pathlen;
507 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
509 cur_chunk = target_path;
511 for (n = 0; n < nmaps; n++) {
514 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
515 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
516 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
522 bp->b_ops = &xfs_symlink_buf_ops;
524 byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
525 byte_cnt = min(byte_cnt, pathlen);
528 buf += xfs_symlink_hdr_set(mp, ip->i_ino, offset,
531 memcpy(buf, cur_chunk, byte_cnt);
533 cur_chunk += byte_cnt;
537 xfs_trans_log_buf(tp, bp, 0, (buf + byte_cnt - 1) -
540 ASSERT(pathlen == 0);
544 * Create the directory entry for the symlink.
546 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
547 &first_block, &free_list, resblks);
550 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
551 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
554 * If this is a synchronous mount, make sure that the
555 * symlink transaction goes to disk before returning to
558 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
559 xfs_trans_set_sync(tp);
562 error = xfs_bmap_finish(&tp, &free_list, &committed);
566 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
577 xfs_bmap_cancel(&free_list);
578 cancel_flags |= XFS_TRANS_ABORT;
580 xfs_trans_cancel(tp, cancel_flags);
585 if (unlock_dp_on_error)
586 xfs_iunlock(dp, XFS_ILOCK_EXCL);
592 * Free a symlink that has blocks associated with it.
595 xfs_inactive_symlink_rmt(
603 xfs_fsblock_t first_block;
604 xfs_bmap_free_t free_list;
607 xfs_bmbt_irec_t mval[XFS_SYMLINK_MAPS];
615 ASSERT(ip->i_df.if_flags & XFS_IFEXTENTS);
617 * We're freeing a symlink that has some
618 * blocks allocated to it. Free the
619 * blocks here. We know that we've got
620 * either 1 or 2 extents and that we can
621 * free them all in one bunmapi call.
623 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
626 * Lock the inode, fix the size, and join it to the transaction.
627 * Hold it so in the normal path, we still have it locked for
628 * the second transaction. In the error paths we need it
629 * held so the cancel won't rele it, see below.
631 size = (int)ip->i_d.di_size;
633 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
635 * Find the block(s) so we can inval and unmap them.
638 xfs_bmap_init(&free_list, &first_block);
639 nmaps = ARRAY_SIZE(mval);
640 error = xfs_bmapi_read(ip, 0, xfs_symlink_blocks(mp, size),
645 * Invalidate the block(s). No validation is done.
647 for (i = 0; i < nmaps; i++) {
648 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
649 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
650 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
655 xfs_trans_binval(tp, bp);
658 * Unmap the dead block(s) to the free_list.
660 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
661 &first_block, &free_list, &done)))
665 * Commit the first transaction. This logs the EFI and the inode.
667 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
670 * The transaction must have been committed, since there were
671 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
672 * The new tp has the extent freeing and EFDs.
676 * The first xact was committed, so add the inode to the new one.
677 * Mark it dirty so it will be logged and moved forward in the log as
678 * part of every commit.
680 xfs_trans_ijoin(tp, ip, 0);
681 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
683 * Get a new, empty transaction to return to our caller.
685 ntp = xfs_trans_dup(tp);
687 * Commit the transaction containing extent freeing and EFDs.
688 * If we get an error on the commit here or on the reserve below,
689 * we need to unlock the inode since the new transaction doesn't
690 * have the inode attached.
692 error = xfs_trans_commit(tp, 0);
695 ASSERT(XFS_FORCED_SHUTDOWN(mp));
699 * transaction commit worked ok so we can drop the extra ticket
700 * reference that we gained in xfs_trans_dup()
702 xfs_log_ticket_put(tp->t_ticket);
705 * Remove the memory for extent descriptions (just bookkeeping).
707 if (ip->i_df.if_bytes)
708 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
709 ASSERT(ip->i_df.if_bytes == 0);
711 * Put an itruncate log reservation in the new transaction
714 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
715 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
716 ASSERT(XFS_FORCED_SHUTDOWN(mp));
720 xfs_trans_ijoin(tp, ip, 0);
725 xfs_bmap_cancel(&free_list);
731 * xfs_inactive_symlink - free a symlink
734 xfs_inactive_symlink(
735 struct xfs_inode *ip,
736 struct xfs_trans **tp)
738 struct xfs_mount *mp = ip->i_mount;
741 trace_xfs_inactive_symlink(ip);
743 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
745 if (XFS_FORCED_SHUTDOWN(mp))
746 return XFS_ERROR(EIO);
749 * Zero length symlinks _can_ exist.
751 pathlen = (int)ip->i_d.di_size;
755 if (pathlen < 0 || pathlen > MAXPATHLEN) {
756 xfs_alert(mp, "%s: inode (0x%llx) bad symlink length (%d)",
757 __func__, (unsigned long long)ip->i_ino, pathlen);
759 return XFS_ERROR(EFSCORRUPTED);
762 if (ip->i_df.if_flags & XFS_IFINLINE) {
763 if (ip->i_df.if_bytes > 0)
764 xfs_idata_realloc(ip, -(ip->i_df.if_bytes),
766 ASSERT(ip->i_df.if_bytes == 0);
770 /* remove the remote symlink */
771 return xfs_inactive_symlink_rmt(ip, tp);