]> git.ipfire.org Git - people/ms/linux.git/blame - fs/xfs/xfs_symlink.c
xfs: remove xfs_bmapi_write() firstblock param
[people/ms/linux.git] / fs / xfs / xfs_symlink.c
CommitLineData
0b61f8a4 1// SPDX-License-Identifier: GPL-2.0
19de7351
DC
2/*
3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4 * Copyright (c) 2012-2013 Red Hat, Inc.
5 * All rights reserved.
19de7351
DC
6 */
7#include "xfs.h"
239880ef 8#include "xfs_shared.h"
19de7351 9#include "xfs_fs.h"
6ca1c906 10#include "xfs_format.h"
239880ef
DC
11#include "xfs_log_format.h"
12#include "xfs_trans_resv.h"
19de7351 13#include "xfs_bit.h"
19de7351 14#include "xfs_mount.h"
57062787 15#include "xfs_da_format.h"
d6cf1305 16#include "xfs_da_btree.h"
3ab78df2 17#include "xfs_defer.h"
2b9ab5ab 18#include "xfs_dir2.h"
19de7351 19#include "xfs_inode.h"
19de7351
DC
20#include "xfs_ialloc.h"
21#include "xfs_alloc.h"
22#include "xfs_bmap.h"
a4fbe6ab 23#include "xfs_bmap_btree.h"
68988114 24#include "xfs_bmap_util.h"
19de7351
DC
25#include "xfs_error.h"
26#include "xfs_quota.h"
19de7351 27#include "xfs_trans_space.h"
19de7351
DC
28#include "xfs_trace.h"
29#include "xfs_symlink.h"
239880ef 30#include "xfs_trans.h"
239880ef 31#include "xfs_log.h"
19de7351
DC
32
33/* ----- Kernel only functions below ----- */
5da8f2f8
DW
34int
35xfs_readlink_bmap_ilocked(
f948dd76
DC
36 struct xfs_inode *ip,
37 char *link)
19de7351 38{
f948dd76
DC
39 struct xfs_mount *mp = ip->i_mount;
40 struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS];
41 struct xfs_buf *bp;
42 xfs_daddr_t d;
43 char *cur_chunk;
44 int pathlen = ip->i_d.di_size;
45 int nmaps = XFS_SYMLINK_MAPS;
46 int byte_cnt;
47 int n;
48 int error = 0;
49 int fsblocks = 0;
50 int offset;
19de7351 51
29db2500
CH
52 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
53
f948dd76
DC
54 fsblocks = xfs_symlink_blocks(mp, pathlen);
55 error = xfs_bmapi_read(ip, 0, fsblocks, mval, &nmaps, 0);
19de7351
DC
56 if (error)
57 goto out;
58
f948dd76 59 offset = 0;
19de7351
DC
60 for (n = 0; n < nmaps; n++) {
61 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
62 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
63
f948dd76
DC
64 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0,
65 &xfs_symlink_buf_ops);
19de7351 66 if (!bp)
2451337d 67 return -ENOMEM;
19de7351
DC
68 error = bp->b_error;
69 if (error) {
70 xfs_buf_ioerror_alert(bp, __func__);
71 xfs_buf_relse(bp);
ac75a1f7
DC
72
73 /* bad CRC means corrupted metadata */
2451337d
DC
74 if (error == -EFSBADCRC)
75 error = -EFSCORRUPTED;
19de7351
DC
76 goto out;
77 }
f948dd76 78 byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
19de7351
DC
79 if (pathlen < byte_cnt)
80 byte_cnt = pathlen;
f948dd76
DC
81
82 cur_chunk = bp->b_addr;
83 if (xfs_sb_version_hascrc(&mp->m_sb)) {
bda65ef8 84 if (!xfs_symlink_hdr_ok(ip->i_ino, offset,
f948dd76 85 byte_cnt, bp)) {
2451337d 86 error = -EFSCORRUPTED;
f948dd76
DC
87 xfs_alert(mp,
88"symlink header does not match required off/len/owner (0x%x/Ox%x,0x%llx)",
89 offset, byte_cnt, ip->i_ino);
90 xfs_buf_relse(bp);
91 goto out;
92
93 }
94
95 cur_chunk += sizeof(struct xfs_dsymlink_hdr);
96 }
97
2ac56d3d 98 memcpy(link + offset, cur_chunk, byte_cnt);
f948dd76 99
19de7351 100 pathlen -= byte_cnt;
f948dd76 101 offset += byte_cnt;
19de7351 102
19de7351
DC
103 xfs_buf_relse(bp);
104 }
f948dd76 105 ASSERT(pathlen == 0);
19de7351
DC
106
107 link[ip->i_d.di_size] = '\0';
108 error = 0;
109
110 out:
111 return error;
112}
113
114int
115xfs_readlink(
f948dd76 116 struct xfs_inode *ip,
19de7351
DC
117 char *link)
118{
f948dd76 119 struct xfs_mount *mp = ip->i_mount;
19de7351
DC
120 xfs_fsize_t pathlen;
121 int error = 0;
122
123 trace_xfs_readlink(ip);
124
30ee052e
CH
125 ASSERT(!(ip->i_df.if_flags & XFS_IFINLINE));
126
19de7351 127 if (XFS_FORCED_SHUTDOWN(mp))
2451337d 128 return -EIO;
19de7351
DC
129
130 xfs_ilock(ip, XFS_ILOCK_SHARED);
131
132 pathlen = ip->i_d.di_size;
133 if (!pathlen)
134 goto out;
135
6eb0b8df 136 if (pathlen < 0 || pathlen > XFS_SYMLINK_MAXLEN) {
19de7351
DC
137 xfs_alert(mp, "%s: inode (%llu) bad symlink length (%lld)",
138 __func__, (unsigned long long) ip->i_ino,
139 (long long) pathlen);
140 ASSERT(0);
2451337d 141 error = -EFSCORRUPTED;
19de7351
DC
142 goto out;
143 }
144
145
5da8f2f8 146 error = xfs_readlink_bmap_ilocked(ip, link);
19de7351
DC
147
148 out:
149 xfs_iunlock(ip, XFS_ILOCK_SHARED);
150 return error;
151}
152
153int
154xfs_symlink(
f948dd76 155 struct xfs_inode *dp,
19de7351
DC
156 struct xfs_name *link_name,
157 const char *target_path,
158 umode_t mode,
f948dd76 159 struct xfs_inode **ipp)
19de7351 160{
f948dd76
DC
161 struct xfs_mount *mp = dp->i_mount;
162 struct xfs_trans *tp = NULL;
163 struct xfs_inode *ip = NULL;
164 int error = 0;
19de7351 165 int pathlen;
2c3234d1 166 struct xfs_defer_ops dfops;
58c90473 167 bool unlock_dp_on_error = false;
19de7351
DC
168 xfs_fileoff_t first_fsb;
169 xfs_filblks_t fs_blocks;
170 int nmaps;
f948dd76 171 struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS];
19de7351
DC
172 xfs_daddr_t d;
173 const char *cur_chunk;
174 int byte_cnt;
175 int n;
176 xfs_buf_t *bp;
177 prid_t prid;
113a5683
CS
178 struct xfs_dquot *udqp = NULL;
179 struct xfs_dquot *gdqp = NULL;
92f8ff73 180 struct xfs_dquot *pdqp = NULL;
19de7351
DC
181 uint resblks;
182
183 *ipp = NULL;
19de7351
DC
184
185 trace_xfs_symlink(dp, link_name);
186
187 if (XFS_FORCED_SHUTDOWN(mp))
2451337d 188 return -EIO;
19de7351
DC
189
190 /*
191 * Check component lengths of the target path name.
192 */
193 pathlen = strlen(target_path);
6eb0b8df 194 if (pathlen >= XFS_SYMLINK_MAXLEN) /* total string too long */
2451337d 195 return -ENAMETOOLONG;
19de7351
DC
196
197 udqp = gdqp = NULL;
163467d3 198 prid = xfs_get_initial_prid(dp);
19de7351
DC
199
200 /*
201 * Make sure that we have allocated dquot(s) on disk.
202 */
7aab1b28
DE
203 error = xfs_qm_vop_dqalloc(dp,
204 xfs_kuid_to_uid(current_fsuid()),
205 xfs_kgid_to_gid(current_fsgid()), prid,
206 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
207 &udqp, &gdqp, &pdqp);
19de7351 208 if (error)
58c90473 209 return error;
19de7351 210
19de7351
DC
211 /*
212 * The symlink will fit into the inode data fork?
213 * There can't be any attributes so we get the whole variable part.
214 */
215 if (pathlen <= XFS_LITINO(mp, dp->i_d.di_version))
216 fs_blocks = 0;
217 else
321a9583 218 fs_blocks = xfs_symlink_blocks(mp, pathlen);
19de7351 219 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
253f4911
CH
220
221 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_symlink, resblks, 0, 0, &tp);
4906e215 222 if (error)
253f4911 223 goto out_release_inode;
19de7351 224
65523218 225 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
19de7351
DC
226 unlock_dp_on_error = true;
227
228 /*
229 * Check whether the directory allows new symlinks or not.
230 */
231 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
2451337d 232 error = -EPERM;
58c90473 233 goto out_trans_cancel;
19de7351
DC
234 }
235
236 /*
237 * Reserve disk quota : blocks and inode.
238 */
92f8ff73
CS
239 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
240 pdqp, resblks, 1, 0);
19de7351 241 if (error)
58c90473 242 goto out_trans_cancel;
19de7351 243
19de7351
DC
244 /*
245 * Initialize the bmap freelist prior to calling either
246 * bmapi or the directory create code.
247 */
f16dea54 248 xfs_defer_init(tp, &dfops, &tp->t_firstblock);
19de7351
DC
249
250 /*
251 * Allocate an inode for the symlink.
252 */
253 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT), 1, 0,
c959025e 254 prid, &ip);
58c90473
DC
255 if (error)
256 goto out_trans_cancel;
19de7351
DC
257
258 /*
58c90473
DC
259 * Now we join the directory inode to the transaction. We do not do it
260 * earlier because xfs_dir_ialloc might commit the previous transaction
261 * (and release all the locks). An error from here on will result in
262 * the transaction cancel unlocking dp so don't do it explicitly in the
19de7351
DC
263 * error path.
264 */
65523218 265 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
19de7351
DC
266 unlock_dp_on_error = false;
267
268 /*
269 * Also attach the dquot(s) to it, if applicable.
270 */
92f8ff73 271 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
19de7351
DC
272
273 if (resblks)
274 resblks -= XFS_IALLOC_SPACE_RES(mp);
275 /*
276 * If the symlink will fit into the inode, write it inline.
277 */
278 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
143f4aed 279 xfs_init_local_fork(ip, XFS_DATA_FORK, target_path, pathlen);
19de7351 280
143f4aed 281 ip->i_d.di_size = pathlen;
19de7351
DC
282 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
283 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
19de7351 284 } else {
f948dd76
DC
285 int offset;
286
19de7351
DC
287 first_fsb = 0;
288 nmaps = XFS_SYMLINK_MAPS;
289
290 error = xfs_bmapi_write(tp, ip, first_fsb, fs_blocks,
a7beabea 291 XFS_BMAPI_METADATA, resblks, mval, &nmaps);
19de7351 292 if (error)
58c90473 293 goto out_bmap_cancel;
19de7351
DC
294
295 if (resblks)
296 resblks -= fs_blocks;
297 ip->i_d.di_size = pathlen;
298 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
299
300 cur_chunk = target_path;
f948dd76 301 offset = 0;
19de7351 302 for (n = 0; n < nmaps; n++) {
321a9583 303 char *buf;
f948dd76 304
19de7351
DC
305 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
306 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
307 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
308 BTOBB(byte_cnt), 0);
309 if (!bp) {
2451337d 310 error = -ENOMEM;
58c90473 311 goto out_bmap_cancel;
19de7351 312 }
f948dd76
DC
313 bp->b_ops = &xfs_symlink_buf_ops;
314
315 byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
321a9583 316 byte_cnt = min(byte_cnt, pathlen);
19de7351 317
f948dd76
DC
318 buf = bp->b_addr;
319 buf += xfs_symlink_hdr_set(mp, ip->i_ino, offset,
320 byte_cnt, bp);
321
322 memcpy(buf, cur_chunk, byte_cnt);
323
19de7351 324 cur_chunk += byte_cnt;
f948dd76
DC
325 pathlen -= byte_cnt;
326 offset += byte_cnt;
19de7351 327
daf7b799 328 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SYMLINK_BUF);
f948dd76
DC
329 xfs_trans_log_buf(tp, bp, 0, (buf + byte_cnt - 1) -
330 (char *)bp->b_addr);
19de7351 331 }
321a9583 332 ASSERT(pathlen == 0);
19de7351
DC
333 }
334
335 /*
336 * Create the directory entry for the symlink.
337 */
381eee69 338 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino, resblks);
19de7351 339 if (error)
58c90473 340 goto out_bmap_cancel;
19de7351
DC
341 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
342 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
343
344 /*
345 * If this is a synchronous mount, make sure that the
346 * symlink transaction goes to disk before returning to
347 * the user.
348 */
349 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
350 xfs_trans_set_sync(tp);
351 }
352
175d1a01 353 error = xfs_defer_finish(&tp, tp->t_dfops);
58c90473
DC
354 if (error)
355 goto out_bmap_cancel;
356
70393313 357 error = xfs_trans_commit(tp);
58c90473
DC
358 if (error)
359 goto out_release_inode;
360
19de7351
DC
361 xfs_qm_dqrele(udqp);
362 xfs_qm_dqrele(gdqp);
92f8ff73 363 xfs_qm_dqrele(pdqp);
19de7351
DC
364
365 *ipp = ip;
366 return 0;
367
58c90473 368out_bmap_cancel:
175d1a01 369 xfs_defer_cancel(tp->t_dfops);
58c90473 370out_trans_cancel:
4906e215 371 xfs_trans_cancel(tp);
58c90473
DC
372out_release_inode:
373 /*
374 * Wait until after the current transaction is aborted to finish the
375 * setup of the inode and release the inode. This prevents recursive
376 * transactions and deadlocks from xfs_inactive.
377 */
378 if (ip) {
379 xfs_finish_inode_setup(ip);
380 IRELE(ip);
381 }
382
19de7351
DC
383 xfs_qm_dqrele(udqp);
384 xfs_qm_dqrele(gdqp);
92f8ff73 385 xfs_qm_dqrele(pdqp);
19de7351
DC
386
387 if (unlock_dp_on_error)
65523218 388 xfs_iunlock(dp, XFS_ILOCK_EXCL);
19de7351
DC
389 return error;
390}
391
392/*
393 * Free a symlink that has blocks associated with it.
394 */
725eb1eb 395STATIC int
19de7351 396xfs_inactive_symlink_rmt(
36b21dde 397 struct xfs_inode *ip)
19de7351
DC
398{
399 xfs_buf_t *bp;
19de7351
DC
400 int done;
401 int error;
2c3234d1 402 struct xfs_defer_ops dfops;
19de7351
DC
403 int i;
404 xfs_mount_t *mp;
405 xfs_bmbt_irec_t mval[XFS_SYMLINK_MAPS];
406 int nmaps;
19de7351
DC
407 int size;
408 xfs_trans_t *tp;
409
19de7351 410 mp = ip->i_mount;
725eb1eb 411 ASSERT(ip->i_df.if_flags & XFS_IFEXTENTS);
19de7351
DC
412 /*
413 * We're freeing a symlink that has some
414 * blocks allocated to it. Free the
415 * blocks here. We know that we've got
416 * either 1 or 2 extents and that we can
417 * free them all in one bunmapi call.
418 */
419 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
420
253f4911
CH
421 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
422 if (error)
36b21dde 423 return error;
36b21dde
BF
424
425 xfs_ilock(ip, XFS_ILOCK_EXCL);
426 xfs_trans_ijoin(tp, ip, 0);
427
19de7351
DC
428 /*
429 * Lock the inode, fix the size, and join it to the transaction.
430 * Hold it so in the normal path, we still have it locked for
431 * the second transaction. In the error paths we need it
432 * held so the cancel won't rele it, see below.
433 */
434 size = (int)ip->i_d.di_size;
435 ip->i_d.di_size = 0;
436 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
437 /*
438 * Find the block(s) so we can inval and unmap them.
439 */
440 done = 0;
37283797 441 xfs_defer_init(tp, &dfops, &tp->t_firstblock);
19de7351 442 nmaps = ARRAY_SIZE(mval);
f948dd76 443 error = xfs_bmapi_read(ip, 0, xfs_symlink_blocks(mp, size),
19de7351
DC
444 mval, &nmaps, 0);
445 if (error)
36b21dde 446 goto error_trans_cancel;
19de7351 447 /*
f948dd76 448 * Invalidate the block(s). No validation is done.
19de7351
DC
449 */
450 for (i = 0; i < nmaps; i++) {
451 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
452 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
453 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
454 if (!bp) {
2451337d 455 error = -ENOMEM;
36b21dde 456 goto error_bmap_cancel;
19de7351
DC
457 }
458 xfs_trans_binval(tp, bp);
459 }
460 /*
2c3234d1 461 * Unmap the dead block(s) to the dfops.
19de7351 462 */
37283797
BF
463 error = xfs_bunmapi(tp, ip, 0, size, 0, nmaps, &tp->t_firstblock,
464 &done);
36b21dde
BF
465 if (error)
466 goto error_bmap_cancel;
19de7351
DC
467 ASSERT(done);
468 /*
469 * Commit the first transaction. This logs the EFI and the inode.
470 */
4bcfa613
BF
471 xfs_defer_ijoin(tp->t_dfops, ip);
472 error = xfs_defer_finish(&tp, tp->t_dfops);
36b21dde
BF
473 if (error)
474 goto error_bmap_cancel;
3565b660 475
19de7351
DC
476 /*
477 * Commit the transaction containing extent freeing and EFDs.
19de7351 478 */
3565b660 479 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
70393313 480 error = xfs_trans_commit(tp);
19de7351
DC
481 if (error) {
482 ASSERT(XFS_FORCED_SHUTDOWN(mp));
36b21dde 483 goto error_unlock;
19de7351 484 }
19de7351
DC
485
486 /*
487 * Remove the memory for extent descriptions (just bookkeeping).
488 */
489 if (ip->i_df.if_bytes)
490 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
491 ASSERT(ip->i_df.if_bytes == 0);
19de7351 492
36b21dde 493 xfs_iunlock(ip, XFS_ILOCK_EXCL);
19de7351
DC
494 return 0;
495
36b21dde 496error_bmap_cancel:
4bcfa613 497 xfs_defer_cancel(tp->t_dfops);
36b21dde 498error_trans_cancel:
4906e215 499 xfs_trans_cancel(tp);
36b21dde
BF
500error_unlock:
501 xfs_iunlock(ip, XFS_ILOCK_EXCL);
19de7351
DC
502 return error;
503}
725eb1eb
MT
504
505/*
506 * xfs_inactive_symlink - free a symlink
507 */
508int
509xfs_inactive_symlink(
36b21dde 510 struct xfs_inode *ip)
725eb1eb
MT
511{
512 struct xfs_mount *mp = ip->i_mount;
513 int pathlen;
514
515 trace_xfs_inactive_symlink(ip);
516
725eb1eb 517 if (XFS_FORCED_SHUTDOWN(mp))
2451337d 518 return -EIO;
725eb1eb 519
36b21dde
BF
520 xfs_ilock(ip, XFS_ILOCK_EXCL);
521
725eb1eb
MT
522 /*
523 * Zero length symlinks _can_ exist.
524 */
525 pathlen = (int)ip->i_d.di_size;
36b21dde
BF
526 if (!pathlen) {
527 xfs_iunlock(ip, XFS_ILOCK_EXCL);
725eb1eb 528 return 0;
36b21dde 529 }
725eb1eb 530
6eb0b8df 531 if (pathlen < 0 || pathlen > XFS_SYMLINK_MAXLEN) {
725eb1eb
MT
532 xfs_alert(mp, "%s: inode (0x%llx) bad symlink length (%d)",
533 __func__, (unsigned long long)ip->i_ino, pathlen);
36b21dde 534 xfs_iunlock(ip, XFS_ILOCK_EXCL);
725eb1eb 535 ASSERT(0);
2451337d 536 return -EFSCORRUPTED;
725eb1eb
MT
537 }
538
539 if (ip->i_df.if_flags & XFS_IFINLINE) {
36b21dde 540 if (ip->i_df.if_bytes > 0)
725eb1eb
MT
541 xfs_idata_realloc(ip, -(ip->i_df.if_bytes),
542 XFS_DATA_FORK);
36b21dde 543 xfs_iunlock(ip, XFS_ILOCK_EXCL);
725eb1eb
MT
544 ASSERT(ip->i_df.if_bytes == 0);
545 return 0;
546 }
547
36b21dde
BF
548 xfs_iunlock(ip, XFS_ILOCK_EXCL);
549
725eb1eb 550 /* remove the remote symlink */
36b21dde 551 return xfs_inactive_symlink_rmt(ip);
725eb1eb 552}