]>
Commit | Line | Data |
---|---|---|
19de7351 DC |
1 | /* |
2 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. | |
3 | * Copyright (c) 2012-2013 Red Hat, Inc. | |
4 | * All rights reserved. | |
5 | * | |
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. | |
9 | * | |
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. | |
14 | * | |
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 | |
18 | */ | |
19 | #include "xfs.h" | |
20 | #include "xfs_fs.h" | |
6ca1c906 | 21 | #include "xfs_format.h" |
19de7351 DC |
22 | #include "xfs_bit.h" |
23 | #include "xfs_log.h" | |
24 | #include "xfs_trans.h" | |
25 | #include "xfs_sb.h" | |
26 | #include "xfs_ag.h" | |
19de7351 DC |
27 | #include "xfs_mount.h" |
28 | #include "xfs_da_btree.h" | |
2b9ab5ab DC |
29 | #include "xfs_dir2_format.h" |
30 | #include "xfs_dir2.h" | |
19de7351 DC |
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" | |
39 | #include "xfs_bmap.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" | |
f948dd76 DC |
47 | #include "xfs_cksum.h" |
48 | #include "xfs_buf_item.h" | |
49 | ||
50 | ||
51 | /* | |
52 | * Each contiguous block has a header, so it is not just a simple pathlen | |
53 | * to FSB conversion. | |
54 | */ | |
55 | int | |
56 | xfs_symlink_blocks( | |
57 | struct xfs_mount *mp, | |
58 | int pathlen) | |
59 | { | |
321a9583 | 60 | int buflen = XFS_SYMLINK_BUF_SPACE(mp, mp->m_sb.sb_blocksize); |
f948dd76 | 61 | |
321a9583 | 62 | return (pathlen + buflen - 1) / buflen; |
f948dd76 DC |
63 | } |
64 | ||
65 | static int | |
66 | xfs_symlink_hdr_set( | |
67 | struct xfs_mount *mp, | |
68 | xfs_ino_t ino, | |
69 | uint32_t offset, | |
70 | uint32_t size, | |
71 | struct xfs_buf *bp) | |
72 | { | |
73 | struct xfs_dsymlink_hdr *dsl = bp->b_addr; | |
74 | ||
75 | if (!xfs_sb_version_hascrc(&mp->m_sb)) | |
76 | return 0; | |
77 | ||
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; | |
85 | ||
86 | return sizeof(struct xfs_dsymlink_hdr); | |
87 | } | |
88 | ||
89 | /* | |
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. | |
93 | */ | |
94 | bool | |
95 | xfs_symlink_hdr_ok( | |
96 | struct xfs_mount *mp, | |
97 | xfs_ino_t ino, | |
98 | uint32_t offset, | |
99 | uint32_t size, | |
100 | struct xfs_buf *bp) | |
101 | { | |
102 | struct xfs_dsymlink_hdr *dsl = bp->b_addr; | |
103 | ||
104 | if (offset != be32_to_cpu(dsl->sl_offset)) | |
105 | return false; | |
106 | if (size != be32_to_cpu(dsl->sl_bytes)) | |
107 | return false; | |
108 | if (ino != be64_to_cpu(dsl->sl_owner)) | |
109 | return false; | |
110 | ||
111 | /* ok */ | |
112 | return true; | |
113 | } | |
114 | ||
115 | static bool | |
116 | xfs_symlink_verify( | |
117 | struct xfs_buf *bp) | |
118 | { | |
119 | struct xfs_mount *mp = bp->b_target->bt_mount; | |
120 | struct xfs_dsymlink_hdr *dsl = bp->b_addr; | |
121 | ||
122 | if (!xfs_sb_version_hascrc(&mp->m_sb)) | |
123 | return false; | |
124 | if (dsl->sl_magic != cpu_to_be32(XFS_SYMLINK_MAGIC)) | |
125 | return false; | |
126 | if (!uuid_equal(&dsl->sl_uuid, &mp->m_sb.sb_uuid)) | |
127 | return false; | |
128 | if (bp->b_bn != be64_to_cpu(dsl->sl_blkno)) | |
129 | return false; | |
130 | if (be32_to_cpu(dsl->sl_offset) + | |
131 | be32_to_cpu(dsl->sl_bytes) >= MAXPATHLEN) | |
132 | return false; | |
133 | if (dsl->sl_owner == 0) | |
134 | return false; | |
135 | ||
136 | return true; | |
137 | } | |
138 | ||
139 | static void | |
140 | xfs_symlink_read_verify( | |
141 | struct xfs_buf *bp) | |
142 | { | |
143 | struct xfs_mount *mp = bp->b_target->bt_mount; | |
144 | ||
145 | /* no verification of non-crc buffers */ | |
146 | if (!xfs_sb_version_hascrc(&mp->m_sb)) | |
147 | return; | |
148 | ||
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); | |
154 | } | |
155 | } | |
156 | ||
157 | static void | |
158 | xfs_symlink_write_verify( | |
159 | struct xfs_buf *bp) | |
160 | { | |
161 | struct xfs_mount *mp = bp->b_target->bt_mount; | |
162 | struct xfs_buf_log_item *bip = bp->b_fspriv; | |
163 | ||
164 | /* no verification of non-crc buffers */ | |
165 | if (!xfs_sb_version_hascrc(&mp->m_sb)) | |
166 | return; | |
167 | ||
168 | if (!xfs_symlink_verify(bp)) { | |
169 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr); | |
170 | xfs_buf_ioerror(bp, EFSCORRUPTED); | |
171 | return; | |
172 | } | |
173 | ||
174 | if (bip) { | |
175 | struct xfs_dsymlink_hdr *dsl = bp->b_addr; | |
176 | dsl->sl_lsn = cpu_to_be64(bip->bli_item.li_lsn); | |
177 | } | |
178 | xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), | |
179 | offsetof(struct xfs_dsymlink_hdr, sl_crc)); | |
180 | } | |
181 | ||
182 | const struct xfs_buf_ops xfs_symlink_buf_ops = { | |
183 | .verify_read = xfs_symlink_read_verify, | |
184 | .verify_write = xfs_symlink_write_verify, | |
185 | }; | |
19de7351 DC |
186 | |
187 | void | |
188 | xfs_symlink_local_to_remote( | |
189 | struct xfs_trans *tp, | |
190 | struct xfs_buf *bp, | |
191 | struct xfs_inode *ip, | |
192 | struct xfs_ifork *ifp) | |
193 | { | |
f948dd76 DC |
194 | struct xfs_mount *mp = ip->i_mount; |
195 | char *buf; | |
196 | ||
197 | if (!xfs_sb_version_hascrc(&mp->m_sb)) { | |
198 | bp->b_ops = NULL; | |
199 | memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes); | |
200 | return; | |
201 | } | |
202 | ||
203 | /* | |
204 | * As this symlink fits in an inode literal area, it must also fit in | |
205 | * the smallest buffer the filesystem supports. | |
206 | */ | |
207 | ASSERT(BBTOB(bp->b_length) >= | |
208 | ifp->if_bytes + sizeof(struct xfs_dsymlink_hdr)); | |
209 | ||
210 | bp->b_ops = &xfs_symlink_buf_ops; | |
211 | ||
212 | buf = bp->b_addr; | |
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); | |
19de7351 DC |
215 | } |
216 | ||
217 | /* ----- Kernel only functions below ----- */ | |
19de7351 DC |
218 | STATIC int |
219 | xfs_readlink_bmap( | |
f948dd76 DC |
220 | struct xfs_inode *ip, |
221 | char *link) | |
19de7351 | 222 | { |
f948dd76 DC |
223 | struct xfs_mount *mp = ip->i_mount; |
224 | struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS]; | |
225 | struct xfs_buf *bp; | |
226 | xfs_daddr_t d; | |
227 | char *cur_chunk; | |
228 | int pathlen = ip->i_d.di_size; | |
229 | int nmaps = XFS_SYMLINK_MAPS; | |
230 | int byte_cnt; | |
231 | int n; | |
232 | int error = 0; | |
233 | int fsblocks = 0; | |
234 | int offset; | |
19de7351 | 235 | |
f948dd76 DC |
236 | fsblocks = xfs_symlink_blocks(mp, pathlen); |
237 | error = xfs_bmapi_read(ip, 0, fsblocks, mval, &nmaps, 0); | |
19de7351 DC |
238 | if (error) |
239 | goto out; | |
240 | ||
f948dd76 | 241 | offset = 0; |
19de7351 DC |
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); | |
245 | ||
f948dd76 DC |
246 | bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0, |
247 | &xfs_symlink_buf_ops); | |
19de7351 DC |
248 | if (!bp) |
249 | return XFS_ERROR(ENOMEM); | |
250 | error = bp->b_error; | |
251 | if (error) { | |
252 | xfs_buf_ioerror_alert(bp, __func__); | |
253 | xfs_buf_relse(bp); | |
254 | goto out; | |
255 | } | |
f948dd76 | 256 | byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt); |
19de7351 DC |
257 | if (pathlen < byte_cnt) |
258 | byte_cnt = pathlen; | |
f948dd76 DC |
259 | |
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, | |
263 | byte_cnt, bp)) { | |
264 | error = EFSCORRUPTED; | |
265 | xfs_alert(mp, | |
266 | "symlink header does not match required off/len/owner (0x%x/Ox%x,0x%llx)", | |
267 | offset, byte_cnt, ip->i_ino); | |
268 | xfs_buf_relse(bp); | |
269 | goto out; | |
270 | ||
271 | } | |
272 | ||
273 | cur_chunk += sizeof(struct xfs_dsymlink_hdr); | |
274 | } | |
275 | ||
276 | memcpy(link + offset, bp->b_addr, byte_cnt); | |
277 | ||
19de7351 | 278 | pathlen -= byte_cnt; |
f948dd76 | 279 | offset += byte_cnt; |
19de7351 | 280 | |
19de7351 DC |
281 | xfs_buf_relse(bp); |
282 | } | |
f948dd76 | 283 | ASSERT(pathlen == 0); |
19de7351 DC |
284 | |
285 | link[ip->i_d.di_size] = '\0'; | |
286 | error = 0; | |
287 | ||
288 | out: | |
289 | return error; | |
290 | } | |
291 | ||
292 | int | |
293 | xfs_readlink( | |
f948dd76 | 294 | struct xfs_inode *ip, |
19de7351 DC |
295 | char *link) |
296 | { | |
f948dd76 | 297 | struct xfs_mount *mp = ip->i_mount; |
19de7351 DC |
298 | xfs_fsize_t pathlen; |
299 | int error = 0; | |
300 | ||
301 | trace_xfs_readlink(ip); | |
302 | ||
303 | if (XFS_FORCED_SHUTDOWN(mp)) | |
304 | return XFS_ERROR(EIO); | |
305 | ||
306 | xfs_ilock(ip, XFS_ILOCK_SHARED); | |
307 | ||
308 | pathlen = ip->i_d.di_size; | |
309 | if (!pathlen) | |
310 | goto out; | |
311 | ||
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); | |
316 | ASSERT(0); | |
317 | error = XFS_ERROR(EFSCORRUPTED); | |
318 | goto out; | |
319 | } | |
320 | ||
321 | ||
322 | if (ip->i_df.if_flags & XFS_IFINLINE) { | |
323 | memcpy(link, ip->i_df.if_u1.if_data, pathlen); | |
324 | link[pathlen] = '\0'; | |
325 | } else { | |
326 | error = xfs_readlink_bmap(ip, link); | |
327 | } | |
328 | ||
329 | out: | |
330 | xfs_iunlock(ip, XFS_ILOCK_SHARED); | |
331 | return error; | |
332 | } | |
333 | ||
334 | int | |
335 | xfs_symlink( | |
f948dd76 | 336 | struct xfs_inode *dp, |
19de7351 DC |
337 | struct xfs_name *link_name, |
338 | const char *target_path, | |
339 | umode_t mode, | |
f948dd76 | 340 | struct xfs_inode **ipp) |
19de7351 | 341 | { |
f948dd76 DC |
342 | struct xfs_mount *mp = dp->i_mount; |
343 | struct xfs_trans *tp = NULL; | |
344 | struct xfs_inode *ip = NULL; | |
345 | int error = 0; | |
19de7351 | 346 | int pathlen; |
f948dd76 | 347 | struct xfs_bmap_free free_list; |
19de7351 | 348 | xfs_fsblock_t first_block; |
f948dd76 | 349 | bool unlock_dp_on_error = false; |
19de7351 DC |
350 | uint cancel_flags; |
351 | int committed; | |
352 | xfs_fileoff_t first_fsb; | |
353 | xfs_filblks_t fs_blocks; | |
354 | int nmaps; | |
f948dd76 | 355 | struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS]; |
19de7351 DC |
356 | xfs_daddr_t d; |
357 | const char *cur_chunk; | |
358 | int byte_cnt; | |
359 | int n; | |
360 | xfs_buf_t *bp; | |
361 | prid_t prid; | |
113a5683 CS |
362 | struct xfs_dquot *udqp = NULL; |
363 | struct xfs_dquot *gdqp = NULL; | |
92f8ff73 | 364 | struct xfs_dquot *pdqp = NULL; |
19de7351 DC |
365 | uint resblks; |
366 | ||
367 | *ipp = NULL; | |
19de7351 DC |
368 | |
369 | trace_xfs_symlink(dp, link_name); | |
370 | ||
371 | if (XFS_FORCED_SHUTDOWN(mp)) | |
372 | return XFS_ERROR(EIO); | |
373 | ||
374 | /* | |
375 | * Check component lengths of the target path name. | |
376 | */ | |
377 | pathlen = strlen(target_path); | |
378 | if (pathlen >= MAXPATHLEN) /* total string too long */ | |
379 | return XFS_ERROR(ENAMETOOLONG); | |
380 | ||
381 | udqp = gdqp = NULL; | |
382 | if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) | |
383 | prid = xfs_get_projid(dp); | |
384 | else | |
385 | prid = XFS_PROJID_DEFAULT; | |
386 | ||
387 | /* | |
388 | * Make sure that we have allocated dquot(s) on disk. | |
389 | */ | |
390 | error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid, | |
92f8ff73 | 391 | XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp, &pdqp); |
19de7351 DC |
392 | if (error) |
393 | goto std_return; | |
394 | ||
395 | tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK); | |
396 | cancel_flags = XFS_TRANS_RELEASE_LOG_RES; | |
397 | /* | |
398 | * The symlink will fit into the inode data fork? | |
399 | * There can't be any attributes so we get the whole variable part. | |
400 | */ | |
401 | if (pathlen <= XFS_LITINO(mp, dp->i_d.di_version)) | |
402 | fs_blocks = 0; | |
403 | else | |
321a9583 | 404 | fs_blocks = xfs_symlink_blocks(mp, pathlen); |
19de7351 DC |
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) { | |
409 | resblks = 0; | |
410 | error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0, | |
411 | XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT); | |
412 | } | |
413 | if (error) { | |
414 | cancel_flags = 0; | |
415 | goto error_return; | |
416 | } | |
417 | ||
418 | xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT); | |
419 | unlock_dp_on_error = true; | |
420 | ||
421 | /* | |
422 | * Check whether the directory allows new symlinks or not. | |
423 | */ | |
424 | if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) { | |
425 | error = XFS_ERROR(EPERM); | |
426 | goto error_return; | |
427 | } | |
428 | ||
429 | /* | |
430 | * Reserve disk quota : blocks and inode. | |
431 | */ | |
92f8ff73 CS |
432 | error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, |
433 | pdqp, resblks, 1, 0); | |
19de7351 DC |
434 | if (error) |
435 | goto error_return; | |
436 | ||
437 | /* | |
438 | * Check for ability to enter directory entry, if no space reserved. | |
439 | */ | |
440 | error = xfs_dir_canenter(tp, dp, link_name, resblks); | |
441 | if (error) | |
442 | goto error_return; | |
443 | /* | |
444 | * Initialize the bmap freelist prior to calling either | |
445 | * bmapi or the directory create code. | |
446 | */ | |
447 | xfs_bmap_init(&free_list, &first_block); | |
448 | ||
449 | /* | |
450 | * Allocate an inode for the symlink. | |
451 | */ | |
452 | error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT), 1, 0, | |
453 | prid, resblks > 0, &ip, NULL); | |
454 | if (error) { | |
455 | if (error == ENOSPC) | |
456 | goto error_return; | |
457 | goto error1; | |
458 | } | |
459 | ||
460 | /* | |
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 | |
463 | * error path. | |
464 | */ | |
465 | xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL); | |
466 | unlock_dp_on_error = false; | |
467 | ||
468 | /* | |
469 | * Also attach the dquot(s) to it, if applicable. | |
470 | */ | |
92f8ff73 | 471 | xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp); |
19de7351 DC |
472 | |
473 | if (resblks) | |
474 | resblks -= XFS_IALLOC_SPACE_RES(mp); | |
475 | /* | |
476 | * If the symlink will fit into the inode, write it inline. | |
477 | */ | |
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; | |
482 | ||
483 | /* | |
484 | * The inode was initially created in extent format. | |
485 | */ | |
486 | ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT); | |
487 | ip->i_df.if_flags |= XFS_IFINLINE; | |
488 | ||
489 | ip->i_d.di_format = XFS_DINODE_FMT_LOCAL; | |
490 | xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE); | |
491 | ||
492 | } else { | |
f948dd76 DC |
493 | int offset; |
494 | ||
19de7351 DC |
495 | first_fsb = 0; |
496 | nmaps = XFS_SYMLINK_MAPS; | |
497 | ||
498 | error = xfs_bmapi_write(tp, ip, first_fsb, fs_blocks, | |
499 | XFS_BMAPI_METADATA, &first_block, resblks, | |
500 | mval, &nmaps, &free_list); | |
501 | if (error) | |
502 | goto error2; | |
503 | ||
504 | if (resblks) | |
505 | resblks -= fs_blocks; | |
506 | ip->i_d.di_size = pathlen; | |
507 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | |
508 | ||
509 | cur_chunk = target_path; | |
f948dd76 | 510 | offset = 0; |
19de7351 | 511 | for (n = 0; n < nmaps; n++) { |
321a9583 | 512 | char *buf; |
f948dd76 | 513 | |
19de7351 DC |
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, | |
517 | BTOBB(byte_cnt), 0); | |
518 | if (!bp) { | |
519 | error = ENOMEM; | |
520 | goto error2; | |
521 | } | |
f948dd76 DC |
522 | bp->b_ops = &xfs_symlink_buf_ops; |
523 | ||
524 | byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt); | |
321a9583 | 525 | byte_cnt = min(byte_cnt, pathlen); |
19de7351 | 526 | |
f948dd76 DC |
527 | buf = bp->b_addr; |
528 | buf += xfs_symlink_hdr_set(mp, ip->i_ino, offset, | |
529 | byte_cnt, bp); | |
530 | ||
531 | memcpy(buf, cur_chunk, byte_cnt); | |
532 | ||
19de7351 | 533 | cur_chunk += byte_cnt; |
f948dd76 DC |
534 | pathlen -= byte_cnt; |
535 | offset += byte_cnt; | |
19de7351 | 536 | |
f948dd76 DC |
537 | xfs_trans_log_buf(tp, bp, 0, (buf + byte_cnt - 1) - |
538 | (char *)bp->b_addr); | |
19de7351 | 539 | } |
321a9583 | 540 | ASSERT(pathlen == 0); |
19de7351 DC |
541 | } |
542 | ||
543 | /* | |
544 | * Create the directory entry for the symlink. | |
545 | */ | |
546 | error = xfs_dir_createname(tp, dp, link_name, ip->i_ino, | |
547 | &first_block, &free_list, resblks); | |
548 | if (error) | |
549 | goto error2; | |
550 | xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); | |
551 | xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); | |
552 | ||
553 | /* | |
554 | * If this is a synchronous mount, make sure that the | |
555 | * symlink transaction goes to disk before returning to | |
556 | * the user. | |
557 | */ | |
558 | if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) { | |
559 | xfs_trans_set_sync(tp); | |
560 | } | |
561 | ||
562 | error = xfs_bmap_finish(&tp, &free_list, &committed); | |
563 | if (error) { | |
564 | goto error2; | |
565 | } | |
566 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); | |
567 | xfs_qm_dqrele(udqp); | |
568 | xfs_qm_dqrele(gdqp); | |
92f8ff73 | 569 | xfs_qm_dqrele(pdqp); |
19de7351 DC |
570 | |
571 | *ipp = ip; | |
572 | return 0; | |
573 | ||
574 | error2: | |
575 | IRELE(ip); | |
576 | error1: | |
577 | xfs_bmap_cancel(&free_list); | |
578 | cancel_flags |= XFS_TRANS_ABORT; | |
579 | error_return: | |
580 | xfs_trans_cancel(tp, cancel_flags); | |
581 | xfs_qm_dqrele(udqp); | |
582 | xfs_qm_dqrele(gdqp); | |
92f8ff73 | 583 | xfs_qm_dqrele(pdqp); |
19de7351 DC |
584 | |
585 | if (unlock_dp_on_error) | |
586 | xfs_iunlock(dp, XFS_ILOCK_EXCL); | |
587 | std_return: | |
588 | return error; | |
589 | } | |
590 | ||
591 | /* | |
592 | * Free a symlink that has blocks associated with it. | |
593 | */ | |
725eb1eb | 594 | STATIC int |
19de7351 DC |
595 | xfs_inactive_symlink_rmt( |
596 | xfs_inode_t *ip, | |
597 | xfs_trans_t **tpp) | |
598 | { | |
599 | xfs_buf_t *bp; | |
600 | int committed; | |
601 | int done; | |
602 | int error; | |
603 | xfs_fsblock_t first_block; | |
604 | xfs_bmap_free_t free_list; | |
605 | int i; | |
606 | xfs_mount_t *mp; | |
607 | xfs_bmbt_irec_t mval[XFS_SYMLINK_MAPS]; | |
608 | int nmaps; | |
609 | xfs_trans_t *ntp; | |
610 | int size; | |
611 | xfs_trans_t *tp; | |
612 | ||
613 | tp = *tpp; | |
614 | mp = ip->i_mount; | |
725eb1eb | 615 | ASSERT(ip->i_df.if_flags & XFS_IFEXTENTS); |
19de7351 DC |
616 | /* |
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. | |
622 | */ | |
623 | ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2); | |
624 | ||
625 | /* | |
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. | |
630 | */ | |
631 | size = (int)ip->i_d.di_size; | |
632 | ip->i_d.di_size = 0; | |
633 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | |
634 | /* | |
635 | * Find the block(s) so we can inval and unmap them. | |
636 | */ | |
637 | done = 0; | |
638 | xfs_bmap_init(&free_list, &first_block); | |
639 | nmaps = ARRAY_SIZE(mval); | |
f948dd76 | 640 | error = xfs_bmapi_read(ip, 0, xfs_symlink_blocks(mp, size), |
19de7351 DC |
641 | mval, &nmaps, 0); |
642 | if (error) | |
643 | goto error0; | |
644 | /* | |
f948dd76 | 645 | * Invalidate the block(s). No validation is done. |
19de7351 DC |
646 | */ |
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); | |
651 | if (!bp) { | |
652 | error = ENOMEM; | |
653 | goto error1; | |
654 | } | |
655 | xfs_trans_binval(tp, bp); | |
656 | } | |
657 | /* | |
658 | * Unmap the dead block(s) to the free_list. | |
659 | */ | |
660 | if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps, | |
661 | &first_block, &free_list, &done))) | |
662 | goto error1; | |
663 | ASSERT(done); | |
664 | /* | |
665 | * Commit the first transaction. This logs the EFI and the inode. | |
666 | */ | |
667 | if ((error = xfs_bmap_finish(&tp, &free_list, &committed))) | |
668 | goto error1; | |
669 | /* | |
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. | |
673 | */ | |
674 | ASSERT(committed); | |
675 | /* | |
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. | |
679 | */ | |
680 | xfs_trans_ijoin(tp, ip, 0); | |
681 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | |
682 | /* | |
683 | * Get a new, empty transaction to return to our caller. | |
684 | */ | |
685 | ntp = xfs_trans_dup(tp); | |
686 | /* | |
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. | |
691 | */ | |
692 | error = xfs_trans_commit(tp, 0); | |
693 | tp = ntp; | |
694 | if (error) { | |
695 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); | |
696 | goto error0; | |
697 | } | |
698 | /* | |
699 | * transaction commit worked ok so we can drop the extra ticket | |
700 | * reference that we gained in xfs_trans_dup() | |
701 | */ | |
702 | xfs_log_ticket_put(tp->t_ticket); | |
703 | ||
704 | /* | |
705 | * Remove the memory for extent descriptions (just bookkeeping). | |
706 | */ | |
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); | |
710 | /* | |
711 | * Put an itruncate log reservation in the new transaction | |
712 | * for our caller. | |
713 | */ | |
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)); | |
717 | goto error0; | |
718 | } | |
719 | ||
720 | xfs_trans_ijoin(tp, ip, 0); | |
721 | *tpp = tp; | |
722 | return 0; | |
723 | ||
724 | error1: | |
725 | xfs_bmap_cancel(&free_list); | |
726 | error0: | |
727 | return error; | |
728 | } | |
725eb1eb MT |
729 | |
730 | /* | |
731 | * xfs_inactive_symlink - free a symlink | |
732 | */ | |
733 | int | |
734 | xfs_inactive_symlink( | |
735 | struct xfs_inode *ip, | |
736 | struct xfs_trans **tp) | |
737 | { | |
738 | struct xfs_mount *mp = ip->i_mount; | |
739 | int pathlen; | |
740 | ||
741 | trace_xfs_inactive_symlink(ip); | |
742 | ||
743 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); | |
744 | ||
745 | if (XFS_FORCED_SHUTDOWN(mp)) | |
746 | return XFS_ERROR(EIO); | |
747 | ||
748 | /* | |
749 | * Zero length symlinks _can_ exist. | |
750 | */ | |
751 | pathlen = (int)ip->i_d.di_size; | |
752 | if (!pathlen) | |
753 | return 0; | |
754 | ||
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); | |
758 | ASSERT(0); | |
759 | return XFS_ERROR(EFSCORRUPTED); | |
760 | } | |
761 | ||
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), | |
765 | XFS_DATA_FORK); | |
766 | ASSERT(ip->i_df.if_bytes == 0); | |
767 | return 0; | |
768 | } | |
769 | ||
770 | /* remove the remote symlink */ | |
771 | return xfs_inactive_symlink_rmt(ip, tp); | |
772 | } |