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_types.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
64 len
-= XFS_SYMLINK_BUF_SPACE(mp
, mp
->m_sb
.sb_blocksize
);
67 ASSERT(fsblocks
<= XFS_SYMLINK_MAPS
);
79 struct xfs_dsymlink_hdr
*dsl
= bp
->b_addr
;
81 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
84 dsl
->sl_magic
= cpu_to_be32(XFS_SYMLINK_MAGIC
);
85 dsl
->sl_offset
= cpu_to_be32(offset
);
86 dsl
->sl_bytes
= cpu_to_be32(size
);
87 uuid_copy(&dsl
->sl_uuid
, &mp
->m_sb
.sb_uuid
);
88 dsl
->sl_owner
= cpu_to_be64(ino
);
89 dsl
->sl_blkno
= cpu_to_be64(bp
->b_bn
);
90 bp
->b_ops
= &xfs_symlink_buf_ops
;
92 return sizeof(struct xfs_dsymlink_hdr
);
96 * Checking of the symlink header is split into two parts. the verifier does
97 * CRC, location and bounds checking, the unpacking function checks the path
98 * parameters and owner.
102 struct xfs_mount
*mp
,
108 struct xfs_dsymlink_hdr
*dsl
= bp
->b_addr
;
110 if (offset
!= be32_to_cpu(dsl
->sl_offset
))
112 if (size
!= be32_to_cpu(dsl
->sl_bytes
))
114 if (ino
!= be64_to_cpu(dsl
->sl_owner
))
125 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
126 struct xfs_dsymlink_hdr
*dsl
= bp
->b_addr
;
128 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
130 if (dsl
->sl_magic
!= cpu_to_be32(XFS_SYMLINK_MAGIC
))
132 if (!uuid_equal(&dsl
->sl_uuid
, &mp
->m_sb
.sb_uuid
))
134 if (bp
->b_bn
!= be64_to_cpu(dsl
->sl_blkno
))
136 if (be32_to_cpu(dsl
->sl_offset
) +
137 be32_to_cpu(dsl
->sl_bytes
) >= MAXPATHLEN
)
139 if (dsl
->sl_owner
== 0)
146 xfs_symlink_read_verify(
149 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
151 /* no verification of non-crc buffers */
152 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
155 if (!xfs_verify_cksum(bp
->b_addr
, BBTOB(bp
->b_length
),
156 offsetof(struct xfs_dsymlink_hdr
, sl_crc
)) ||
157 !xfs_symlink_verify(bp
)) {
158 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
, mp
, bp
->b_addr
);
159 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
164 xfs_symlink_write_verify(
167 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
168 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
170 /* no verification of non-crc buffers */
171 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
174 if (!xfs_symlink_verify(bp
)) {
175 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
, mp
, bp
->b_addr
);
176 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
181 struct xfs_dsymlink_hdr
*dsl
= bp
->b_addr
;
182 dsl
->sl_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
184 xfs_update_cksum(bp
->b_addr
, BBTOB(bp
->b_length
),
185 offsetof(struct xfs_dsymlink_hdr
, sl_crc
));
188 const struct xfs_buf_ops xfs_symlink_buf_ops
= {
189 .verify_read
= xfs_symlink_read_verify
,
190 .verify_write
= xfs_symlink_write_verify
,
194 xfs_symlink_local_to_remote(
195 struct xfs_trans
*tp
,
197 struct xfs_inode
*ip
,
198 struct xfs_ifork
*ifp
)
200 struct xfs_mount
*mp
= ip
->i_mount
;
203 if (!xfs_sb_version_hascrc(&mp
->m_sb
)) {
205 memcpy(bp
->b_addr
, ifp
->if_u1
.if_data
, ifp
->if_bytes
);
210 * As this symlink fits in an inode literal area, it must also fit in
211 * the smallest buffer the filesystem supports.
213 ASSERT(BBTOB(bp
->b_length
) >=
214 ifp
->if_bytes
+ sizeof(struct xfs_dsymlink_hdr
));
216 bp
->b_ops
= &xfs_symlink_buf_ops
;
219 buf
+= xfs_symlink_hdr_set(mp
, ip
->i_ino
, 0, ifp
->if_bytes
, bp
);
220 memcpy(buf
, ifp
->if_u1
.if_data
, ifp
->if_bytes
);
223 /* ----- Kernel only functions below ----- */
226 struct xfs_inode
*ip
,
229 struct xfs_mount
*mp
= ip
->i_mount
;
230 struct xfs_bmbt_irec mval
[XFS_SYMLINK_MAPS
];
234 int pathlen
= ip
->i_d
.di_size
;
235 int nmaps
= XFS_SYMLINK_MAPS
;
242 fsblocks
= xfs_symlink_blocks(mp
, pathlen
);
243 error
= xfs_bmapi_read(ip
, 0, fsblocks
, mval
, &nmaps
, 0);
248 for (n
= 0; n
< nmaps
; n
++) {
249 d
= XFS_FSB_TO_DADDR(mp
, mval
[n
].br_startblock
);
250 byte_cnt
= XFS_FSB_TO_B(mp
, mval
[n
].br_blockcount
);
252 bp
= xfs_buf_read(mp
->m_ddev_targp
, d
, BTOBB(byte_cnt
), 0,
253 &xfs_symlink_buf_ops
);
255 return XFS_ERROR(ENOMEM
);
258 xfs_buf_ioerror_alert(bp
, __func__
);
262 byte_cnt
= XFS_SYMLINK_BUF_SPACE(mp
, byte_cnt
);
263 if (pathlen
< byte_cnt
)
266 cur_chunk
= bp
->b_addr
;
267 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
268 if (!xfs_symlink_hdr_ok(mp
, ip
->i_ino
, offset
,
270 error
= EFSCORRUPTED
;
272 "symlink header does not match required off/len/owner (0x%x/Ox%x,0x%llx)",
273 offset
, byte_cnt
, ip
->i_ino
);
279 cur_chunk
+= sizeof(struct xfs_dsymlink_hdr
);
282 memcpy(link
+ offset
, bp
->b_addr
, byte_cnt
);
289 ASSERT(pathlen
== 0);
291 link
[ip
->i_d
.di_size
] = '\0';
300 struct xfs_inode
*ip
,
303 struct xfs_mount
*mp
= ip
->i_mount
;
307 trace_xfs_readlink(ip
);
309 if (XFS_FORCED_SHUTDOWN(mp
))
310 return XFS_ERROR(EIO
);
312 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
314 pathlen
= ip
->i_d
.di_size
;
318 if (pathlen
< 0 || pathlen
> MAXPATHLEN
) {
319 xfs_alert(mp
, "%s: inode (%llu) bad symlink length (%lld)",
320 __func__
, (unsigned long long) ip
->i_ino
,
321 (long long) pathlen
);
323 error
= XFS_ERROR(EFSCORRUPTED
);
328 if (ip
->i_df
.if_flags
& XFS_IFINLINE
) {
329 memcpy(link
, ip
->i_df
.if_u1
.if_data
, pathlen
);
330 link
[pathlen
] = '\0';
332 error
= xfs_readlink_bmap(ip
, link
);
336 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
342 struct xfs_inode
*dp
,
343 struct xfs_name
*link_name
,
344 const char *target_path
,
346 struct xfs_inode
**ipp
)
348 struct xfs_mount
*mp
= dp
->i_mount
;
349 struct xfs_trans
*tp
= NULL
;
350 struct xfs_inode
*ip
= NULL
;
353 struct xfs_bmap_free free_list
;
354 xfs_fsblock_t first_block
;
355 bool unlock_dp_on_error
= false;
358 xfs_fileoff_t first_fsb
;
359 xfs_filblks_t fs_blocks
;
361 struct xfs_bmbt_irec mval
[XFS_SYMLINK_MAPS
];
363 const char *cur_chunk
;
368 struct xfs_dquot
*udqp
, *gdqp
;
373 trace_xfs_symlink(dp
, link_name
);
375 if (XFS_FORCED_SHUTDOWN(mp
))
376 return XFS_ERROR(EIO
);
379 * Check component lengths of the target path name.
381 pathlen
= strlen(target_path
);
382 if (pathlen
>= MAXPATHLEN
) /* total string too long */
383 return XFS_ERROR(ENAMETOOLONG
);
386 if (dp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
)
387 prid
= xfs_get_projid(dp
);
389 prid
= XFS_PROJID_DEFAULT
;
392 * Make sure that we have allocated dquot(s) on disk.
394 error
= xfs_qm_vop_dqalloc(dp
, current_fsuid(), current_fsgid(), prid
,
395 XFS_QMOPT_QUOTALL
| XFS_QMOPT_INHERIT
, &udqp
, &gdqp
);
399 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SYMLINK
);
400 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
402 * The symlink will fit into the inode data fork?
403 * There can't be any attributes so we get the whole variable part.
405 if (pathlen
<= XFS_LITINO(mp
, dp
->i_d
.di_version
))
408 fs_blocks
= XFS_B_TO_FSB(mp
, pathlen
);
409 resblks
= XFS_SYMLINK_SPACE_RES(mp
, link_name
->len
, fs_blocks
);
410 error
= xfs_trans_reserve(tp
, resblks
, XFS_SYMLINK_LOG_RES(mp
), 0,
411 XFS_TRANS_PERM_LOG_RES
, XFS_SYMLINK_LOG_COUNT
);
412 if (error
== ENOSPC
&& fs_blocks
== 0) {
414 error
= xfs_trans_reserve(tp
, 0, XFS_SYMLINK_LOG_RES(mp
), 0,
415 XFS_TRANS_PERM_LOG_RES
, XFS_SYMLINK_LOG_COUNT
);
422 xfs_ilock(dp
, XFS_ILOCK_EXCL
| XFS_ILOCK_PARENT
);
423 unlock_dp_on_error
= true;
426 * Check whether the directory allows new symlinks or not.
428 if (dp
->i_d
.di_flags
& XFS_DIFLAG_NOSYMLINKS
) {
429 error
= XFS_ERROR(EPERM
);
434 * Reserve disk quota : blocks and inode.
436 error
= xfs_trans_reserve_quota(tp
, mp
, udqp
, gdqp
, resblks
, 1, 0);
441 * Check for ability to enter directory entry, if no space reserved.
443 error
= xfs_dir_canenter(tp
, dp
, link_name
, resblks
);
447 * Initialize the bmap freelist prior to calling either
448 * bmapi or the directory create code.
450 xfs_bmap_init(&free_list
, &first_block
);
453 * Allocate an inode for the symlink.
455 error
= xfs_dir_ialloc(&tp
, dp
, S_IFLNK
| (mode
& ~S_IFMT
), 1, 0,
456 prid
, resblks
> 0, &ip
, NULL
);
464 * An error after we've joined dp to the transaction will result in the
465 * transaction cancel unlocking dp so don't do it explicitly in the
468 xfs_trans_ijoin(tp
, dp
, XFS_ILOCK_EXCL
);
469 unlock_dp_on_error
= false;
472 * Also attach the dquot(s) to it, if applicable.
474 xfs_qm_vop_create_dqattach(tp
, ip
, udqp
, gdqp
);
477 resblks
-= XFS_IALLOC_SPACE_RES(mp
);
479 * If the symlink will fit into the inode, write it inline.
481 if (pathlen
<= XFS_IFORK_DSIZE(ip
)) {
482 xfs_idata_realloc(ip
, pathlen
, XFS_DATA_FORK
);
483 memcpy(ip
->i_df
.if_u1
.if_data
, target_path
, pathlen
);
484 ip
->i_d
.di_size
= pathlen
;
487 * The inode was initially created in extent format.
489 ip
->i_df
.if_flags
&= ~(XFS_IFEXTENTS
| XFS_IFBROOT
);
490 ip
->i_df
.if_flags
|= XFS_IFINLINE
;
492 ip
->i_d
.di_format
= XFS_DINODE_FMT_LOCAL
;
493 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_DDATA
| XFS_ILOG_CORE
);
499 nmaps
= XFS_SYMLINK_MAPS
;
501 error
= xfs_bmapi_write(tp
, ip
, first_fsb
, fs_blocks
,
502 XFS_BMAPI_METADATA
, &first_block
, resblks
,
503 mval
, &nmaps
, &free_list
);
508 resblks
-= fs_blocks
;
509 ip
->i_d
.di_size
= pathlen
;
510 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
512 cur_chunk
= target_path
;
514 for (n
= 0; n
< nmaps
; n
++) {
517 d
= XFS_FSB_TO_DADDR(mp
, mval
[n
].br_startblock
);
518 byte_cnt
= XFS_FSB_TO_B(mp
, mval
[n
].br_blockcount
);
519 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
,
525 bp
->b_ops
= &xfs_symlink_buf_ops
;
527 byte_cnt
= XFS_SYMLINK_BUF_SPACE(mp
, byte_cnt
);
528 if (pathlen
< byte_cnt
) {
533 buf
+= xfs_symlink_hdr_set(mp
, ip
->i_ino
, offset
,
536 memcpy(buf
, cur_chunk
, byte_cnt
);
538 cur_chunk
+= byte_cnt
;
542 xfs_trans_log_buf(tp
, bp
, 0, (buf
+ byte_cnt
- 1) -
548 * Create the directory entry for the symlink.
550 error
= xfs_dir_createname(tp
, dp
, link_name
, ip
->i_ino
,
551 &first_block
, &free_list
, resblks
);
554 xfs_trans_ichgtime(tp
, dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
555 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
558 * If this is a synchronous mount, make sure that the
559 * symlink transaction goes to disk before returning to
562 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
563 xfs_trans_set_sync(tp
);
566 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
570 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
580 xfs_bmap_cancel(&free_list
);
581 cancel_flags
|= XFS_TRANS_ABORT
;
583 xfs_trans_cancel(tp
, cancel_flags
);
587 if (unlock_dp_on_error
)
588 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
594 * Free a symlink that has blocks associated with it.
597 xfs_inactive_symlink_rmt(
605 xfs_fsblock_t first_block
;
606 xfs_bmap_free_t free_list
;
609 xfs_bmbt_irec_t mval
[XFS_SYMLINK_MAPS
];
617 ASSERT(ip
->i_d
.di_size
> XFS_IFORK_DSIZE(ip
));
619 * We're freeing a symlink that has some
620 * blocks allocated to it. Free the
621 * blocks here. We know that we've got
622 * either 1 or 2 extents and that we can
623 * free them all in one bunmapi call.
625 ASSERT(ip
->i_d
.di_nextents
> 0 && ip
->i_d
.di_nextents
<= 2);
628 * Lock the inode, fix the size, and join it to the transaction.
629 * Hold it so in the normal path, we still have it locked for
630 * the second transaction. In the error paths we need it
631 * held so the cancel won't rele it, see below.
633 size
= (int)ip
->i_d
.di_size
;
635 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
637 * Find the block(s) so we can inval and unmap them.
640 xfs_bmap_init(&free_list
, &first_block
);
641 nmaps
= ARRAY_SIZE(mval
);
642 error
= xfs_bmapi_read(ip
, 0, xfs_symlink_blocks(mp
, size
),
647 * Invalidate the block(s). No validation is done.
649 for (i
= 0; i
< nmaps
; i
++) {
650 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
,
651 XFS_FSB_TO_DADDR(mp
, mval
[i
].br_startblock
),
652 XFS_FSB_TO_BB(mp
, mval
[i
].br_blockcount
), 0);
657 xfs_trans_binval(tp
, bp
);
660 * Unmap the dead block(s) to the free_list.
662 if ((error
= xfs_bunmapi(tp
, ip
, 0, size
, XFS_BMAPI_METADATA
, nmaps
,
663 &first_block
, &free_list
, &done
)))
667 * Commit the first transaction. This logs the EFI and the inode.
669 if ((error
= xfs_bmap_finish(&tp
, &free_list
, &committed
)))
672 * The transaction must have been committed, since there were
673 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
674 * The new tp has the extent freeing and EFDs.
678 * The first xact was committed, so add the inode to the new one.
679 * Mark it dirty so it will be logged and moved forward in the log as
680 * part of every commit.
682 xfs_trans_ijoin(tp
, ip
, 0);
683 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
685 * Get a new, empty transaction to return to our caller.
687 ntp
= xfs_trans_dup(tp
);
689 * Commit the transaction containing extent freeing and EFDs.
690 * If we get an error on the commit here or on the reserve below,
691 * we need to unlock the inode since the new transaction doesn't
692 * have the inode attached.
694 error
= xfs_trans_commit(tp
, 0);
697 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
701 * transaction commit worked ok so we can drop the extra ticket
702 * reference that we gained in xfs_trans_dup()
704 xfs_log_ticket_put(tp
->t_ticket
);
707 * Remove the memory for extent descriptions (just bookkeeping).
709 if (ip
->i_df
.if_bytes
)
710 xfs_idata_realloc(ip
, -ip
->i_df
.if_bytes
, XFS_DATA_FORK
);
711 ASSERT(ip
->i_df
.if_bytes
== 0);
713 * Put an itruncate log reservation in the new transaction
716 if ((error
= xfs_trans_reserve(tp
, 0, XFS_ITRUNCATE_LOG_RES(mp
), 0,
717 XFS_TRANS_PERM_LOG_RES
, XFS_ITRUNCATE_LOG_COUNT
))) {
718 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
722 xfs_trans_ijoin(tp
, ip
, 0);
727 xfs_bmap_cancel(&free_list
);