xfs: remove xfs_inactive_attrs
[deliverable/linux.git] / fs / xfs / xfs_vnodeops.c
CommitLineData
1da177e4 1/*
3e57ecf6 2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
7b718769 3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
1da177e4 13 *
7b718769
NS
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1da177e4 17 */
16f7e0fe 18
1da177e4 19#include "xfs.h"
a844f451 20#include "xfs_fs.h"
1da177e4 21#include "xfs_types.h"
a844f451 22#include "xfs_bit.h"
1da177e4
LT
23#include "xfs_log.h"
24#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
1da177e4 27#include "xfs_dir2.h"
1da177e4 28#include "xfs_mount.h"
a844f451 29#include "xfs_da_btree.h"
1da177e4
LT
30#include "xfs_bmap_btree.h"
31#include "xfs_ialloc_btree.h"
1da177e4 32#include "xfs_dinode.h"
1da177e4 33#include "xfs_inode.h"
a844f451 34#include "xfs_inode_item.h"
a844f451 35#include "xfs_itable.h"
a844f451
NS
36#include "xfs_ialloc.h"
37#include "xfs_alloc.h"
1da177e4 38#include "xfs_bmap.h"
ef14f0c1 39#include "xfs_acl.h"
1da177e4 40#include "xfs_attr.h"
1da177e4 41#include "xfs_error.h"
1da177e4
LT
42#include "xfs_quota.h"
43#include "xfs_utils.h"
a844f451 44#include "xfs_rtalloc.h"
1da177e4 45#include "xfs_trans_space.h"
1da177e4 46#include "xfs_log_priv.h"
2a82b8be 47#include "xfs_filestream.h"
993386c1 48#include "xfs_vnodeops.h"
0b1b213f 49#include "xfs_trace.h"
1da177e4 50
7d4fb40a
NS
51/*
52 * The maximum pathlen is 1024 bytes. Since the minimum file system
53 * blocksize is 512 bytes, we can get a max of 2 extents back from
54 * bmapi.
55 */
56#define SYMLINK_MAPS 2
57
804c83c3
CH
58STATIC int
59xfs_readlink_bmap(
60 xfs_inode_t *ip,
61 char *link)
62{
63 xfs_mount_t *mp = ip->i_mount;
64 int pathlen = ip->i_d.di_size;
65 int nmaps = SYMLINK_MAPS;
66 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
67 xfs_daddr_t d;
68 int byte_cnt;
69 int n;
70 xfs_buf_t *bp;
71 int error = 0;
72
5c8ed202
DC
73 error = xfs_bmapi_read(ip, 0, XFS_B_TO_FSB(mp, pathlen), mval, &nmaps,
74 0);
804c83c3
CH
75 if (error)
76 goto out;
77
78 for (n = 0; n < nmaps; n++) {
79 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
80 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
81
611c9946 82 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0);
ac4d6888
CS
83 if (!bp)
84 return XFS_ERROR(ENOMEM);
e5702805 85 error = bp->b_error;
804c83c3 86 if (error) {
901796af 87 xfs_buf_ioerror_alert(bp, __func__);
804c83c3
CH
88 xfs_buf_relse(bp);
89 goto out;
90 }
91 if (pathlen < byte_cnt)
92 byte_cnt = pathlen;
93 pathlen -= byte_cnt;
94
62926044 95 memcpy(link, bp->b_addr, byte_cnt);
804c83c3
CH
96 xfs_buf_relse(bp);
97 }
98
99 link[ip->i_d.di_size] = '\0';
100 error = 0;
101
102 out:
103 return error;
104}
105
993386c1 106int
1da177e4 107xfs_readlink(
993386c1 108 xfs_inode_t *ip,
804c83c3 109 char *link)
1da177e4 110{
804c83c3 111 xfs_mount_t *mp = ip->i_mount;
b52a360b 112 xfs_fsize_t pathlen;
1da177e4 113 int error = 0;
1da177e4 114
cca28fb8 115 trace_xfs_readlink(ip);
1da177e4
LT
116
117 if (XFS_FORCED_SHUTDOWN(mp))
118 return XFS_ERROR(EIO);
119
120 xfs_ilock(ip, XFS_ILOCK_SHARED);
121
804c83c3
CH
122 pathlen = ip->i_d.di_size;
123 if (!pathlen)
124 goto out;
1da177e4 125
b52a360b
CM
126 if (pathlen < 0 || pathlen > MAXPATHLEN) {
127 xfs_alert(mp, "%s: inode (%llu) bad symlink length (%lld)",
128 __func__, (unsigned long long) ip->i_ino,
129 (long long) pathlen);
130 ASSERT(0);
9b025eb3
JK
131 error = XFS_ERROR(EFSCORRUPTED);
132 goto out;
b52a360b
CM
133 }
134
135
1da177e4 136 if (ip->i_df.if_flags & XFS_IFINLINE) {
804c83c3
CH
137 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
138 link[pathlen] = '\0';
139 } else {
140 error = xfs_readlink_bmap(ip, link);
1da177e4
LT
141 }
142
804c83c3 143 out:
1da177e4 144 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1da177e4
LT
145 return error;
146}
147
c56c9631
CH
148/*
149 * Flags for xfs_free_eofblocks
150 */
151#define XFS_FREE_EOF_TRYLOCK (1<<0)
152
1da177e4 153/*
92dfe8d2
DC
154 * This is called by xfs_inactive to free any blocks beyond eof
155 * when the link count isn't zero and by xfs_dm_punch_hole() when
156 * punching a hole to EOF.
1da177e4 157 */
d96f8f89 158STATIC int
92dfe8d2 159xfs_free_eofblocks(
1da177e4 160 xfs_mount_t *mp,
92dfe8d2
DC
161 xfs_inode_t *ip,
162 int flags)
1da177e4
LT
163{
164 xfs_trans_t *tp;
165 int error;
166 xfs_fileoff_t end_fsb;
167 xfs_fileoff_t last_fsb;
168 xfs_filblks_t map_len;
169 int nimaps;
170 xfs_bmbt_irec_t imap;
171
172 /*
173 * Figure out if there are any blocks beyond the end
174 * of the file. If not, then there is nothing to do.
175 */
ce7ae151 176 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_ISIZE(ip));
32972383 177 last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
3f34885c 178 if (last_fsb <= end_fsb)
014c2544 179 return 0;
3f34885c 180 map_len = last_fsb - end_fsb;
1da177e4
LT
181
182 nimaps = 1;
183 xfs_ilock(ip, XFS_ILOCK_SHARED);
5c8ed202 184 error = xfs_bmapi_read(ip, end_fsb, map_len, &imap, &nimaps, 0);
1da177e4
LT
185 xfs_iunlock(ip, XFS_ILOCK_SHARED);
186
187 if (!error && (nimaps != 0) &&
68bdb6ea
YL
188 (imap.br_startblock != HOLESTARTBLOCK ||
189 ip->i_delayed_blks)) {
1da177e4
LT
190 /*
191 * Attach the dquots to the inode up front.
192 */
7d095257
CH
193 error = xfs_qm_dqattach(ip, 0);
194 if (error)
014c2544 195 return error;
1da177e4
LT
196
197 /*
198 * There are blocks after the end of file.
199 * Free them up now by truncating the file to
200 * its current size.
201 */
202 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
203
c56c9631
CH
204 if (flags & XFS_FREE_EOF_TRYLOCK) {
205 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
206 xfs_trans_cancel(tp, 0);
207 return 0;
208 }
209 } else {
92dfe8d2 210 xfs_ilock(ip, XFS_IOLOCK_EXCL);
c56c9631 211 }
1da177e4
LT
212
213 error = xfs_trans_reserve(tp, 0,
214 XFS_ITRUNCATE_LOG_RES(mp),
215 0, XFS_TRANS_PERM_LOG_RES,
216 XFS_ITRUNCATE_LOG_COUNT);
217 if (error) {
218 ASSERT(XFS_FORCED_SHUTDOWN(mp));
219 xfs_trans_cancel(tp, 0);
220 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
014c2544 221 return error;
1da177e4
LT
222 }
223
224 xfs_ilock(ip, XFS_ILOCK_EXCL);
ddc3415a 225 xfs_trans_ijoin(tp, ip, 0);
1da177e4 226
673e8e59
CH
227 /*
228 * Do not update the on-disk file size. If we update the
229 * on-disk file size and then the system crashes before the
230 * contents of the file are flushed to disk then the files
231 * may be full of holes (ie NULL files bug).
232 */
233 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK,
ce7ae151 234 XFS_ISIZE(ip));
1da177e4 235 if (error) {
8f04c47a
CH
236 /*
237 * If we get an error at this point we simply don't
238 * bother truncating the file.
239 */
1da177e4
LT
240 xfs_trans_cancel(tp,
241 (XFS_TRANS_RELEASE_LOG_RES |
242 XFS_TRANS_ABORT));
243 } else {
244 error = xfs_trans_commit(tp,
1c72bf90 245 XFS_TRANS_RELEASE_LOG_RES);
1da177e4 246 }
c56c9631 247 xfs_iunlock(ip, XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL);
1da177e4 248 }
014c2544 249 return error;
1da177e4
LT
250}
251
252/*
253 * Free a symlink that has blocks associated with it.
254 */
255STATIC int
256xfs_inactive_symlink_rmt(
257 xfs_inode_t *ip,
258 xfs_trans_t **tpp)
259{
260 xfs_buf_t *bp;
261 int committed;
262 int done;
263 int error;
264 xfs_fsblock_t first_block;
265 xfs_bmap_free_t free_list;
266 int i;
267 xfs_mount_t *mp;
268 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
269 int nmaps;
270 xfs_trans_t *ntp;
271 int size;
272 xfs_trans_t *tp;
273
274 tp = *tpp;
275 mp = ip->i_mount;
276 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
277 /*
278 * We're freeing a symlink that has some
279 * blocks allocated to it. Free the
280 * blocks here. We know that we've got
281 * either 1 or 2 extents and that we can
282 * free them all in one bunmapi call.
283 */
284 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
b373e98d 285
1da177e4
LT
286 /*
287 * Lock the inode, fix the size, and join it to the transaction.
288 * Hold it so in the normal path, we still have it locked for
289 * the second transaction. In the error paths we need it
290 * held so the cancel won't rele it, see below.
291 */
1da177e4
LT
292 size = (int)ip->i_d.di_size;
293 ip->i_d.di_size = 0;
1da177e4
LT
294 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
295 /*
296 * Find the block(s) so we can inval and unmap them.
297 */
298 done = 0;
9d87c319 299 xfs_bmap_init(&free_list, &first_block);
e8c96f8c 300 nmaps = ARRAY_SIZE(mval);
5c8ed202
DC
301 error = xfs_bmapi_read(ip, 0, XFS_B_TO_FSB(mp, size),
302 mval, &nmaps, 0);
303 if (error)
1da177e4
LT
304 goto error0;
305 /*
306 * Invalidate the block(s).
307 */
308 for (i = 0; i < nmaps; i++) {
309 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
310 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
311 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
2a30f36d
CS
312 if (!bp) {
313 error = ENOMEM;
314 goto error1;
315 }
1da177e4
LT
316 xfs_trans_binval(tp, bp);
317 }
318 /*
319 * Unmap the dead block(s) to the free_list.
320 */
321 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
b4e9181e 322 &first_block, &free_list, &done)))
1da177e4
LT
323 goto error1;
324 ASSERT(done);
325 /*
326 * Commit the first transaction. This logs the EFI and the inode.
327 */
f7c99b6f 328 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
1da177e4
LT
329 goto error1;
330 /*
331 * The transaction must have been committed, since there were
332 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
333 * The new tp has the extent freeing and EFDs.
334 */
335 ASSERT(committed);
336 /*
337 * The first xact was committed, so add the inode to the new one.
338 * Mark it dirty so it will be logged and moved forward in the log as
339 * part of every commit.
340 */
ddc3415a 341 xfs_trans_ijoin(tp, ip, 0);
1da177e4
LT
342 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
343 /*
344 * Get a new, empty transaction to return to our caller.
345 */
346 ntp = xfs_trans_dup(tp);
347 /*
c41564b5 348 * Commit the transaction containing extent freeing and EFDs.
1da177e4
LT
349 * If we get an error on the commit here or on the reserve below,
350 * we need to unlock the inode since the new transaction doesn't
351 * have the inode attached.
352 */
1c72bf90 353 error = xfs_trans_commit(tp, 0);
1da177e4
LT
354 tp = ntp;
355 if (error) {
356 ASSERT(XFS_FORCED_SHUTDOWN(mp));
357 goto error0;
358 }
cc09c0dc
DC
359 /*
360 * transaction commit worked ok so we can drop the extra ticket
361 * reference that we gained in xfs_trans_dup()
362 */
363 xfs_log_ticket_put(tp->t_ticket);
364
1da177e4
LT
365 /*
366 * Remove the memory for extent descriptions (just bookkeeping).
367 */
368 if (ip->i_df.if_bytes)
369 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
370 ASSERT(ip->i_df.if_bytes == 0);
371 /*
372 * Put an itruncate log reservation in the new transaction
373 * for our caller.
374 */
375 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
376 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
377 ASSERT(XFS_FORCED_SHUTDOWN(mp));
378 goto error0;
379 }
b373e98d
CH
380
381 xfs_trans_ijoin(tp, ip, 0);
1da177e4
LT
382 *tpp = tp;
383 return 0;
384
385 error1:
386 xfs_bmap_cancel(&free_list);
387 error0:
1da177e4 388 return error;
1da177e4
LT
389}
390
993386c1 391int
1da177e4 392xfs_release(
993386c1 393 xfs_inode_t *ip)
1da177e4 394{
993386c1 395 xfs_mount_t *mp = ip->i_mount;
1da177e4
LT
396 int error;
397
42173f68 398 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
1da177e4 399 return 0;
1da177e4
LT
400
401 /* If this is a read-only mount, don't do this (would generate I/O) */
bd186aa9 402 if (mp->m_flags & XFS_MOUNT_RDONLY)
1da177e4
LT
403 return 0;
404
2a82b8be 405 if (!XFS_FORCED_SHUTDOWN(mp)) {
b3aea4ed
CH
406 int truncated;
407
2a82b8be
DC
408 /*
409 * If we are using filestreams, and we have an unlinked
410 * file that we are processing the last close on, then nothing
411 * will be able to reopen and write to this file. Purge this
412 * inode from the filestreams cache so that it doesn't delay
413 * teardown of the inode.
414 */
415 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
416 xfs_filestream_deassociate(ip);
417
fbf3ce8d
CH
418 /*
419 * If we previously truncated this file and removed old data
420 * in the process, we want to initiate "early" writeout on
421 * the last close. This is an attempt to combat the notorious
25985edc 422 * NULL files problem which is particularly noticeable from a
fbf3ce8d
CH
423 * truncate down, buffered (re-)write (delalloc), followed by
424 * a crash. What we are effectively doing here is
425 * significantly reducing the time window where we'd otherwise
426 * be exposed to that problem.
427 */
09262b43 428 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
df4368a1
DC
429 if (truncated) {
430 xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
431 if (VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0)
432 xfs_flush_pages(ip, 0, -1, XBF_ASYNC, FI_NONE);
433 }
fbf3ce8d
CH
434 }
435
6e857567
DC
436 if (ip->i_d.di_nlink == 0)
437 return 0;
c56c9631 438
abbede1b 439 if ((S_ISREG(ip->i_d.di_mode) &&
ce7ae151
CH
440 (VFS_I(ip)->i_size > 0 ||
441 (VN_CACHED(VFS_I(ip)) > 0 || ip->i_delayed_blks > 0)) &&
6e857567
DC
442 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
443 (!(ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
444
445 /*
446 * If we can't get the iolock just skip truncating the blocks
447 * past EOF because we could deadlock with the mmap_sem
448 * otherwise. We'll get another chance to drop them once the
449 * last reference to the inode is dropped, so we'll never leak
450 * blocks permanently.
451 *
452 * Further, check if the inode is being opened, written and
453 * closed frequently and we have delayed allocation blocks
25985edc 454 * outstanding (e.g. streaming writes from the NFS server),
6e857567
DC
455 * truncating the blocks past EOF will cause fragmentation to
456 * occur.
457 *
458 * In this case don't do the truncation, either, but we have to
459 * be careful how we detect this case. Blocks beyond EOF show
460 * up as i_delayed_blks even when the inode is clean, so we
461 * need to truncate them away first before checking for a dirty
462 * release. Hence on the first dirty close we will still remove
463 * the speculative allocation, but after that we will leave it
464 * in place.
465 */
466 if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
467 return 0;
1da177e4 468
6e857567
DC
469 error = xfs_free_eofblocks(mp, ip,
470 XFS_FREE_EOF_TRYLOCK);
471 if (error)
472 return error;
473
474 /* delalloc blocks after truncation means it really is dirty */
475 if (ip->i_delayed_blks)
476 xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
477 }
1da177e4
LT
478 return 0;
479}
480
481/*
482 * xfs_inactive
483 *
484 * This is called when the vnode reference count for the vnode
485 * goes to zero. If the file has been unlinked, then it must
486 * now be truncated. Also, we clear all of the read-ahead state
487 * kept for the inode here since the file is now closed.
488 */
993386c1 489int
1da177e4 490xfs_inactive(
993386c1 491 xfs_inode_t *ip)
1da177e4 492{
67fcaa73 493 xfs_bmap_free_t free_list;
1da177e4
LT
494 xfs_fsblock_t first_block;
495 int committed;
496 xfs_trans_t *tp;
497 xfs_mount_t *mp;
498 int error;
b373e98d 499 int truncate = 0;
1da177e4 500
1da177e4
LT
501 /*
502 * If the inode is already free, then there can be nothing
503 * to clean up here.
504 */
cb4c8cc1 505 if (ip->i_d.di_mode == 0 || is_bad_inode(VFS_I(ip))) {
1da177e4
LT
506 ASSERT(ip->i_df.if_real_bytes == 0);
507 ASSERT(ip->i_df.if_broot_bytes == 0);
508 return VN_INACTIVE_CACHE;
509 }
510
1da177e4
LT
511 mp = ip->i_mount;
512
1da177e4
LT
513 error = 0;
514
515 /* If this is a read-only mount, don't do this (would generate I/O) */
bd186aa9 516 if (mp->m_flags & XFS_MOUNT_RDONLY)
1da177e4
LT
517 goto out;
518
519 if (ip->i_d.di_nlink != 0) {
abbede1b 520 if ((S_ISREG(ip->i_d.di_mode) &&
ce7ae151
CH
521 (VFS_I(ip)->i_size > 0 ||
522 (VN_CACHED(VFS_I(ip)) > 0 || ip->i_delayed_blks > 0)) &&
523 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
524 (!(ip->i_d.di_flags &
dd9f438e 525 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
ce7ae151 526 ip->i_delayed_blks != 0))) {
c56c9631 527 error = xfs_free_eofblocks(mp, ip, 0);
92dfe8d2 528 if (error)
014c2544 529 return VN_INACTIVE_CACHE;
1da177e4
LT
530 }
531 goto out;
532 }
533
b373e98d
CH
534 if (S_ISREG(ip->i_d.di_mode) &&
535 (ip->i_d.di_size != 0 || XFS_ISIZE(ip) != 0 ||
536 ip->i_d.di_nextents > 0 || ip->i_delayed_blks > 0))
537 truncate = 1;
1da177e4 538
7d095257
CH
539 error = xfs_qm_dqattach(ip, 0);
540 if (error)
014c2544 541 return VN_INACTIVE_CACHE;
1da177e4
LT
542
543 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
b373e98d
CH
544 error = xfs_trans_reserve(tp, 0,
545 (truncate || S_ISLNK(ip->i_d.di_mode)) ?
546 XFS_ITRUNCATE_LOG_RES(mp) :
547 XFS_IFREE_LOG_RES(mp),
548 0,
549 XFS_TRANS_PERM_LOG_RES,
550 XFS_ITRUNCATE_LOG_COUNT);
551 if (error) {
552 ASSERT(XFS_FORCED_SHUTDOWN(mp));
553 xfs_trans_cancel(tp, 0);
554 return VN_INACTIVE_CACHE;
555 }
1da177e4 556
b373e98d
CH
557 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
558 xfs_trans_ijoin(tp, ip, 0);
1da177e4 559
b373e98d
CH
560 if (S_ISLNK(ip->i_d.di_mode)) {
561 /*
562 * Zero length symlinks _can_ exist.
563 */
564 if (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) {
565 error = xfs_inactive_symlink_rmt(ip, &tp);
566 if (error)
567 goto out_cancel;
568 } else if (ip->i_df.if_bytes > 0) {
569 xfs_idata_realloc(ip, -(ip->i_df.if_bytes),
570 XFS_DATA_FORK);
571 ASSERT(ip->i_df.if_bytes == 0);
572 }
573 } else if (truncate) {
673e8e59 574 ip->i_d.di_size = 0;
673e8e59
CH
575 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
576
577 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
b373e98d
CH
578 if (error)
579 goto out_cancel;
673e8e59
CH
580
581 ASSERT(ip->i_d.di_nextents == 0);
1da177e4
LT
582 }
583
584 /*
fe67be03
CH
585 * If there are attributes associated with the file then blow them away
586 * now. The code calls a routine that recursively deconstructs the
587 * attribute fork. We need to just commit the current transaction
1da177e4
LT
588 * because we can't use it for xfs_attr_inactive().
589 */
590 if (ip->i_d.di_anextents > 0) {
fe67be03
CH
591 ASSERT(ip->i_d.di_forkoff != 0);
592
593 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
594 xfs_iunlock(ip, XFS_ILOCK_EXCL);
595 if (error)
596 goto error_unlock;
597
598 error = xfs_attr_inactive(ip);
599 if (error)
600 goto error_unlock;
601
602 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
603 error = xfs_trans_reserve(tp, 0,
604 XFS_IFREE_LOG_RES(mp),
605 0, XFS_TRANS_PERM_LOG_RES,
606 XFS_INACTIVE_LOG_COUNT);
607 if (error)
608 goto error_cancel;
609
610 xfs_ilock(ip, XFS_ILOCK_EXCL);
611 xfs_trans_ijoin(tp, ip, 0);
1da177e4
LT
612 }
613
fe67be03
CH
614 if (ip->i_afp)
615 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
616
617 ASSERT(ip->i_d.di_anextents == 0);
618
1da177e4
LT
619 /*
620 * Free the inode.
621 */
9d87c319 622 xfs_bmap_init(&free_list, &first_block);
1da177e4
LT
623 error = xfs_ifree(tp, ip, &free_list);
624 if (error) {
625 /*
626 * If we fail to free the inode, shut down. The cancel
627 * might do that, we need to make sure. Otherwise the
628 * inode might be lost for a long time or forever.
629 */
630 if (!XFS_FORCED_SHUTDOWN(mp)) {
0b932ccc
DC
631 xfs_notice(mp, "%s: xfs_ifree returned error %d",
632 __func__, error);
7d04a335 633 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1da177e4
LT
634 }
635 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
636 } else {
637 /*
638 * Credit the quota account(s). The inode is gone.
639 */
7d095257 640 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1da177e4
LT
641
642 /*
78e9da77
DC
643 * Just ignore errors at this point. There is nothing we can
644 * do except to try to keep going. Make sure it's not a silent
645 * error.
1da177e4 646 */
78e9da77
DC
647 error = xfs_bmap_finish(&tp, &free_list, &committed);
648 if (error)
53487786
DC
649 xfs_notice(mp, "%s: xfs_bmap_finish returned error %d",
650 __func__, error);
78e9da77
DC
651 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
652 if (error)
53487786
DC
653 xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
654 __func__, error);
1da177e4 655 }
7d095257 656
1da177e4
LT
657 /*
658 * Release the dquots held by inode, if any.
659 */
7d095257 660 xfs_qm_dqdetach(ip);
1da177e4
LT
661 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
662
b373e98d
CH
663out:
664 return VN_INACTIVE_CACHE;
665out_cancel:
666 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
667 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1da177e4 668 return VN_INACTIVE_CACHE;
fe67be03
CH
669
670error_cancel:
671 ASSERT(XFS_FORCED_SHUTDOWN(mp));
672 xfs_trans_cancel(tp, 0);
673error_unlock:
674 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
675 return VN_INACTIVE_CACHE;
1da177e4
LT
676}
677
384f3ced
BN
678/*
679 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
680 * is allowed, otherwise it has to be an exact match. If a CI match is found,
681 * ci_name->name will point to a the actual name (caller must free) or
682 * will be set to NULL if an exact match is found.
683 */
993386c1 684int
1da177e4 685xfs_lookup(
993386c1 686 xfs_inode_t *dp,
556b8b16 687 struct xfs_name *name,
384f3ced
BN
688 xfs_inode_t **ipp,
689 struct xfs_name *ci_name)
1da177e4 690{
eca450b7 691 xfs_ino_t inum;
1da177e4
LT
692 int error;
693 uint lock_mode;
1da177e4 694
cca28fb8 695 trace_xfs_lookup(dp, name);
1da177e4
LT
696
697 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
698 return XFS_ERROR(EIO);
699
700 lock_mode = xfs_ilock_map_shared(dp);
384f3ced 701 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
1da177e4 702 xfs_iunlock_map_shared(dp, lock_mode);
eca450b7
CH
703
704 if (error)
705 goto out;
706
7b6259e7 707 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
eca450b7 708 if (error)
384f3ced 709 goto out_free_name;
eca450b7 710
eca450b7
CH
711 return 0;
712
384f3ced
BN
713out_free_name:
714 if (ci_name)
715 kmem_free(ci_name->name);
716out:
eca450b7 717 *ipp = NULL;
1da177e4
LT
718 return error;
719}
720
993386c1 721int
1da177e4 722xfs_create(
993386c1 723 xfs_inode_t *dp,
556b8b16 724 struct xfs_name *name,
576b1d67 725 umode_t mode,
3e5daf05 726 xfs_dev_t rdev,
6c77b0ea 727 xfs_inode_t **ipp)
1da177e4 728{
517b5e8c
CH
729 int is_dir = S_ISDIR(mode);
730 struct xfs_mount *mp = dp->i_mount;
731 struct xfs_inode *ip = NULL;
732 struct xfs_trans *tp = NULL;
556b8b16 733 int error;
1da177e4
LT
734 xfs_bmap_free_t free_list;
735 xfs_fsblock_t first_block;
993386c1 736 boolean_t unlock_dp_on_error = B_FALSE;
1da177e4
LT
737 uint cancel_flags;
738 int committed;
6743099c 739 prid_t prid;
517b5e8c
CH
740 struct xfs_dquot *udqp = NULL;
741 struct xfs_dquot *gdqp = NULL;
1da177e4 742 uint resblks;
517b5e8c
CH
743 uint log_res;
744 uint log_count;
1da177e4 745
cca28fb8 746 trace_xfs_create(dp, name);
1da177e4 747
517b5e8c
CH
748 if (XFS_FORCED_SHUTDOWN(mp))
749 return XFS_ERROR(EIO);
750
365ca83d 751 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
6743099c 752 prid = xfs_get_projid(dp);
1da177e4 753 else
6743099c 754 prid = XFS_PROJID_DEFAULT;
1da177e4
LT
755
756 /*
757 * Make sure that we have allocated dquot(s) on disk.
758 */
7d095257 759 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
517b5e8c 760 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
1da177e4 761 if (error)
ec3ba85f 762 return error;
1da177e4 763
517b5e8c
CH
764 if (is_dir) {
765 rdev = 0;
766 resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
767 log_res = XFS_MKDIR_LOG_RES(mp);
768 log_count = XFS_MKDIR_LOG_COUNT;
769 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
770 } else {
771 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
772 log_res = XFS_CREATE_LOG_RES(mp);
773 log_count = XFS_CREATE_LOG_COUNT;
774 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
775 }
1da177e4 776
1da177e4 777 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
517b5e8c 778
1da177e4
LT
779 /*
780 * Initially assume that the file does not exist and
781 * reserve the resources for that case. If that is not
782 * the case we'll drop the one we have and get a more
783 * appropriate transaction later.
784 */
517b5e8c
CH
785 error = xfs_trans_reserve(tp, resblks, log_res, 0,
786 XFS_TRANS_PERM_LOG_RES, log_count);
1da177e4 787 if (error == ENOSPC) {
153fec43
DC
788 /* flush outstanding delalloc blocks and retry */
789 xfs_flush_inodes(dp);
4734d401
CH
790 error = xfs_trans_reserve(tp, resblks, log_res, 0,
791 XFS_TRANS_PERM_LOG_RES, log_count);
153fec43
DC
792 }
793 if (error == ENOSPC) {
794 /* No space at all so try a "no-allocation" reservation */
1da177e4 795 resblks = 0;
517b5e8c
CH
796 error = xfs_trans_reserve(tp, 0, log_res, 0,
797 XFS_TRANS_PERM_LOG_RES, log_count);
1da177e4
LT
798 }
799 if (error) {
800 cancel_flags = 0;
517b5e8c 801 goto out_trans_cancel;
1da177e4
LT
802 }
803
f7c66ce3 804 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
993386c1 805 unlock_dp_on_error = B_TRUE;
1da177e4 806
517b5e8c 807 xfs_bmap_init(&free_list, &first_block);
1da177e4
LT
808
809 /*
810 * Reserve disk quota and the inode.
811 */
7d095257 812 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
1da177e4 813 if (error)
517b5e8c 814 goto out_trans_cancel;
1da177e4 815
556b8b16
BN
816 error = xfs_dir_canenter(tp, dp, name, resblks);
817 if (error)
517b5e8c
CH
818 goto out_trans_cancel;
819
820 /*
821 * A newly created regular or special file just has one directory
822 * entry pointing to them, but a directory also the "." entry
823 * pointing to itself.
824 */
6c77b0ea 825 error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev,
517b5e8c 826 prid, resblks > 0, &ip, &committed);
1da177e4
LT
827 if (error) {
828 if (error == ENOSPC)
517b5e8c
CH
829 goto out_trans_cancel;
830 goto out_trans_abort;
1da177e4 831 }
1da177e4 832
1da177e4 833 /*
993386c1
CH
834 * Now we join the directory inode to the transaction. We do not do it
835 * earlier because xfs_dir_ialloc might commit the previous transaction
836 * (and release all the locks). An error from here on will result in
837 * the transaction cancel unlocking dp so don't do it explicitly in the
838 * error path.
1da177e4 839 */
ddc3415a 840 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
993386c1 841 unlock_dp_on_error = B_FALSE;
1da177e4 842
556b8b16 843 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
f6c2d1fa
NS
844 &first_block, &free_list, resblks ?
845 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1da177e4
LT
846 if (error) {
847 ASSERT(error != ENOSPC);
517b5e8c 848 goto out_trans_abort;
1da177e4 849 }
dcd79a14 850 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1da177e4
LT
851 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
852
517b5e8c
CH
853 if (is_dir) {
854 error = xfs_dir_init(tp, ip, dp);
855 if (error)
856 goto out_bmap_cancel;
857
858 error = xfs_bumplink(tp, dp);
859 if (error)
860 goto out_bmap_cancel;
861 }
862
1da177e4
LT
863 /*
864 * If this is a synchronous mount, make sure that the
865 * create transaction goes to disk before returning to
866 * the user.
867 */
517b5e8c 868 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1da177e4 869 xfs_trans_set_sync(tp);
1da177e4 870
1da177e4
LT
871 /*
872 * Attach the dquot(s) to the inodes and modify them incore.
873 * These ids of the inode couldn't have changed since the new
874 * inode has been locked ever since it was created.
875 */
7d095257 876 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
1da177e4 877
f7c99b6f 878 error = xfs_bmap_finish(&tp, &free_list, &committed);
517b5e8c 879 if (error)
ec3ba85f 880 goto out_bmap_cancel;
1da177e4 881
1c72bf90 882 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
ec3ba85f
CH
883 if (error)
884 goto out_release_inode;
1da177e4 885
7d095257
CH
886 xfs_qm_dqrele(udqp);
887 xfs_qm_dqrele(gdqp);
1da177e4 888
979ebab1 889 *ipp = ip;
288699fe 890 return 0;
1da177e4 891
517b5e8c
CH
892 out_bmap_cancel:
893 xfs_bmap_cancel(&free_list);
894 out_trans_abort:
1da177e4 895 cancel_flags |= XFS_TRANS_ABORT;
517b5e8c
CH
896 out_trans_cancel:
897 xfs_trans_cancel(tp, cancel_flags);
ec3ba85f
CH
898 out_release_inode:
899 /*
900 * Wait until after the current transaction is aborted to
901 * release the inode. This prevents recursive transactions
902 * and deadlocks from xfs_inactive.
903 */
904 if (ip)
905 IRELE(ip);
906
7d095257
CH
907 xfs_qm_dqrele(udqp);
908 xfs_qm_dqrele(gdqp);
1da177e4 909
993386c1
CH
910 if (unlock_dp_on_error)
911 xfs_iunlock(dp, XFS_ILOCK_EXCL);
288699fe 912 return error;
1da177e4
LT
913}
914
1da177e4
LT
915#ifdef DEBUG
916int xfs_locked_n;
917int xfs_small_retries;
918int xfs_middle_retries;
919int xfs_lots_retries;
920int xfs_lock_delays;
921#endif
922
f7c66ce3
LM
923/*
924 * Bump the subclass so xfs_lock_inodes() acquires each lock with
925 * a different value
926 */
927static inline int
928xfs_lock_inumorder(int lock_mode, int subclass)
929{
930 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
0f1145cc 931 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
f7c66ce3 932 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
0f1145cc 933 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
f7c66ce3
LM
934
935 return lock_mode;
936}
937
1da177e4
LT
938/*
939 * The following routine will lock n inodes in exclusive mode.
940 * We assume the caller calls us with the inodes in i_ino order.
941 *
942 * We need to detect deadlock where an inode that we lock
943 * is in the AIL and we start waiting for another inode that is locked
944 * by a thread in a long running transaction (such as truncate). This can
945 * result in deadlock since the long running trans might need to wait
946 * for the inode we just locked in order to push the tail and free space
947 * in the log.
948 */
949void
950xfs_lock_inodes(
951 xfs_inode_t **ips,
952 int inodes,
1da177e4
LT
953 uint lock_mode)
954{
955 int attempts = 0, i, j, try_lock;
956 xfs_log_item_t *lp;
957
958 ASSERT(ips && (inodes >= 2)); /* we need at least two */
959
cfa853e4
CH
960 try_lock = 0;
961 i = 0;
1da177e4
LT
962
963again:
964 for (; i < inodes; i++) {
965 ASSERT(ips[i]);
966
967 if (i && (ips[i] == ips[i-1])) /* Already locked */
968 continue;
969
970 /*
971 * If try_lock is not set yet, make sure all locked inodes
972 * are not in the AIL.
973 * If any are, set try_lock to be used later.
974 */
975
976 if (!try_lock) {
977 for (j = (i - 1); j >= 0 && !try_lock; j--) {
978 lp = (xfs_log_item_t *)ips[j]->i_itemp;
979 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
980 try_lock++;
981 }
982 }
983 }
984
985 /*
986 * If any of the previous locks we have locked is in the AIL,
987 * we must TRY to get the second and subsequent locks. If
988 * we can't get any, we must release all we have
989 * and try again.
990 */
991
992 if (try_lock) {
993 /* try_lock must be 0 if i is 0. */
994 /*
995 * try_lock means we have an inode locked
996 * that is in the AIL.
997 */
998 ASSERT(i != 0);
f7c66ce3 999 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
1da177e4
LT
1000 attempts++;
1001
1002 /*
1003 * Unlock all previous guys and try again.
1004 * xfs_iunlock will try to push the tail
1005 * if the inode is in the AIL.
1006 */
1007
1008 for(j = i - 1; j >= 0; j--) {
1009
1010 /*
1011 * Check to see if we've already
1012 * unlocked this one.
1013 * Not the first one going back,
1014 * and the inode ptr is the same.
1015 */
1016 if ((j != (i - 1)) && ips[j] ==
1017 ips[j+1])
1018 continue;
1019
1020 xfs_iunlock(ips[j], lock_mode);
1021 }
1022
1023 if ((attempts % 5) == 0) {
1024 delay(1); /* Don't just spin the CPU */
1025#ifdef DEBUG
1026 xfs_lock_delays++;
1027#endif
1028 }
1029 i = 0;
1030 try_lock = 0;
1031 goto again;
1032 }
1033 } else {
f7c66ce3 1034 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
1da177e4
LT
1035 }
1036 }
1037
1038#ifdef DEBUG
1039 if (attempts) {
1040 if (attempts < 5) xfs_small_retries++;
1041 else if (attempts < 100) xfs_middle_retries++;
1042 else xfs_lots_retries++;
1043 } else {
1044 xfs_locked_n++;
1045 }
1046#endif
1047}
1048
f9114eba
DC
1049/*
1050 * xfs_lock_two_inodes() can only be used to lock one type of lock
1051 * at a time - the iolock or the ilock, but not both at once. If
1052 * we lock both at once, lockdep will report false positives saying
1053 * we have violated locking orders.
1054 */
e1cccd91
CH
1055void
1056xfs_lock_two_inodes(
1057 xfs_inode_t *ip0,
1058 xfs_inode_t *ip1,
1059 uint lock_mode)
1060{
1061 xfs_inode_t *temp;
1062 int attempts = 0;
1063 xfs_log_item_t *lp;
1064
f9114eba
DC
1065 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1066 ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
e1cccd91
CH
1067 ASSERT(ip0->i_ino != ip1->i_ino);
1068
1069 if (ip0->i_ino > ip1->i_ino) {
1070 temp = ip0;
1071 ip0 = ip1;
1072 ip1 = temp;
1073 }
1074
1075 again:
1076 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
1077
1078 /*
1079 * If the first lock we have locked is in the AIL, we must TRY to get
1080 * the second lock. If we can't get it, we must release the first one
1081 * and try again.
1082 */
1083 lp = (xfs_log_item_t *)ip0->i_itemp;
1084 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1085 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
1086 xfs_iunlock(ip0, lock_mode);
1087 if ((++attempts % 5) == 0)
1088 delay(1); /* Don't just spin the CPU */
1089 goto again;
1090 }
1091 } else {
1092 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
1093 }
1094}
1095
993386c1 1096int
1da177e4 1097xfs_remove(
993386c1 1098 xfs_inode_t *dp,
556b8b16
BN
1099 struct xfs_name *name,
1100 xfs_inode_t *ip)
1da177e4 1101{
993386c1 1102 xfs_mount_t *mp = dp->i_mount;
1da177e4 1103 xfs_trans_t *tp = NULL;
8f112e3b 1104 int is_dir = S_ISDIR(ip->i_d.di_mode);
1da177e4
LT
1105 int error = 0;
1106 xfs_bmap_free_t free_list;
1107 xfs_fsblock_t first_block;
1108 int cancel_flags;
1109 int committed;
1da177e4
LT
1110 int link_zero;
1111 uint resblks;
8f112e3b 1112 uint log_count;
1da177e4 1113
cca28fb8 1114 trace_xfs_remove(dp, name);
1da177e4 1115
1da177e4
LT
1116 if (XFS_FORCED_SHUTDOWN(mp))
1117 return XFS_ERROR(EIO);
1118
7d095257 1119 error = xfs_qm_dqattach(dp, 0);
8f112e3b
CH
1120 if (error)
1121 goto std_return;
1122
7d095257 1123 error = xfs_qm_dqattach(ip, 0);
8f112e3b 1124 if (error)
1da177e4 1125 goto std_return;
1da177e4 1126
8f112e3b
CH
1127 if (is_dir) {
1128 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
1129 log_count = XFS_DEFAULT_LOG_COUNT;
1130 } else {
1131 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
1132 log_count = XFS_REMOVE_LOG_COUNT;
1133 }
1da177e4 1134 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
8f112e3b 1135
1da177e4
LT
1136 /*
1137 * We try to get the real space reservation first,
1138 * allowing for directory btree deletion(s) implying
1139 * possible bmap insert(s). If we can't get the space
1140 * reservation then we use 0 instead, and avoid the bmap
1141 * btree insert(s) in the directory code by, if the bmap
1142 * insert tries to happen, instead trimming the LAST
1143 * block from the directory.
1144 */
1145 resblks = XFS_REMOVE_SPACE_RES(mp);
1146 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
8f112e3b 1147 XFS_TRANS_PERM_LOG_RES, log_count);
1da177e4
LT
1148 if (error == ENOSPC) {
1149 resblks = 0;
1150 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
8f112e3b 1151 XFS_TRANS_PERM_LOG_RES, log_count);
1da177e4
LT
1152 }
1153 if (error) {
1154 ASSERT(error != ENOSPC);
8f112e3b
CH
1155 cancel_flags = 0;
1156 goto out_trans_cancel;
1da177e4
LT
1157 }
1158
e1cccd91 1159 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
1da177e4 1160
ddc3415a
CH
1161 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1162 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1da177e4 1163
8f112e3b
CH
1164 /*
1165 * If we're removing a directory perform some additional validation.
1166 */
1167 if (is_dir) {
1168 ASSERT(ip->i_d.di_nlink >= 2);
1169 if (ip->i_d.di_nlink != 2) {
1170 error = XFS_ERROR(ENOTEMPTY);
1171 goto out_trans_cancel;
1172 }
1173 if (!xfs_dir_isempty(ip)) {
1174 error = XFS_ERROR(ENOTEMPTY);
1175 goto out_trans_cancel;
1176 }
1177 }
1178
9d87c319 1179 xfs_bmap_init(&free_list, &first_block);
556b8b16 1180 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
d4377d84 1181 &first_block, &free_list, resblks);
1da177e4
LT
1182 if (error) {
1183 ASSERT(error != ENOENT);
8f112e3b 1184 goto out_bmap_cancel;
1da177e4 1185 }
dcd79a14 1186 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1da177e4 1187
8f112e3b
CH
1188 if (is_dir) {
1189 /*
1190 * Drop the link from ip's "..".
1191 */
1192 error = xfs_droplink(tp, dp);
1193 if (error)
1194 goto out_bmap_cancel;
1195
1196 /*
2b7035fd 1197 * Drop the "." link from ip to self.
8f112e3b
CH
1198 */
1199 error = xfs_droplink(tp, ip);
1200 if (error)
1201 goto out_bmap_cancel;
1202 } else {
1203 /*
1204 * When removing a non-directory we need to log the parent
26c52951
DC
1205 * inode here. For a directory this is done implicitly
1206 * by the xfs_droplink call for the ".." entry.
8f112e3b
CH
1207 */
1208 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1da177e4
LT
1209 }
1210
8f112e3b 1211 /*
2b7035fd 1212 * Drop the link from dp to ip.
8f112e3b
CH
1213 */
1214 error = xfs_droplink(tp, ip);
1215 if (error)
1216 goto out_bmap_cancel;
1217
1218 /*
1219 * Determine if this is the last link while
1da177e4
LT
1220 * we are in the transaction.
1221 */
8f112e3b 1222 link_zero = (ip->i_d.di_nlink == 0);
1da177e4 1223
1da177e4
LT
1224 /*
1225 * If this is a synchronous mount, make sure that the
1226 * remove transaction goes to disk before returning to
1227 * the user.
1228 */
8f112e3b 1229 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1da177e4 1230 xfs_trans_set_sync(tp);
1da177e4 1231
f7c99b6f 1232 error = xfs_bmap_finish(&tp, &free_list, &committed);
8f112e3b
CH
1233 if (error)
1234 goto out_bmap_cancel;
1da177e4 1235
1c72bf90 1236 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
5df78e73 1237 if (error)
1da177e4 1238 goto std_return;
1da177e4 1239
2a82b8be
DC
1240 /*
1241 * If we are using filestreams, kill the stream association.
1242 * If the file is still open it may get a new one but that
1243 * will get killed on last close in xfs_close() so we don't
1244 * have to worry about that.
1245 */
8f112e3b 1246 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
2a82b8be
DC
1247 xfs_filestream_deassociate(ip);
1248
288699fe 1249 return 0;
1da177e4 1250
8f112e3b 1251 out_bmap_cancel:
1da177e4
LT
1252 xfs_bmap_cancel(&free_list);
1253 cancel_flags |= XFS_TRANS_ABORT;
8f112e3b 1254 out_trans_cancel:
1da177e4 1255 xfs_trans_cancel(tp, cancel_flags);
288699fe
CH
1256 std_return:
1257 return error;
1da177e4
LT
1258}
1259
993386c1 1260int
1da177e4 1261xfs_link(
993386c1 1262 xfs_inode_t *tdp,
a3da7896 1263 xfs_inode_t *sip,
556b8b16 1264 struct xfs_name *target_name)
1da177e4 1265{
993386c1 1266 xfs_mount_t *mp = tdp->i_mount;
1da177e4 1267 xfs_trans_t *tp;
1da177e4
LT
1268 int error;
1269 xfs_bmap_free_t free_list;
1270 xfs_fsblock_t first_block;
1271 int cancel_flags;
1272 int committed;
1da177e4 1273 int resblks;
1da177e4 1274
cca28fb8 1275 trace_xfs_link(tdp, target_name);
1da177e4 1276
a3da7896 1277 ASSERT(!S_ISDIR(sip->i_d.di_mode));
1da177e4 1278
1da177e4
LT
1279 if (XFS_FORCED_SHUTDOWN(mp))
1280 return XFS_ERROR(EIO);
1281
7d095257 1282 error = xfs_qm_dqattach(sip, 0);
cb3f35bb
CH
1283 if (error)
1284 goto std_return;
1285
7d095257 1286 error = xfs_qm_dqattach(tdp, 0);
1da177e4
LT
1287 if (error)
1288 goto std_return;
1289
1290 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
1291 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
556b8b16 1292 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
1da177e4
LT
1293 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
1294 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
1295 if (error == ENOSPC) {
1296 resblks = 0;
1297 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
1298 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
1299 }
1300 if (error) {
1301 cancel_flags = 0;
1302 goto error_return;
1303 }
1304
e1cccd91 1305 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
1da177e4 1306
ddc3415a
CH
1307 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
1308 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
1da177e4 1309
365ca83d
NS
1310 /*
1311 * If we are using project inheritance, we only allow hard link
1312 * creation in our tree when the project IDs are the same; else
1313 * the tree quota mechanism could be circumvented.
1314 */
1315 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
6743099c 1316 (xfs_get_projid(tdp) != xfs_get_projid(sip)))) {
b1ecdda9 1317 error = XFS_ERROR(EXDEV);
365ca83d
NS
1318 goto error_return;
1319 }
1320
556b8b16
BN
1321 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
1322 if (error)
1da177e4
LT
1323 goto error_return;
1324
9d87c319 1325 xfs_bmap_init(&free_list, &first_block);
1da177e4 1326
556b8b16
BN
1327 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
1328 &first_block, &free_list, resblks);
1da177e4
LT
1329 if (error)
1330 goto abort_return;
dcd79a14 1331 xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1da177e4
LT
1332 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
1333
1334 error = xfs_bumplink(tp, sip);
b71d300c 1335 if (error)
1da177e4 1336 goto abort_return;
1da177e4
LT
1337
1338 /*
1339 * If this is a synchronous mount, make sure that the
1340 * link transaction goes to disk before returning to
1341 * the user.
1342 */
1343 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1344 xfs_trans_set_sync(tp);
1345 }
1346
f7c99b6f 1347 error = xfs_bmap_finish (&tp, &free_list, &committed);
1da177e4
LT
1348 if (error) {
1349 xfs_bmap_cancel(&free_list);
1350 goto abort_return;
1351 }
1352
288699fe 1353 return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1da177e4
LT
1354
1355 abort_return:
1356 cancel_flags |= XFS_TRANS_ABORT;
1da177e4
LT
1357 error_return:
1358 xfs_trans_cancel(tp, cancel_flags);
288699fe
CH
1359 std_return:
1360 return error;
1da177e4 1361}
b71d300c 1362
993386c1 1363int
1da177e4 1364xfs_symlink(
993386c1 1365 xfs_inode_t *dp,
556b8b16
BN
1366 struct xfs_name *link_name,
1367 const char *target_path,
576b1d67 1368 umode_t mode,
6c77b0ea 1369 xfs_inode_t **ipp)
1da177e4 1370{
993386c1 1371 xfs_mount_t *mp = dp->i_mount;
1da177e4 1372 xfs_trans_t *tp;
1da177e4
LT
1373 xfs_inode_t *ip;
1374 int error;
1375 int pathlen;
1376 xfs_bmap_free_t free_list;
1377 xfs_fsblock_t first_block;
993386c1 1378 boolean_t unlock_dp_on_error = B_FALSE;
1da177e4
LT
1379 uint cancel_flags;
1380 int committed;
1381 xfs_fileoff_t first_fsb;
1382 xfs_filblks_t fs_blocks;
1383 int nmaps;
1384 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1385 xfs_daddr_t d;
556b8b16 1386 const char *cur_chunk;
1da177e4
LT
1387 int byte_cnt;
1388 int n;
1389 xfs_buf_t *bp;
6743099c 1390 prid_t prid;
1da177e4
LT
1391 struct xfs_dquot *udqp, *gdqp;
1392 uint resblks;
1da177e4 1393
3937be5b 1394 *ipp = NULL;
1da177e4
LT
1395 error = 0;
1396 ip = NULL;
1397 tp = NULL;
1398
cca28fb8 1399 trace_xfs_symlink(dp, link_name);
1da177e4
LT
1400
1401 if (XFS_FORCED_SHUTDOWN(mp))
1402 return XFS_ERROR(EIO);
1403
1da177e4
LT
1404 /*
1405 * Check component lengths of the target path name.
1406 */
1407 pathlen = strlen(target_path);
1408 if (pathlen >= MAXPATHLEN) /* total string too long */
1409 return XFS_ERROR(ENAMETOOLONG);
1da177e4 1410
1da177e4 1411 udqp = gdqp = NULL;
365ca83d 1412 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
6743099c 1413 prid = xfs_get_projid(dp);
1da177e4 1414 else
6743099c 1415 prid = XFS_PROJID_DEFAULT;
1da177e4
LT
1416
1417 /*
1418 * Make sure that we have allocated dquot(s) on disk.
1419 */
7d095257 1420 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
1da177e4
LT
1421 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
1422 if (error)
1423 goto std_return;
1424
1425 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
1426 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1427 /*
1428 * The symlink will fit into the inode data fork?
1429 * There can't be any attributes so we get the whole variable part.
1430 */
1431 if (pathlen <= XFS_LITINO(mp))
1432 fs_blocks = 0;
1433 else
1434 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
556b8b16 1435 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
1da177e4
LT
1436 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
1437 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
1438 if (error == ENOSPC && fs_blocks == 0) {
1439 resblks = 0;
1440 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
1441 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
1442 }
1443 if (error) {
1444 cancel_flags = 0;
1da177e4
LT
1445 goto error_return;
1446 }
1447
f7c66ce3 1448 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
993386c1 1449 unlock_dp_on_error = B_TRUE;
1da177e4
LT
1450
1451 /*
1452 * Check whether the directory allows new symlinks or not.
1453 */
1454 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
1455 error = XFS_ERROR(EPERM);
1456 goto error_return;
1457 }
1458
1459 /*
1460 * Reserve disk quota : blocks and inode.
1461 */
7d095257 1462 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
1da177e4
LT
1463 if (error)
1464 goto error_return;
1465
1466 /*
1467 * Check for ability to enter directory entry, if no space reserved.
1468 */
556b8b16
BN
1469 error = xfs_dir_canenter(tp, dp, link_name, resblks);
1470 if (error)
1da177e4
LT
1471 goto error_return;
1472 /*
1473 * Initialize the bmap freelist prior to calling either
1474 * bmapi or the directory create code.
1475 */
9d87c319 1476 xfs_bmap_init(&free_list, &first_block);
1da177e4
LT
1477
1478 /*
1479 * Allocate an inode for the symlink.
1480 */
6c77b0ea
CH
1481 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT), 1, 0,
1482 prid, resblks > 0, &ip, NULL);
1da177e4
LT
1483 if (error) {
1484 if (error == ENOSPC)
1485 goto error_return;
1486 goto error1;
1487 }
1da177e4 1488
993386c1
CH
1489 /*
1490 * An error after we've joined dp to the transaction will result in the
1491 * transaction cancel unlocking dp so don't do it explicitly in the
1492 * error path.
1493 */
ddc3415a 1494 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
993386c1 1495 unlock_dp_on_error = B_FALSE;
1da177e4
LT
1496
1497 /*
1498 * Also attach the dquot(s) to it, if applicable.
1499 */
7d095257 1500 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
1da177e4
LT
1501
1502 if (resblks)
1503 resblks -= XFS_IALLOC_SPACE_RES(mp);
1504 /*
1505 * If the symlink will fit into the inode, write it inline.
1506 */
1507 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
1508 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
1509 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
1510 ip->i_d.di_size = pathlen;
1511
1512 /*
1513 * The inode was initially created in extent format.
1514 */
1515 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
1516 ip->i_df.if_flags |= XFS_IFINLINE;
1517
1518 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
1519 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
1520
1521 } else {
1522 first_fsb = 0;
1523 nmaps = SYMLINK_MAPS;
1524
c0dc7828
DC
1525 error = xfs_bmapi_write(tp, ip, first_fsb, fs_blocks,
1526 XFS_BMAPI_METADATA, &first_block, resblks,
1527 mval, &nmaps, &free_list);
ec3ba85f
CH
1528 if (error)
1529 goto error2;
1da177e4
LT
1530
1531 if (resblks)
1532 resblks -= fs_blocks;
1533 ip->i_d.di_size = pathlen;
1534 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1535
1536 cur_chunk = target_path;
1537 for (n = 0; n < nmaps; n++) {
1538 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
1539 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
1540 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
1541 BTOBB(byte_cnt), 0);
2a30f36d
CS
1542 if (!bp) {
1543 error = ENOMEM;
1544 goto error2;
1545 }
1da177e4
LT
1546 if (pathlen < byte_cnt) {
1547 byte_cnt = pathlen;
1548 }
1549 pathlen -= byte_cnt;
1550
62926044 1551 memcpy(bp->b_addr, cur_chunk, byte_cnt);
1da177e4
LT
1552 cur_chunk += byte_cnt;
1553
1554 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
1555 }
1556 }
1557
1558 /*
1559 * Create the directory entry for the symlink.
1560 */
556b8b16
BN
1561 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
1562 &first_block, &free_list, resblks);
f6c2d1fa 1563 if (error)
ec3ba85f 1564 goto error2;
dcd79a14 1565 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1da177e4
LT
1566 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1567
1da177e4
LT
1568 /*
1569 * If this is a synchronous mount, make sure that the
1570 * symlink transaction goes to disk before returning to
1571 * the user.
1572 */
1573 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1574 xfs_trans_set_sync(tp);
1575 }
1576
f7c99b6f 1577 error = xfs_bmap_finish(&tp, &free_list, &committed);
1da177e4
LT
1578 if (error) {
1579 goto error2;
1580 }
1c72bf90 1581 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
7d095257
CH
1582 xfs_qm_dqrele(udqp);
1583 xfs_qm_dqrele(gdqp);
1da177e4 1584
288699fe
CH
1585 *ipp = ip;
1586 return 0;
1da177e4
LT
1587
1588 error2:
1589 IRELE(ip);
1590 error1:
1591 xfs_bmap_cancel(&free_list);
1592 cancel_flags |= XFS_TRANS_ABORT;
1593 error_return:
1594 xfs_trans_cancel(tp, cancel_flags);
7d095257
CH
1595 xfs_qm_dqrele(udqp);
1596 xfs_qm_dqrele(gdqp);
1da177e4 1597
993386c1 1598 if (unlock_dp_on_error)
1da177e4 1599 xfs_iunlock(dp, XFS_ILOCK_EXCL);
288699fe
CH
1600 std_return:
1601 return error;
1da177e4
LT
1602}
1603
1da177e4 1604int
993386c1
CH
1605xfs_set_dmattrs(
1606 xfs_inode_t *ip,
1da177e4 1607 u_int evmask,
993386c1 1608 u_int16_t state)
1da177e4 1609{
993386c1 1610 xfs_mount_t *mp = ip->i_mount;
1da177e4 1611 xfs_trans_t *tp;
1da177e4
LT
1612 int error;
1613
1614 if (!capable(CAP_SYS_ADMIN))
1615 return XFS_ERROR(EPERM);
1616
1da177e4
LT
1617 if (XFS_FORCED_SHUTDOWN(mp))
1618 return XFS_ERROR(EIO);
1619
1620 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
1621 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
1622 if (error) {
1623 xfs_trans_cancel(tp, 0);
1624 return error;
1625 }
1626 xfs_ilock(ip, XFS_ILOCK_EXCL);
ddc3415a 1627 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1da177e4 1628
613d7043
CH
1629 ip->i_d.di_dmevmask = evmask;
1630 ip->i_d.di_dmstate = state;
1da177e4
LT
1631
1632 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1c72bf90 1633 error = xfs_trans_commit(tp, 0);
1da177e4
LT
1634
1635 return error;
1636}
1637
1da177e4
LT
1638/*
1639 * xfs_alloc_file_space()
1640 * This routine allocates disk space for the given file.
1641 *
1642 * If alloc_type == 0, this request is for an ALLOCSP type
1643 * request which will change the file size. In this case, no
1644 * DMAPI event will be generated by the call. A TRUNCATE event
1645 * will be generated later by xfs_setattr.
1646 *
1647 * If alloc_type != 0, this request is for a RESVSP type
1648 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
1649 * lower block boundary byte address is less than the file's
1650 * length.
1651 *
1652 * RETURNS:
1653 * 0 on success
1654 * errno on error
1655 *
1656 */
ba0f32d4 1657STATIC int
1da177e4
LT
1658xfs_alloc_file_space(
1659 xfs_inode_t *ip,
1660 xfs_off_t offset,
1661 xfs_off_t len,
1662 int alloc_type,
1663 int attr_flags)
1664{
dd9f438e
NS
1665 xfs_mount_t *mp = ip->i_mount;
1666 xfs_off_t count;
1da177e4
LT
1667 xfs_filblks_t allocated_fsb;
1668 xfs_filblks_t allocatesize_fsb;
dd9f438e
NS
1669 xfs_extlen_t extsz, temp;
1670 xfs_fileoff_t startoffset_fsb;
1da177e4 1671 xfs_fsblock_t firstfsb;
dd9f438e 1672 int nimaps;
dd9f438e 1673 int quota_flag;
1da177e4 1674 int rt;
1da177e4 1675 xfs_trans_t *tp;
dd9f438e
NS
1676 xfs_bmbt_irec_t imaps[1], *imapp;
1677 xfs_bmap_free_t free_list;
1678 uint qblocks, resblks, resrtextents;
1679 int committed;
1680 int error;
1da177e4 1681
cca28fb8 1682 trace_xfs_alloc_file_space(ip);
1da177e4
LT
1683
1684 if (XFS_FORCED_SHUTDOWN(mp))
1685 return XFS_ERROR(EIO);
1686
7d095257
CH
1687 error = xfs_qm_dqattach(ip, 0);
1688 if (error)
1da177e4
LT
1689 return error;
1690
1691 if (len <= 0)
1692 return XFS_ERROR(EINVAL);
1693
957d0ebe
DC
1694 rt = XFS_IS_REALTIME_INODE(ip);
1695 extsz = xfs_get_extsz_hint(ip);
1696
1da177e4 1697 count = len;
1da177e4 1698 imapp = &imaps[0];
dd9f438e 1699 nimaps = 1;
1da177e4
LT
1700 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
1701 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
1702
1da177e4 1703 /*
dd9f438e 1704 * Allocate file space until done or until there is an error
1da177e4 1705 */
1da177e4 1706 while (allocatesize_fsb && !error) {
dd9f438e
NS
1707 xfs_fileoff_t s, e;
1708
1da177e4 1709 /*
3ddb8fa9 1710 * Determine space reservations for data/realtime.
1da177e4 1711 */
dd9f438e 1712 if (unlikely(extsz)) {
1da177e4 1713 s = startoffset_fsb;
dd9f438e
NS
1714 do_div(s, extsz);
1715 s *= extsz;
1716 e = startoffset_fsb + allocatesize_fsb;
1717 if ((temp = do_mod(startoffset_fsb, extsz)))
1718 e += temp;
1719 if ((temp = do_mod(e, extsz)))
1720 e += extsz - temp;
1721 } else {
1722 s = 0;
1723 e = allocatesize_fsb;
1724 }
1725
72656c46
DC
1726 /*
1727 * The transaction reservation is limited to a 32-bit block
1728 * count, hence we need to limit the number of blocks we are
1729 * trying to reserve to avoid an overflow. We can't allocate
1730 * more than @nimaps extents, and an extent is limited on disk
1731 * to MAXEXTLEN (21 bits), so use that to enforce the limit.
1732 */
1733 resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));
dd9f438e 1734 if (unlikely(rt)) {
72656c46 1735 resrtextents = qblocks = resblks;
dd9f438e
NS
1736 resrtextents /= mp->m_sb.sb_rextsize;
1737 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
1738 quota_flag = XFS_QMOPT_RES_RTBLKS;
1da177e4 1739 } else {
dd9f438e 1740 resrtextents = 0;
72656c46 1741 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
dd9f438e 1742 quota_flag = XFS_QMOPT_RES_REGBLKS;
1da177e4
LT
1743 }
1744
1745 /*
dd9f438e 1746 * Allocate and setup the transaction.
1da177e4
LT
1747 */
1748 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
dd9f438e
NS
1749 error = xfs_trans_reserve(tp, resblks,
1750 XFS_WRITE_LOG_RES(mp), resrtextents,
1da177e4
LT
1751 XFS_TRANS_PERM_LOG_RES,
1752 XFS_WRITE_LOG_COUNT);
1da177e4 1753 /*
dd9f438e 1754 * Check for running out of space
1da177e4
LT
1755 */
1756 if (error) {
1757 /*
1758 * Free the transaction structure.
1759 */
1760 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
1761 xfs_trans_cancel(tp, 0);
1762 break;
1763 }
1764 xfs_ilock(ip, XFS_ILOCK_EXCL);
7d095257
CH
1765 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
1766 0, quota_flag);
1da177e4
LT
1767 if (error)
1768 goto error1;
1769
ddc3415a 1770 xfs_trans_ijoin(tp, ip, 0);
1da177e4 1771
9d87c319 1772 xfs_bmap_init(&free_list, &firstfsb);
c0dc7828
DC
1773 error = xfs_bmapi_write(tp, ip, startoffset_fsb,
1774 allocatesize_fsb, alloc_type, &firstfsb,
1775 0, imapp, &nimaps, &free_list);
1da177e4
LT
1776 if (error) {
1777 goto error0;
1778 }
1779
1780 /*
dd9f438e 1781 * Complete the transaction
1da177e4 1782 */
f7c99b6f 1783 error = xfs_bmap_finish(&tp, &free_list, &committed);
1da177e4
LT
1784 if (error) {
1785 goto error0;
1786 }
1787
1c72bf90 1788 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1da177e4
LT
1789 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1790 if (error) {
1791 break;
1792 }
1793
1794 allocated_fsb = imapp->br_blockcount;
1795
dd9f438e 1796 if (nimaps == 0) {
1da177e4
LT
1797 error = XFS_ERROR(ENOSPC);
1798 break;
1799 }
1800
1801 startoffset_fsb += allocated_fsb;
1802 allocatesize_fsb -= allocated_fsb;
1803 }
1da177e4
LT
1804
1805 return error;
1806
dd9f438e 1807error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
1da177e4 1808 xfs_bmap_cancel(&free_list);
ea562ed6 1809 xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
dd9f438e
NS
1810
1811error1: /* Just cancel transaction */
1da177e4
LT
1812 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1813 xfs_iunlock(ip, XFS_ILOCK_EXCL);
288699fe 1814 return error;
1da177e4
LT
1815}
1816
1817/*
1818 * Zero file bytes between startoff and endoff inclusive.
1819 * The iolock is held exclusive and no blocks are buffered.
2fd6f6ec
LM
1820 *
1821 * This function is used by xfs_free_file_space() to zero
1822 * partial blocks when the range to free is not block aligned.
1823 * When unreserving space with boundaries that are not block
1824 * aligned we round up the start and round down the end
1825 * boundaries and then use this function to zero the parts of
1826 * the blocks that got dropped during the rounding.
1da177e4
LT
1827 */
1828STATIC int
1829xfs_zero_remaining_bytes(
1830 xfs_inode_t *ip,
1831 xfs_off_t startoff,
1832 xfs_off_t endoff)
1833{
1834 xfs_bmbt_irec_t imap;
1835 xfs_fileoff_t offset_fsb;
1836 xfs_off_t lastoffset;
1837 xfs_off_t offset;
1838 xfs_buf_t *bp;
1839 xfs_mount_t *mp = ip->i_mount;
1840 int nimap;
1841 int error = 0;
1842
2fd6f6ec
LM
1843 /*
1844 * Avoid doing I/O beyond eof - it's not necessary
1845 * since nothing can read beyond eof. The space will
1846 * be zeroed when the file is extended anyway.
1847 */
ce7ae151 1848 if (startoff >= XFS_ISIZE(ip))
2fd6f6ec
LM
1849 return 0;
1850
ce7ae151
CH
1851 if (endoff > XFS_ISIZE(ip))
1852 endoff = XFS_ISIZE(ip);
2fd6f6ec 1853
686865f7
DC
1854 bp = xfs_buf_get_uncached(XFS_IS_REALTIME_INODE(ip) ?
1855 mp->m_rtdev_targp : mp->m_ddev_targp,
aa5c158e 1856 BTOBB(mp->m_sb.sb_blocksize), 0);
c6422617
LM
1857 if (!bp)
1858 return XFS_ERROR(ENOMEM);
1da177e4 1859
c8da0faf
CH
1860 xfs_buf_unlock(bp);
1861
1da177e4
LT
1862 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
1863 offset_fsb = XFS_B_TO_FSBT(mp, offset);
1864 nimap = 1;
5c8ed202 1865 error = xfs_bmapi_read(ip, offset_fsb, 1, &imap, &nimap, 0);
1da177e4
LT
1866 if (error || nimap < 1)
1867 break;
1868 ASSERT(imap.br_blockcount >= 1);
1869 ASSERT(imap.br_startoff == offset_fsb);
1870 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
1871 if (lastoffset > endoff)
1872 lastoffset = endoff;
1873 if (imap.br_startblock == HOLESTARTBLOCK)
1874 continue;
1875 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
1876 if (imap.br_state == XFS_EXT_UNWRITTEN)
1877 continue;
1878 XFS_BUF_UNDONE(bp);
1879 XFS_BUF_UNWRITE(bp);
1880 XFS_BUF_READ(bp);
9d87c319 1881 XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock));
1da177e4 1882 xfsbdstrat(mp, bp);
1a1a3e97 1883 error = xfs_buf_iowait(bp);
d64e31a2 1884 if (error) {
901796af
CH
1885 xfs_buf_ioerror_alert(bp,
1886 "xfs_zero_remaining_bytes(read)");
1da177e4
LT
1887 break;
1888 }
62926044 1889 memset(bp->b_addr +
1da177e4
LT
1890 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
1891 0, lastoffset - offset + 1);
1892 XFS_BUF_UNDONE(bp);
1893 XFS_BUF_UNREAD(bp);
1894 XFS_BUF_WRITE(bp);
1895 xfsbdstrat(mp, bp);
1a1a3e97 1896 error = xfs_buf_iowait(bp);
d64e31a2 1897 if (error) {
901796af
CH
1898 xfs_buf_ioerror_alert(bp,
1899 "xfs_zero_remaining_bytes(write)");
1da177e4
LT
1900 break;
1901 }
1902 }
1903 xfs_buf_free(bp);
1904 return error;
1905}
1906
1907/*
1908 * xfs_free_file_space()
1909 * This routine frees disk space for the given file.
1910 *
1911 * This routine is only called by xfs_change_file_space
1912 * for an UNRESVSP type call.
1913 *
1914 * RETURNS:
1915 * 0 on success
1916 * errno on error
1917 *
1918 */
1919STATIC int
1920xfs_free_file_space(
1921 xfs_inode_t *ip,
1922 xfs_off_t offset,
1923 xfs_off_t len,
1924 int attr_flags)
1925{
1926 int committed;
1927 int done;
1da177e4
LT
1928 xfs_fileoff_t endoffset_fsb;
1929 int error;
1930 xfs_fsblock_t firstfsb;
1931 xfs_bmap_free_t free_list;
1da177e4
LT
1932 xfs_bmbt_irec_t imap;
1933 xfs_off_t ioffset;
1934 xfs_extlen_t mod=0;
1935 xfs_mount_t *mp;
1936 int nimap;
1937 uint resblks;
673cdf5c 1938 uint rounding;
1da177e4
LT
1939 int rt;
1940 xfs_fileoff_t startoffset_fsb;
1941 xfs_trans_t *tp;
5fcbab35 1942 int need_iolock = 1;
1da177e4 1943
1da177e4
LT
1944 mp = ip->i_mount;
1945
cca28fb8 1946 trace_xfs_free_file_space(ip);
bd5a876a 1947
7d095257
CH
1948 error = xfs_qm_dqattach(ip, 0);
1949 if (error)
1da177e4
LT
1950 return error;
1951
1952 error = 0;
1953 if (len <= 0) /* if nothing being freed */
1954 return error;
71ddabb9 1955 rt = XFS_IS_REALTIME_INODE(ip);
1da177e4 1956 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
288699fe 1957 endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
1da177e4 1958
0f285c8a 1959 if (attr_flags & XFS_ATTR_NOLOCK)
5fcbab35 1960 need_iolock = 0;
9fa8046f 1961 if (need_iolock) {
1da177e4 1962 xfs_ilock(ip, XFS_IOLOCK_EXCL);
25e41b3d 1963 /* wait for the completion of any pending DIOs */
4a06fd26 1964 inode_dio_wait(VFS_I(ip));
9fa8046f 1965 }
5fcbab35 1966
e6a4b37f 1967 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
1da177e4 1968 ioffset = offset & ~(rounding - 1);
bd5a876a 1969
df80c933 1970 if (VN_CACHED(VFS_I(ip)) != 0) {
e6a4b37f 1971 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
d3cf2094
LM
1972 if (error)
1973 goto out_unlock_iolock;
bd5a876a
CH
1974 }
1975
1da177e4
LT
1976 /*
1977 * Need to zero the stuff we're not freeing, on disk.
9da096fd 1978 * If it's a realtime file & can't use unwritten extents then we
1da177e4
LT
1979 * actually need to zero the extent edges. Otherwise xfs_bunmapi
1980 * will take care of it for us.
1981 */
62118709 1982 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
1da177e4 1983 nimap = 1;
5c8ed202
DC
1984 error = xfs_bmapi_read(ip, startoffset_fsb, 1,
1985 &imap, &nimap, 0);
1da177e4
LT
1986 if (error)
1987 goto out_unlock_iolock;
1988 ASSERT(nimap == 0 || nimap == 1);
1989 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
1990 xfs_daddr_t block;
1991
1992 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
1993 block = imap.br_startblock;
1994 mod = do_div(block, mp->m_sb.sb_rextsize);
1995 if (mod)
1996 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
1997 }
1998 nimap = 1;
5c8ed202
DC
1999 error = xfs_bmapi_read(ip, endoffset_fsb - 1, 1,
2000 &imap, &nimap, 0);
1da177e4
LT
2001 if (error)
2002 goto out_unlock_iolock;
2003 ASSERT(nimap == 0 || nimap == 1);
2004 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2005 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2006 mod++;
2007 if (mod && (mod != mp->m_sb.sb_rextsize))
2008 endoffset_fsb -= mod;
2009 }
2010 }
2011 if ((done = (endoffset_fsb <= startoffset_fsb)))
2012 /*
2013 * One contiguous piece to clear
2014 */
2015 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
2016 else {
2017 /*
2018 * Some full blocks, possibly two pieces to clear
2019 */
2020 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
2021 error = xfs_zero_remaining_bytes(ip, offset,
2022 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
2023 if (!error &&
2024 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
2025 error = xfs_zero_remaining_bytes(ip,
2026 XFS_FSB_TO_B(mp, endoffset_fsb),
2027 offset + len - 1);
2028 }
2029
2030 /*
2031 * free file space until done or until there is an error
2032 */
2033 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2034 while (!error && !done) {
2035
2036 /*
91ebecc7
DC
2037 * allocate and setup the transaction. Allow this
2038 * transaction to dip into the reserve blocks to ensure
2039 * the freeing of the space succeeds at ENOSPC.
1da177e4
LT
2040 */
2041 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
91ebecc7 2042 tp->t_flags |= XFS_TRANS_RESERVE;
1da177e4
LT
2043 error = xfs_trans_reserve(tp,
2044 resblks,
2045 XFS_WRITE_LOG_RES(mp),
2046 0,
2047 XFS_TRANS_PERM_LOG_RES,
2048 XFS_WRITE_LOG_COUNT);
2049
2050 /*
2051 * check for running out of space
2052 */
2053 if (error) {
2054 /*
2055 * Free the transaction structure.
2056 */
2057 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2058 xfs_trans_cancel(tp, 0);
2059 break;
2060 }
2061 xfs_ilock(ip, XFS_ILOCK_EXCL);
7d095257
CH
2062 error = xfs_trans_reserve_quota(tp, mp,
2063 ip->i_udquot, ip->i_gdquot,
2064 resblks, 0, XFS_QMOPT_RES_REGBLKS);
1da177e4
LT
2065 if (error)
2066 goto error1;
2067
ddc3415a 2068 xfs_trans_ijoin(tp, ip, 0);
1da177e4
LT
2069
2070 /*
2071 * issue the bunmapi() call to free the blocks
2072 */
9d87c319 2073 xfs_bmap_init(&free_list, &firstfsb);
541d7d3c 2074 error = xfs_bunmapi(tp, ip, startoffset_fsb,
1da177e4 2075 endoffset_fsb - startoffset_fsb,
b4e9181e 2076 0, 2, &firstfsb, &free_list, &done);
1da177e4
LT
2077 if (error) {
2078 goto error0;
2079 }
2080
2081 /*
2082 * complete the transaction
2083 */
f7c99b6f 2084 error = xfs_bmap_finish(&tp, &free_list, &committed);
1da177e4
LT
2085 if (error) {
2086 goto error0;
2087 }
2088
1c72bf90 2089 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1da177e4
LT
2090 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2091 }
2092
2093 out_unlock_iolock:
2094 if (need_iolock)
2095 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
2096 return error;
2097
2098 error0:
2099 xfs_bmap_cancel(&free_list);
2100 error1:
2101 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2102 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
2103 XFS_ILOCK_EXCL);
2104 return error;
2105}
2106
2107/*
2108 * xfs_change_file_space()
2109 * This routine allocates or frees disk space for the given file.
2110 * The user specified parameters are checked for alignment and size
2111 * limitations.
2112 *
2113 * RETURNS:
2114 * 0 on success
2115 * errno on error
2116 *
2117 */
2118int
2119xfs_change_file_space(
993386c1 2120 xfs_inode_t *ip,
1da177e4
LT
2121 int cmd,
2122 xfs_flock64_t *bf,
2123 xfs_off_t offset,
1da177e4
LT
2124 int attr_flags)
2125{
993386c1 2126 xfs_mount_t *mp = ip->i_mount;
1da177e4
LT
2127 int clrprealloc;
2128 int error;
2129 xfs_fsize_t fsize;
1da177e4
LT
2130 int setprealloc;
2131 xfs_off_t startoffset;
2132 xfs_off_t llen;
2133 xfs_trans_t *tp;
0f285c8a 2134 struct iattr iattr;
44722352 2135 int prealloc_type;
1da177e4 2136
993386c1 2137 if (!S_ISREG(ip->i_d.di_mode))
1da177e4
LT
2138 return XFS_ERROR(EINVAL);
2139
1da177e4
LT
2140 switch (bf->l_whence) {
2141 case 0: /*SEEK_SET*/
2142 break;
2143 case 1: /*SEEK_CUR*/
2144 bf->l_start += offset;
2145 break;
2146 case 2: /*SEEK_END*/
ce7ae151 2147 bf->l_start += XFS_ISIZE(ip);
1da177e4
LT
2148 break;
2149 default:
2150 return XFS_ERROR(EINVAL);
2151 }
2152
2153 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
2154
32972383
DC
2155 if (bf->l_start < 0 ||
2156 bf->l_start > mp->m_super->s_maxbytes ||
2157 bf->l_start + llen < 0 ||
2158 bf->l_start + llen > mp->m_super->s_maxbytes)
1da177e4
LT
2159 return XFS_ERROR(EINVAL);
2160
2161 bf->l_whence = 0;
2162
2163 startoffset = bf->l_start;
ce7ae151 2164 fsize = XFS_ISIZE(ip);
1da177e4
LT
2165
2166 /*
2167 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
2168 * file space.
2169 * These calls do NOT zero the data space allocated to the file,
2170 * nor do they change the file size.
2171 *
2172 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
2173 * space.
2174 * These calls cause the new file data to be zeroed and the file
2175 * size to be changed.
2176 */
2177 setprealloc = clrprealloc = 0;
44722352 2178 prealloc_type = XFS_BMAPI_PREALLOC;
1da177e4
LT
2179
2180 switch (cmd) {
44722352
DC
2181 case XFS_IOC_ZERO_RANGE:
2182 prealloc_type |= XFS_BMAPI_CONVERT;
2183 xfs_tosspages(ip, startoffset, startoffset + bf->l_len, 0);
2184 /* FALLTHRU */
1da177e4
LT
2185 case XFS_IOC_RESVSP:
2186 case XFS_IOC_RESVSP64:
2187 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
44722352 2188 prealloc_type, attr_flags);
1da177e4
LT
2189 if (error)
2190 return error;
2191 setprealloc = 1;
2192 break;
2193
2194 case XFS_IOC_UNRESVSP:
2195 case XFS_IOC_UNRESVSP64:
2196 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
2197 attr_flags)))
2198 return error;
2199 break;
2200
2201 case XFS_IOC_ALLOCSP:
2202 case XFS_IOC_ALLOCSP64:
2203 case XFS_IOC_FREESP:
2204 case XFS_IOC_FREESP64:
bc4010ec
DC
2205 /*
2206 * These operations actually do IO when extending the file, but
2207 * the allocation is done seperately to the zeroing that is
2208 * done. This set of operations need to be serialised against
2209 * other IO operations, such as truncate and buffered IO. We
2210 * need to take the IOLOCK here to serialise the allocation and
2211 * zeroing IO to prevent other IOLOCK holders (e.g. getbmap,
2212 * truncate, direct IO) from racing against the transient
2213 * allocated but not written state we can have here.
2214 */
2215 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1da177e4
LT
2216 if (startoffset > fsize) {
2217 error = xfs_alloc_file_space(ip, fsize,
bc4010ec
DC
2218 startoffset - fsize, 0,
2219 attr_flags | XFS_ATTR_NOLOCK);
2220 if (error) {
2221 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1da177e4 2222 break;
bc4010ec 2223 }
1da177e4
LT
2224 }
2225
0f285c8a
CH
2226 iattr.ia_valid = ATTR_SIZE;
2227 iattr.ia_size = startoffset;
1da177e4 2228
bc4010ec
DC
2229 error = xfs_setattr_size(ip, &iattr,
2230 attr_flags | XFS_ATTR_NOLOCK);
2231 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1da177e4
LT
2232
2233 if (error)
2234 return error;
2235
2236 clrprealloc = 1;
2237 break;
2238
2239 default:
2240 ASSERT(0);
2241 return XFS_ERROR(EINVAL);
2242 }
2243
2244 /*
2245 * update the inode timestamp, mode, and prealloc flag bits
2246 */
2247 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
2248
2249 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
2250 0, 0, 0))) {
2251 /* ASSERT(0); */
2252 xfs_trans_cancel(tp, 0);
2253 return error;
2254 }
2255
2256 xfs_ilock(ip, XFS_ILOCK_EXCL);
ddc3415a 2257 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1da177e4 2258
0f285c8a 2259 if ((attr_flags & XFS_ATTR_DMI) == 0) {
1da177e4
LT
2260 ip->i_d.di_mode &= ~S_ISUID;
2261
2262 /*
2263 * Note that we don't have to worry about mandatory
2264 * file locking being disabled here because we only
2265 * clear the S_ISGID bit if the Group execute bit is
2266 * on, but if it was on then mandatory locking wouldn't
2267 * have been enabled.
2268 */
2269 if (ip->i_d.di_mode & S_IXGRP)
2270 ip->i_d.di_mode &= ~S_ISGID;
2271
dcd79a14 2272 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1da177e4
LT
2273 }
2274 if (setprealloc)
2275 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
2276 else if (clrprealloc)
2277 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
2278
2279 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
82878897
DC
2280 if (attr_flags & XFS_ATTR_SYNC)
2281 xfs_trans_set_sync(tp);
23bb0be1 2282 return xfs_trans_commit(tp, 0);
1da177e4 2283}
This page took 0.683169 seconds and 5 git commands to generate.