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"
28 #include "xfs_mount.h"
29 #include "xfs_da_btree.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_ialloc_btree.h"
32 #include "xfs_dinode.h"
33 #include "xfs_inode.h"
34 #include "xfs_inode_item.h"
35 #include "xfs_itable.h"
36 #include "xfs_ialloc.h"
37 #include "xfs_alloc.h"
39 #include "xfs_error.h"
40 #include "xfs_quota.h"
41 #include "xfs_utils.h"
42 #include "xfs_trans_space.h"
43 #include "xfs_log_priv.h"
44 #include "xfs_trace.h"
45 #include "xfs_symlink.h"
46 #include "xfs_cksum.h"
47 #include "xfs_buf_item.h"
51 * Each contiguous block has a header, so it is not just a simple pathlen
59 int buflen = XFS_SYMLINK_BUF_SPACE(mp, mp->m_sb.sb_blocksize);
61 return (pathlen + buflen - 1) / buflen;
72 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
74 if (!xfs_sb_version_hascrc(&mp->m_sb))
77 dsl->sl_magic = cpu_to_be32(XFS_SYMLINK_MAGIC);
78 dsl->sl_offset = cpu_to_be32(offset);
79 dsl->sl_bytes = cpu_to_be32(size);
80 uuid_copy(&dsl->sl_uuid, &mp->m_sb.sb_uuid);
81 dsl->sl_owner = cpu_to_be64(ino);
82 dsl->sl_blkno = cpu_to_be64(bp->b_bn);
83 bp->b_ops = &xfs_symlink_buf_ops;
85 return sizeof(struct xfs_dsymlink_hdr);
89 * Checking of the symlink header is split into two parts. the verifier does
90 * CRC, location and bounds checking, the unpacking function checks the path
91 * parameters and owner.
101 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
103 if (offset != be32_to_cpu(dsl->sl_offset))
105 if (size != be32_to_cpu(dsl->sl_bytes))
107 if (ino != be64_to_cpu(dsl->sl_owner))
118 struct xfs_mount *mp = bp->b_target->bt_mount;
119 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
121 if (!xfs_sb_version_hascrc(&mp->m_sb))
123 if (dsl->sl_magic != cpu_to_be32(XFS_SYMLINK_MAGIC))
125 if (!uuid_equal(&dsl->sl_uuid, &mp->m_sb.sb_uuid))
127 if (bp->b_bn != be64_to_cpu(dsl->sl_blkno))
129 if (be32_to_cpu(dsl->sl_offset) +
130 be32_to_cpu(dsl->sl_bytes) >= MAXPATHLEN)
132 if (dsl->sl_owner == 0)
139 xfs_symlink_read_verify(
142 struct xfs_mount *mp = bp->b_target->bt_mount;
144 /* no verification of non-crc buffers */
145 if (!xfs_sb_version_hascrc(&mp->m_sb))
148 if (!xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
149 offsetof(struct xfs_dsymlink_hdr, sl_crc)) ||
150 !xfs_symlink_verify(bp)) {
151 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
152 xfs_buf_ioerror(bp, EFSCORRUPTED);
157 xfs_symlink_write_verify(
160 struct xfs_mount *mp = bp->b_target->bt_mount;
161 struct xfs_buf_log_item *bip = bp->b_fspriv;
163 /* no verification of non-crc buffers */
164 if (!xfs_sb_version_hascrc(&mp->m_sb))
167 if (!xfs_symlink_verify(bp)) {
168 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
169 xfs_buf_ioerror(bp, EFSCORRUPTED);
174 struct xfs_dsymlink_hdr *dsl = bp->b_addr;
175 dsl->sl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
177 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
178 offsetof(struct xfs_dsymlink_hdr, sl_crc));
181 const struct xfs_buf_ops xfs_symlink_buf_ops = {
182 .verify_read = xfs_symlink_read_verify,
183 .verify_write = xfs_symlink_write_verify,
187 xfs_symlink_local_to_remote(
188 struct xfs_trans *tp,
190 struct xfs_inode *ip,
191 struct xfs_ifork *ifp)
193 struct xfs_mount *mp = ip->i_mount;
196 if (!xfs_sb_version_hascrc(&mp->m_sb)) {
198 memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes);
203 * As this symlink fits in an inode literal area, it must also fit in
204 * the smallest buffer the filesystem supports.
206 ASSERT(BBTOB(bp->b_length) >=
207 ifp->if_bytes + sizeof(struct xfs_dsymlink_hdr));
209 bp->b_ops = &xfs_symlink_buf_ops;
212 buf += xfs_symlink_hdr_set(mp, ip->i_ino, 0, ifp->if_bytes, bp);
213 memcpy(buf, ifp->if_u1.if_data, ifp->if_bytes);
216 /* ----- Kernel only functions below ----- */
219 struct xfs_inode *ip,
222 struct xfs_mount *mp = ip->i_mount;
223 struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS];
227 int pathlen = ip->i_d.di_size;
228 int nmaps = XFS_SYMLINK_MAPS;
235 fsblocks = xfs_symlink_blocks(mp, pathlen);
236 error = xfs_bmapi_read(ip, 0, fsblocks, mval, &nmaps, 0);
241 for (n = 0; n < nmaps; n++) {
242 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
243 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
245 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0,
246 &xfs_symlink_buf_ops);
248 return XFS_ERROR(ENOMEM);
251 xfs_buf_ioerror_alert(bp, __func__);
255 byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
256 if (pathlen < byte_cnt)
259 cur_chunk = bp->b_addr;
260 if (xfs_sb_version_hascrc(&mp->m_sb)) {
261 if (!xfs_symlink_hdr_ok(mp, ip->i_ino, offset,
263 error = EFSCORRUPTED;
265 "symlink header does not match required off/len/owner (0x%x/Ox%x,0x%llx)",
266 offset, byte_cnt, ip->i_ino);
272 cur_chunk += sizeof(struct xfs_dsymlink_hdr);
275 memcpy(link + offset, bp->b_addr, byte_cnt);
282 ASSERT(pathlen == 0);
284 link[ip->i_d.di_size] = '\0';
293 struct xfs_inode *ip,
296 struct xfs_mount *mp = ip->i_mount;
300 trace_xfs_readlink(ip);
302 if (XFS_FORCED_SHUTDOWN(mp))
303 return XFS_ERROR(EIO);
305 xfs_ilock(ip, XFS_ILOCK_SHARED);
307 pathlen = ip->i_d.di_size;
311 if (pathlen < 0 || pathlen > MAXPATHLEN) {
312 xfs_alert(mp, "%s: inode (%llu) bad symlink length (%lld)",
313 __func__, (unsigned long long) ip->i_ino,
314 (long long) pathlen);
316 error = XFS_ERROR(EFSCORRUPTED);
321 if (ip->i_df.if_flags & XFS_IFINLINE) {
322 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
323 link[pathlen] = '\0';
325 error = xfs_readlink_bmap(ip, link);
329 xfs_iunlock(ip, XFS_ILOCK_SHARED);
335 struct xfs_inode *dp,
336 struct xfs_name *link_name,
337 const char *target_path,
339 struct xfs_inode **ipp)
341 struct xfs_mount *mp = dp->i_mount;
342 struct xfs_trans *tp = NULL;
343 struct xfs_inode *ip = NULL;
346 struct xfs_bmap_free free_list;
347 xfs_fsblock_t first_block;
348 bool unlock_dp_on_error = false;
351 xfs_fileoff_t first_fsb;
352 xfs_filblks_t fs_blocks;
354 struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS];
356 const char *cur_chunk;
361 struct xfs_dquot *udqp = NULL;
362 struct xfs_dquot *gdqp = NULL;
363 struct xfs_dquot *pdqp = NULL;
368 trace_xfs_symlink(dp, link_name);
370 if (XFS_FORCED_SHUTDOWN(mp))
371 return XFS_ERROR(EIO);
374 * Check component lengths of the target path name.
376 pathlen = strlen(target_path);
377 if (pathlen >= MAXPATHLEN) /* total string too long */
378 return XFS_ERROR(ENAMETOOLONG);
381 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
382 prid = xfs_get_projid(dp);
384 prid = XFS_PROJID_DEFAULT;
387 * Make sure that we have allocated dquot(s) on disk.
389 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
390 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp, &pdqp);
394 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
395 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
397 * The symlink will fit into the inode data fork?
398 * There can't be any attributes so we get the whole variable part.
400 if (pathlen <= XFS_LITINO(mp, dp->i_d.di_version))
403 fs_blocks = xfs_symlink_blocks(mp, pathlen);
404 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
405 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
406 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
407 if (error == ENOSPC && fs_blocks == 0) {
409 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
410 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
417 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
418 unlock_dp_on_error = true;
421 * Check whether the directory allows new symlinks or not.
423 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
424 error = XFS_ERROR(EPERM);
429 * Reserve disk quota : blocks and inode.
431 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
432 pdqp, resblks, 1, 0);
437 * Check for ability to enter directory entry, if no space reserved.
439 error = xfs_dir_canenter(tp, dp, link_name, resblks);
443 * Initialize the bmap freelist prior to calling either
444 * bmapi or the directory create code.
446 xfs_bmap_init(&free_list, &first_block);
449 * Allocate an inode for the symlink.
451 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT), 1, 0,
452 prid, resblks > 0, &ip, NULL);
460 * An error after we've joined dp to the transaction will result in the
461 * transaction cancel unlocking dp so don't do it explicitly in the
464 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
465 unlock_dp_on_error = false;
468 * Also attach the dquot(s) to it, if applicable.
470 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
473 resblks -= XFS_IALLOC_SPACE_RES(mp);
475 * If the symlink will fit into the inode, write it inline.
477 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
478 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
479 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
480 ip->i_d.di_size = pathlen;
483 * The inode was initially created in extent format.
485 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
486 ip->i_df.if_flags |= XFS_IFINLINE;
488 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
489 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
495 nmaps = XFS_SYMLINK_MAPS;
497 error = xfs_bmapi_write(tp, ip, first_fsb, fs_blocks,
498 XFS_BMAPI_METADATA, &first_block, resblks,
499 mval, &nmaps, &free_list);
504 resblks -= fs_blocks;
505 ip->i_d.di_size = pathlen;
506 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
508 cur_chunk = target_path;
510 for (n = 0; n < nmaps; n++) {
513 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
514 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
515 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
521 bp->b_ops = &xfs_symlink_buf_ops;
523 byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
524 byte_cnt = min(byte_cnt, pathlen);
527 buf += xfs_symlink_hdr_set(mp, ip->i_ino, offset,
530 memcpy(buf, cur_chunk, byte_cnt);
532 cur_chunk += byte_cnt;
536 xfs_trans_log_buf(tp, bp, 0, (buf + byte_cnt - 1) -
539 ASSERT(pathlen == 0);
543 * Create the directory entry for the symlink.
545 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
546 &first_block, &free_list, resblks);
549 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
550 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
553 * If this is a synchronous mount, make sure that the
554 * symlink transaction goes to disk before returning to
557 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
558 xfs_trans_set_sync(tp);
561 error = xfs_bmap_finish(&tp, &free_list, &committed);
565 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
576 xfs_bmap_cancel(&free_list);
577 cancel_flags |= XFS_TRANS_ABORT;
579 xfs_trans_cancel(tp, cancel_flags);
584 if (unlock_dp_on_error)
585 xfs_iunlock(dp, XFS_ILOCK_EXCL);
591 * Free a symlink that has blocks associated with it.
594 xfs_inactive_symlink_rmt(
602 xfs_fsblock_t first_block;
603 xfs_bmap_free_t free_list;
606 xfs_bmbt_irec_t mval[XFS_SYMLINK_MAPS];
614 ASSERT(ip->i_df.if_flags & XFS_IFEXTENTS);
616 * We're freeing a symlink that has some
617 * blocks allocated to it. Free the
618 * blocks here. We know that we've got
619 * either 1 or 2 extents and that we can
620 * free them all in one bunmapi call.
622 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
625 * Lock the inode, fix the size, and join it to the transaction.
626 * Hold it so in the normal path, we still have it locked for
627 * the second transaction. In the error paths we need it
628 * held so the cancel won't rele it, see below.
630 size = (int)ip->i_d.di_size;
632 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
634 * Find the block(s) so we can inval and unmap them.
637 xfs_bmap_init(&free_list, &first_block);
638 nmaps = ARRAY_SIZE(mval);
639 error = xfs_bmapi_read(ip, 0, xfs_symlink_blocks(mp, size),
644 * Invalidate the block(s). No validation is done.
646 for (i = 0; i < nmaps; i++) {
647 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
648 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
649 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
654 xfs_trans_binval(tp, bp);
657 * Unmap the dead block(s) to the free_list.
659 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
660 &first_block, &free_list, &done)))
664 * Commit the first transaction. This logs the EFI and the inode.
666 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
669 * The transaction must have been committed, since there were
670 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
671 * The new tp has the extent freeing and EFDs.
675 * The first xact was committed, so add the inode to the new one.
676 * Mark it dirty so it will be logged and moved forward in the log as
677 * part of every commit.
679 xfs_trans_ijoin(tp, ip, 0);
680 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
682 * Get a new, empty transaction to return to our caller.
684 ntp = xfs_trans_dup(tp);
686 * Commit the transaction containing extent freeing and EFDs.
687 * If we get an error on the commit here or on the reserve below,
688 * we need to unlock the inode since the new transaction doesn't
689 * have the inode attached.
691 error = xfs_trans_commit(tp, 0);
694 ASSERT(XFS_FORCED_SHUTDOWN(mp));
698 * transaction commit worked ok so we can drop the extra ticket
699 * reference that we gained in xfs_trans_dup()
701 xfs_log_ticket_put(tp->t_ticket);
704 * Remove the memory for extent descriptions (just bookkeeping).
706 if (ip->i_df.if_bytes)
707 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
708 ASSERT(ip->i_df.if_bytes == 0);
710 * Put an itruncate log reservation in the new transaction
713 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
714 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
715 ASSERT(XFS_FORCED_SHUTDOWN(mp));
719 xfs_trans_ijoin(tp, ip, 0);
724 xfs_bmap_cancel(&free_list);
730 * xfs_inactive_symlink - free a symlink
733 xfs_inactive_symlink(
734 struct xfs_inode *ip,
735 struct xfs_trans **tp)
737 struct xfs_mount *mp = ip->i_mount;
740 trace_xfs_inactive_symlink(ip);
742 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
744 if (XFS_FORCED_SHUTDOWN(mp))
745 return XFS_ERROR(EIO);
748 * Zero length symlinks _can_ exist.
750 pathlen = (int)ip->i_d.di_size;
754 if (pathlen < 0 || pathlen > MAXPATHLEN) {
755 xfs_alert(mp, "%s: inode (0x%llx) bad symlink length (%d)",
756 __func__, (unsigned long long)ip->i_ino, pathlen);
758 return XFS_ERROR(EFSCORRUPTED);
761 if (ip->i_df.if_flags & XFS_IFINLINE) {
762 if (ip->i_df.if_bytes > 0)
763 xfs_idata_realloc(ip, -(ip->i_df.if_bytes),
765 ASSERT(ip->i_df.if_bytes == 0);
769 /* remove the remote symlink */
770 return xfs_inactive_symlink_rmt(ip, tp);