]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/xfs/xfs_vnodeops.c
xfs: block callers of xfs_flush_inodes() correctly
[net-next-2.6.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 23#include "xfs_log.h"
a844f451 24#include "xfs_inum.h"
1da177e4
LT
25#include "xfs_trans.h"
26#include "xfs_sb.h"
27#include "xfs_ag.h"
1da177e4
LT
28#include "xfs_dir2.h"
29#include "xfs_dmapi.h"
30#include "xfs_mount.h"
a844f451 31#include "xfs_da_btree.h"
1da177e4 32#include "xfs_bmap_btree.h"
a844f451 33#include "xfs_alloc_btree.h"
1da177e4 34#include "xfs_ialloc_btree.h"
1da177e4 35#include "xfs_dir2_sf.h"
a844f451 36#include "xfs_attr_sf.h"
1da177e4 37#include "xfs_dinode.h"
1da177e4 38#include "xfs_inode.h"
a844f451 39#include "xfs_inode_item.h"
a844f451
NS
40#include "xfs_itable.h"
41#include "xfs_btree.h"
42#include "xfs_ialloc.h"
43#include "xfs_alloc.h"
1da177e4 44#include "xfs_bmap.h"
1da177e4
LT
45#include "xfs_attr.h"
46#include "xfs_rw.h"
1da177e4 47#include "xfs_error.h"
1da177e4
LT
48#include "xfs_quota.h"
49#include "xfs_utils.h"
a844f451 50#include "xfs_rtalloc.h"
1da177e4 51#include "xfs_trans_space.h"
1da177e4 52#include "xfs_log_priv.h"
2a82b8be 53#include "xfs_filestream.h"
993386c1 54#include "xfs_vnodeops.h"
1da177e4 55
1da177e4
LT
56int
57xfs_setattr(
0f285c8a
CH
58 struct xfs_inode *ip,
59 struct iattr *iattr,
ea5a3dc8 60 int flags)
1da177e4 61{
993386c1 62 xfs_mount_t *mp = ip->i_mount;
e4f75291 63 struct inode *inode = VFS_I(ip);
0f285c8a 64 int mask = iattr->ia_valid;
1da177e4 65 xfs_trans_t *tp;
1da177e4
LT
66 int code;
67 uint lock_flags;
68 uint commit_flags=0;
69 uid_t uid=0, iuid=0;
70 gid_t gid=0, igid=0;
71 int timeflags = 0;
1da177e4 72 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
5fcbab35 73 int need_iolock = 1;
1da177e4 74
cf441eeb 75 xfs_itrace_entry(ip);
1da177e4 76
bd186aa9 77 if (mp->m_flags & XFS_MOUNT_RDONLY)
1da177e4
LT
78 return XFS_ERROR(EROFS);
79
1da177e4
LT
80 if (XFS_FORCED_SHUTDOWN(mp))
81 return XFS_ERROR(EIO);
82
c4cd747e
CH
83 code = -inode_change_ok(inode, iattr);
84 if (code)
85 return code;
86
1da177e4
LT
87 olddquot1 = olddquot2 = NULL;
88 udqp = gdqp = NULL;
89
90 /*
91 * If disk quotas is on, we make sure that the dquots do exist on disk,
92 * before we start any other transactions. Trying to do this later
93 * is messy. We don't care to take a readlock to look at the ids
94 * in inode here, because we can't hold it across the trans_reserve.
95 * If the IDs do change before we take the ilock, we're covered
96 * because the i_*dquot fields will get updated anyway.
97 */
0f285c8a 98 if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
1da177e4
LT
99 uint qflags = 0;
100
0f285c8a
CH
101 if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
102 uid = iattr->ia_uid;
1da177e4
LT
103 qflags |= XFS_QMOPT_UQUOTA;
104 } else {
105 uid = ip->i_d.di_uid;
106 }
0f285c8a
CH
107 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
108 gid = iattr->ia_gid;
1da177e4
LT
109 qflags |= XFS_QMOPT_GQUOTA;
110 } else {
111 gid = ip->i_d.di_gid;
112 }
25fe55e8 113
1da177e4
LT
114 /*
115 * We take a reference when we initialize udqp and gdqp,
116 * so it is important that we never blindly double trip on
117 * the same variable. See xfs_create() for an example.
118 */
119 ASSERT(udqp == NULL);
120 ASSERT(gdqp == NULL);
25fe55e8
CH
121 code = XFS_QM_DQVOPALLOC(mp, ip, uid, gid, ip->i_d.di_projid,
122 qflags, &udqp, &gdqp);
1da177e4 123 if (code)
014c2544 124 return code;
1da177e4
LT
125 }
126
127 /*
128 * For the other attributes, we acquire the inode lock and
129 * first do an error checking pass.
130 */
131 tp = NULL;
132 lock_flags = XFS_ILOCK_EXCL;
0f285c8a 133 if (flags & XFS_ATTR_NOLOCK)
5fcbab35 134 need_iolock = 0;
0f285c8a
CH
135 if (!(mask & ATTR_SIZE)) {
136 if ((mask != (ATTR_CTIME|ATTR_ATIME|ATTR_MTIME)) ||
1da177e4
LT
137 (mp->m_flags & XFS_MOUNT_WSYNC)) {
138 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
139 commit_flags = 0;
140 if ((code = xfs_trans_reserve(tp, 0,
141 XFS_ICHANGE_LOG_RES(mp), 0,
142 0, 0))) {
143 lock_flags = 0;
144 goto error_return;
145 }
146 }
147 } else {
eb9df39d 148 if (DM_EVENT_ENABLED(ip, DM_EVENT_TRUNCATE) &&
0f285c8a 149 !(flags & XFS_ATTR_DMI)) {
1da177e4 150 int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
bc4ac74a 151 code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, ip,
0f285c8a 152 iattr->ia_size, 0, dmflags, NULL);
1da177e4
LT
153 if (code) {
154 lock_flags = 0;
155 goto error_return;
156 }
157 }
158 if (need_iolock)
159 lock_flags |= XFS_IOLOCK_EXCL;
160 }
161
162 xfs_ilock(ip, lock_flags);
163
1da177e4
LT
164 /*
165 * Change file ownership. Must be the owner or privileged.
1da177e4 166 */
0f285c8a 167 if (mask & (ATTR_UID|ATTR_GID)) {
1da177e4
LT
168 /*
169 * These IDs could have changed since we last looked at them.
170 * But, we're assured that if the ownership did change
171 * while we didn't have the inode locked, inode's dquot(s)
172 * would have changed also.
173 */
174 iuid = ip->i_d.di_uid;
1da177e4 175 igid = ip->i_d.di_gid;
0f285c8a
CH
176 gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
177 uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
1da177e4 178
1da177e4 179 /*
25fe55e8 180 * Do a quota reservation only if uid/gid is actually
1da177e4
LT
181 * going to change.
182 */
183 if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
184 (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
185 ASSERT(tp);
186 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
187 capable(CAP_FOWNER) ?
188 XFS_QMOPT_FORCE_RES : 0);
189 if (code) /* out of quota */
190 goto error_return;
191 }
192 }
193
194 /*
195 * Truncate file. Must have write permission and not be a directory.
196 */
0f285c8a 197 if (mask & ATTR_SIZE) {
1da177e4 198 /* Short circuit the truncate case for zero length files */
0f285c8a
CH
199 if (iattr->ia_size == 0 &&
200 ip->i_size == 0 && ip->i_d.di_nextents == 0) {
1da177e4
LT
201 xfs_iunlock(ip, XFS_ILOCK_EXCL);
202 lock_flags &= ~XFS_ILOCK_EXCL;
0f285c8a 203 if (mask & ATTR_CTIME)
1da177e4
LT
204 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
205 code = 0;
206 goto error_return;
207 }
208
42173f68 209 if (S_ISDIR(ip->i_d.di_mode)) {
1da177e4
LT
210 code = XFS_ERROR(EISDIR);
211 goto error_return;
42173f68 212 } else if (!S_ISREG(ip->i_d.di_mode)) {
1da177e4
LT
213 code = XFS_ERROR(EINVAL);
214 goto error_return;
215 }
c4cd747e 216
1da177e4
LT
217 /*
218 * Make sure that the dquots are attached to the inode.
219 */
c4cd747e
CH
220 code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED);
221 if (code)
1da177e4 222 goto error_return;
1da177e4 223
c4cd747e
CH
224 /*
225 * Now we can make the changes. Before we join the inode
226 * to the transaction, if ATTR_SIZE is set then take care of
227 * the part of the truncation that must be done without the
228 * inode lock. This needs to be done before joining the inode
229 * to the transaction, because the inode cannot be unlocked
230 * once it is a part of the transaction.
231 */
0f285c8a 232 if (iattr->ia_size > ip->i_size) {
61436feb
CH
233 /*
234 * Do the first part of growing a file: zero any data
235 * in the last block that is beyond the old EOF. We
236 * need to do this before the inode is joined to the
237 * transaction to modify the i_size.
238 */
0f285c8a 239 code = xfs_zero_eof(ip, iattr->ia_size, ip->i_size);
374e2ac3 240 }
1da177e4 241 xfs_iunlock(ip, XFS_ILOCK_EXCL);
c32676ee
DC
242
243 /*
244 * We are going to log the inode size change in this
245 * transaction so any previous writes that are beyond the on
246 * disk EOF and the new EOF that have not been written out need
247 * to be written here. If we do not write the data out, we
248 * expose ourselves to the null files problem.
249 *
250 * Only flush from the on disk size to the smaller of the in
251 * memory file size or the new size as that's the range we
252 * really care about here and prevents waiting for other data
253 * not within the range we care about here.
254 */
255 if (!code &&
0f285c8a
CH
256 ip->i_size != ip->i_d.di_size &&
257 iattr->ia_size > ip->i_d.di_size) {
739bfb2a 258 code = xfs_flush_pages(ip,
0f285c8a 259 ip->i_d.di_size, iattr->ia_size,
c32676ee
DC
260 XFS_B_ASYNC, FI_NONE);
261 }
262
263 /* wait for all I/O to complete */
25e41b3d 264 xfs_ioend_wait(ip);
c32676ee 265
1da177e4 266 if (!code)
0f285c8a 267 code = xfs_itruncate_data(ip, iattr->ia_size);
1da177e4
LT
268 if (code) {
269 ASSERT(tp == NULL);
270 lock_flags &= ~XFS_ILOCK_EXCL;
271 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
272 goto error_return;
273 }
274 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
275 if ((code = xfs_trans_reserve(tp, 0,
276 XFS_ITRUNCATE_LOG_RES(mp), 0,
277 XFS_TRANS_PERM_LOG_RES,
278 XFS_ITRUNCATE_LOG_COUNT))) {
279 xfs_trans_cancel(tp, 0);
280 if (need_iolock)
281 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
282 return code;
283 }
284 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
285 xfs_ilock(ip, XFS_ILOCK_EXCL);
1da177e4 286
1da177e4
LT
287 xfs_trans_ijoin(tp, ip, lock_flags);
288 xfs_trans_ihold(tp, ip);
1da177e4 289
44d814ce
DC
290 /*
291 * Only change the c/mtime if we are changing the size
292 * or we are explicitly asked to change it. This handles
293 * the semantic difference between truncate() and ftruncate()
294 * as implemented in the VFS.
295 */
0f285c8a 296 if (iattr->ia_size != ip->i_size || (mask & ATTR_CTIME))
44d814ce
DC
297 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
298
0f285c8a
CH
299 if (iattr->ia_size > ip->i_size) {
300 ip->i_d.di_size = iattr->ia_size;
301 ip->i_size = iattr->ia_size;
302 if (!(flags & XFS_ATTR_DMI))
61436feb
CH
303 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
304 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
0f285c8a
CH
305 } else if (iattr->ia_size <= ip->i_size ||
306 (iattr->ia_size == 0 && ip->i_d.di_nextents)) {
1da177e4
LT
307 /*
308 * signal a sync transaction unless
309 * we're truncating an already unlinked
310 * file on a wsync filesystem
311 */
0f285c8a 312 code = xfs_itruncate_finish(&tp, ip, iattr->ia_size,
1da177e4
LT
313 XFS_DATA_FORK,
314 ((ip->i_d.di_nlink != 0 ||
315 !(mp->m_flags & XFS_MOUNT_WSYNC))
316 ? 1 : 0));
7d4fb40a 317 if (code)
1da177e4 318 goto abort_return;
7d4fb40a
NS
319 /*
320 * Truncated "down", so we're removing references
321 * to old data here - if we now delay flushing for
322 * a long time, we expose ourselves unduly to the
323 * notorious NULL files problem. So, we mark this
324 * vnode and flush it when the file is closed, and
325 * do not wait the usual (long) time for writeout.
326 */
b3aea4ed 327 xfs_iflags_set(ip, XFS_ITRUNCATED);
1da177e4 328 }
c4cd747e
CH
329 } else if (tp) {
330 xfs_trans_ijoin(tp, ip, lock_flags);
331 xfs_trans_ihold(tp, ip);
1da177e4
LT
332 }
333
334 /*
335 * Change file ownership. Must be the owner or privileged.
1da177e4 336 */
0f285c8a 337 if (mask & (ATTR_UID|ATTR_GID)) {
1da177e4
LT
338 /*
339 * CAP_FSETID overrides the following restrictions:
340 *
341 * The set-user-ID and set-group-ID bits of a file will be
342 * cleared upon successful return from chown()
343 */
344 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
345 !capable(CAP_FSETID)) {
346 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
347 }
348
349 /*
350 * Change the ownerships and register quota modifications
351 * in the transaction.
352 */
353 if (iuid != uid) {
354 if (XFS_IS_UQUOTA_ON(mp)) {
0f285c8a 355 ASSERT(mask & ATTR_UID);
1da177e4
LT
356 ASSERT(udqp);
357 olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
358 &ip->i_udquot, udqp);
359 }
360 ip->i_d.di_uid = uid;
f13fae2d 361 inode->i_uid = uid;
1da177e4
LT
362 }
363 if (igid != gid) {
364 if (XFS_IS_GQUOTA_ON(mp)) {
c8ad20ff 365 ASSERT(!XFS_IS_PQUOTA_ON(mp));
0f285c8a 366 ASSERT(mask & ATTR_GID);
1da177e4
LT
367 ASSERT(gdqp);
368 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
369 &ip->i_gdquot, gdqp);
370 }
371 ip->i_d.di_gid = gid;
f13fae2d 372 inode->i_gid = gid;
1da177e4 373 }
1da177e4
LT
374
375 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
376 timeflags |= XFS_ICHGTIME_CHG;
377 }
378
c4cd747e
CH
379 /*
380 * Change file access modes.
381 */
382 if (mask & ATTR_MODE) {
383 umode_t mode = iattr->ia_mode;
384
385 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
386 mode &= ~S_ISGID;
387
388 ip->i_d.di_mode &= S_IFMT;
389 ip->i_d.di_mode |= mode & ~S_IFMT;
390
391 inode->i_mode &= S_IFMT;
392 inode->i_mode |= mode & ~S_IFMT;
393
394 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
395 timeflags |= XFS_ICHGTIME_CHG;
396 }
1da177e4
LT
397
398 /*
399 * Change file access or modified times.
400 */
0f285c8a
CH
401 if (mask & (ATTR_ATIME|ATTR_MTIME)) {
402 if (mask & ATTR_ATIME) {
f13fae2d 403 inode->i_atime = iattr->ia_atime;
0f285c8a
CH
404 ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
405 ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
1da177e4 406 ip->i_update_core = 1;
1da177e4 407 }
0f285c8a 408 if (mask & ATTR_MTIME) {
f13fae2d 409 inode->i_mtime = iattr->ia_mtime;
0f285c8a
CH
410 ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
411 ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
1da177e4
LT
412 timeflags &= ~XFS_ICHGTIME_MOD;
413 timeflags |= XFS_ICHGTIME_CHG;
414 }
0f285c8a 415 if (tp && (mask & (ATTR_MTIME_SET|ATTR_ATIME_SET)))
1da177e4
LT
416 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
417 }
418
1da177e4 419 /*
0f285c8a 420 * Change file inode change time only if ATTR_CTIME set
1da177e4
LT
421 * AND we have been called by a DMI function.
422 */
423
0f285c8a 424 if ((flags & XFS_ATTR_DMI) && (mask & ATTR_CTIME)) {
f13fae2d 425 inode->i_ctime = iattr->ia_ctime;
0f285c8a
CH
426 ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
427 ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
1da177e4
LT
428 ip->i_update_core = 1;
429 timeflags &= ~XFS_ICHGTIME_CHG;
430 }
431
432 /*
433 * Send out timestamp changes that need to be set to the
434 * current time. Not done when called by a DMI function.
435 */
0f285c8a 436 if (timeflags && !(flags & XFS_ATTR_DMI))
1da177e4
LT
437 xfs_ichgtime(ip, timeflags);
438
439 XFS_STATS_INC(xs_ig_attrchg);
440
441 /*
442 * If this is a synchronous mount, make sure that the
443 * transaction goes to disk before returning to the user.
444 * This is slightly sub-optimal in that truncates require
c41564b5 445 * two sync transactions instead of one for wsync filesystems.
1da177e4
LT
446 * One for the truncate and one for the timestamps since we
447 * don't want to change the timestamps unless we're sure the
448 * truncate worked. Truncates are less than 1% of the laddis
449 * mix so this probably isn't worth the trouble to optimize.
450 */
451 code = 0;
452 if (tp) {
453 if (mp->m_flags & XFS_MOUNT_WSYNC)
454 xfs_trans_set_sync(tp);
455
1c72bf90 456 code = xfs_trans_commit(tp, commit_flags);
1da177e4
LT
457 }
458
1da177e4
LT
459 xfs_iunlock(ip, lock_flags);
460
461 /*
462 * Release any dquot(s) the inode had kept before chown.
463 */
464 XFS_QM_DQRELE(mp, olddquot1);
465 XFS_QM_DQRELE(mp, olddquot2);
466 XFS_QM_DQRELE(mp, udqp);
467 XFS_QM_DQRELE(mp, gdqp);
468
469 if (code) {
470 return code;
471 }
472
eb9df39d 473 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
0f285c8a 474 !(flags & XFS_ATTR_DMI)) {
bc4ac74a 475 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
1da177e4
LT
476 NULL, DM_RIGHT_NULL, NULL, NULL,
477 0, 0, AT_DELAY_FLAG(flags));
478 }
479 return 0;
480
481 abort_return:
482 commit_flags |= XFS_TRANS_ABORT;
483 /* FALLTHROUGH */
484 error_return:
485 XFS_QM_DQRELE(mp, udqp);
486 XFS_QM_DQRELE(mp, gdqp);
487 if (tp) {
488 xfs_trans_cancel(tp, commit_flags);
489 }
490 if (lock_flags != 0) {
491 xfs_iunlock(ip, lock_flags);
492 }
493 return code;
494}
495
7d4fb40a
NS
496/*
497 * The maximum pathlen is 1024 bytes. Since the minimum file system
498 * blocksize is 512 bytes, we can get a max of 2 extents back from
499 * bmapi.
500 */
501#define SYMLINK_MAPS 2
502
804c83c3
CH
503STATIC int
504xfs_readlink_bmap(
505 xfs_inode_t *ip,
506 char *link)
507{
508 xfs_mount_t *mp = ip->i_mount;
509 int pathlen = ip->i_d.di_size;
510 int nmaps = SYMLINK_MAPS;
511 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
512 xfs_daddr_t d;
513 int byte_cnt;
514 int n;
515 xfs_buf_t *bp;
516 int error = 0;
517
518 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
519 mval, &nmaps, NULL, NULL);
520 if (error)
521 goto out;
522
523 for (n = 0; n < nmaps; n++) {
524 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
525 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
526
527 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0);
528 error = XFS_BUF_GETERROR(bp);
529 if (error) {
530 xfs_ioerror_alert("xfs_readlink",
531 ip->i_mount, bp, XFS_BUF_ADDR(bp));
532 xfs_buf_relse(bp);
533 goto out;
534 }
535 if (pathlen < byte_cnt)
536 byte_cnt = pathlen;
537 pathlen -= byte_cnt;
538
539 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
540 xfs_buf_relse(bp);
541 }
542
543 link[ip->i_d.di_size] = '\0';
544 error = 0;
545
546 out:
547 return error;
548}
549
993386c1 550int
1da177e4 551xfs_readlink(
993386c1 552 xfs_inode_t *ip,
804c83c3 553 char *link)
1da177e4 554{
804c83c3 555 xfs_mount_t *mp = ip->i_mount;
1da177e4 556 int pathlen;
1da177e4 557 int error = 0;
1da177e4 558
cf441eeb 559 xfs_itrace_entry(ip);
1da177e4
LT
560
561 if (XFS_FORCED_SHUTDOWN(mp))
562 return XFS_ERROR(EIO);
563
564 xfs_ilock(ip, XFS_ILOCK_SHARED);
565
566 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
804c83c3 567 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
1da177e4 568
804c83c3
CH
569 pathlen = ip->i_d.di_size;
570 if (!pathlen)
571 goto out;
1da177e4
LT
572
573 if (ip->i_df.if_flags & XFS_IFINLINE) {
804c83c3
CH
574 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
575 link[pathlen] = '\0';
576 } else {
577 error = xfs_readlink_bmap(ip, link);
1da177e4
LT
578 }
579
804c83c3 580 out:
1da177e4 581 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1da177e4
LT
582 return error;
583}
584
1da177e4
LT
585/*
586 * xfs_fsync
587 *
978b7237
DC
588 * This is called to sync the inode and its data out to disk. We need to hold
589 * the I/O lock while flushing the data, and the inode lock while flushing the
590 * inode. The inode lock CANNOT be held while flushing the data, so acquire
591 * after we're done with that.
1da177e4 592 */
993386c1 593int
1da177e4 594xfs_fsync(
978b7237 595 xfs_inode_t *ip)
1da177e4 596{
1da177e4
LT
597 xfs_trans_t *tp;
598 int error;
f538d4da 599 int log_flushed = 0, changed = 1;
1da177e4 600
cf441eeb 601 xfs_itrace_entry(ip);
1da177e4 602
1da177e4
LT
603 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
604 return XFS_ERROR(EIO);
605
978b7237 606 /* capture size updates in I/O completion before writing the inode. */
2e656092 607 error = xfs_wait_on_pages(ip, 0, -1);
978b7237
DC
608 if (error)
609 return XFS_ERROR(error);
776a75fa 610
1da177e4 611 /*
978b7237
DC
612 * We always need to make sure that the required inode state is safe on
613 * disk. The vnode might be clean but we still might need to force the
614 * log because of committed transactions that haven't hit the disk yet.
615 * Likewise, there could be unflushed non-transactional changes to the
616 * inode core that have to go to disk and this requires us to issue
617 * a synchronous transaction to capture these changes correctly.
1da177e4 618 *
978b7237
DC
619 * This code relies on the assumption that if the update_* fields
620 * of the inode are clear and the inode is unpinned then it is clean
621 * and no action is required.
1da177e4
LT
622 */
623 xfs_ilock(ip, XFS_ILOCK_SHARED);
624
978b7237 625 if (!(ip->i_update_size || ip->i_update_core)) {
1da177e4 626 /*
978b7237
DC
627 * Timestamps/size haven't changed since last inode flush or
628 * inode transaction commit. That means either nothing got
629 * written or a transaction committed which caught the updates.
630 * If the latter happened and the transaction hasn't hit the
631 * disk yet, the inode will be still be pinned. If it is,
632 * force the log.
1da177e4
LT
633 */
634
635 xfs_iunlock(ip, XFS_ILOCK_SHARED);
636
637 if (xfs_ipincount(ip)) {
978b7237
DC
638 error = _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
639 XFS_LOG_FORCE | XFS_LOG_SYNC,
f538d4da
CH
640 &log_flushed);
641 } else {
642 /*
978b7237
DC
643 * If the inode is not pinned and nothing has changed
644 * we don't need to flush the cache.
f538d4da
CH
645 */
646 changed = 0;
1da177e4 647 }
1da177e4
LT
648 } else {
649 /*
978b7237
DC
650 * Kick off a transaction to log the inode core to get the
651 * updates. The sync transaction will also force the log.
1da177e4
LT
652 */
653 xfs_iunlock(ip, XFS_ILOCK_SHARED);
654 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
978b7237
DC
655 error = xfs_trans_reserve(tp, 0,
656 XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0);
657 if (error) {
1da177e4
LT
658 xfs_trans_cancel(tp, 0);
659 return error;
660 }
661 xfs_ilock(ip, XFS_ILOCK_EXCL);
662
663 /*
978b7237
DC
664 * Note - it's possible that we might have pushed ourselves out
665 * of the way during trans_reserve which would flush the inode.
666 * But there's no guarantee that the inode buffer has actually
667 * gone out yet (it's delwri). Plus the buffer could be pinned
668 * anyway if it's part of an inode in another recent
669 * transaction. So we play it safe and fire off the
670 * transaction anyway.
1da177e4
LT
671 */
672 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
673 xfs_trans_ihold(tp, ip);
674 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
978b7237 675 xfs_trans_set_sync(tp);
1c72bf90 676 error = _xfs_trans_commit(tp, 0, &log_flushed);
1da177e4
LT
677
678 xfs_iunlock(ip, XFS_ILOCK_EXCL);
679 }
f538d4da
CH
680
681 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
682 /*
683 * If the log write didn't issue an ordered tag we need
684 * to flush the disk cache for the data device now.
685 */
686 if (!log_flushed)
687 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
688
689 /*
690 * If this inode is on the RT dev we need to flush that
c41564b5 691 * cache as well.
f538d4da 692 */
71ddabb9 693 if (XFS_IS_REALTIME_INODE(ip))
f538d4da
CH
694 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
695 }
696
1da177e4
LT
697 return error;
698}
699
700/*
92dfe8d2
DC
701 * This is called by xfs_inactive to free any blocks beyond eof
702 * when the link count isn't zero and by xfs_dm_punch_hole() when
703 * punching a hole to EOF.
1da177e4 704 */
92dfe8d2
DC
705int
706xfs_free_eofblocks(
1da177e4 707 xfs_mount_t *mp,
92dfe8d2
DC
708 xfs_inode_t *ip,
709 int flags)
1da177e4
LT
710{
711 xfs_trans_t *tp;
712 int error;
713 xfs_fileoff_t end_fsb;
714 xfs_fileoff_t last_fsb;
715 xfs_filblks_t map_len;
716 int nimaps;
717 xfs_bmbt_irec_t imap;
92dfe8d2 718 int use_iolock = (flags & XFS_FREE_EOF_LOCK);
1da177e4
LT
719
720 /*
721 * Figure out if there are any blocks beyond the end
722 * of the file. If not, then there is nothing to do.
723 */
ba87ea69 724 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
1da177e4
LT
725 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
726 map_len = last_fsb - end_fsb;
727 if (map_len <= 0)
014c2544 728 return 0;
1da177e4
LT
729
730 nimaps = 1;
731 xfs_ilock(ip, XFS_ILOCK_SHARED);
541d7d3c 732 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
3e57ecf6 733 NULL, 0, &imap, &nimaps, NULL, NULL);
1da177e4
LT
734 xfs_iunlock(ip, XFS_ILOCK_SHARED);
735
736 if (!error && (nimaps != 0) &&
68bdb6ea
YL
737 (imap.br_startblock != HOLESTARTBLOCK ||
738 ip->i_delayed_blks)) {
1da177e4
LT
739 /*
740 * Attach the dquots to the inode up front.
741 */
742 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
014c2544 743 return error;
1da177e4
LT
744
745 /*
746 * There are blocks after the end of file.
747 * Free them up now by truncating the file to
748 * its current size.
749 */
750 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
751
752 /*
753 * Do the xfs_itruncate_start() call before
754 * reserving any log space because
755 * itruncate_start will call into the buffer
756 * cache and we can't
757 * do that within a transaction.
758 */
92dfe8d2
DC
759 if (use_iolock)
760 xfs_ilock(ip, XFS_IOLOCK_EXCL);
d3cf2094 761 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
ba87ea69 762 ip->i_size);
d3cf2094 763 if (error) {
87ae3c24 764 xfs_trans_cancel(tp, 0);
92dfe8d2
DC
765 if (use_iolock)
766 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
d3cf2094
LM
767 return error;
768 }
1da177e4
LT
769
770 error = xfs_trans_reserve(tp, 0,
771 XFS_ITRUNCATE_LOG_RES(mp),
772 0, XFS_TRANS_PERM_LOG_RES,
773 XFS_ITRUNCATE_LOG_COUNT);
774 if (error) {
775 ASSERT(XFS_FORCED_SHUTDOWN(mp));
776 xfs_trans_cancel(tp, 0);
777 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
014c2544 778 return error;
1da177e4
LT
779 }
780
781 xfs_ilock(ip, XFS_ILOCK_EXCL);
782 xfs_trans_ijoin(tp, ip,
783 XFS_IOLOCK_EXCL |
784 XFS_ILOCK_EXCL);
785 xfs_trans_ihold(tp, ip);
786
787 error = xfs_itruncate_finish(&tp, ip,
ba87ea69 788 ip->i_size,
1da177e4
LT
789 XFS_DATA_FORK,
790 0);
791 /*
792 * If we get an error at this point we
793 * simply don't bother truncating the file.
794 */
795 if (error) {
796 xfs_trans_cancel(tp,
797 (XFS_TRANS_RELEASE_LOG_RES |
798 XFS_TRANS_ABORT));
799 } else {
800 error = xfs_trans_commit(tp,
1c72bf90 801 XFS_TRANS_RELEASE_LOG_RES);
1da177e4 802 }
92dfe8d2
DC
803 xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
804 : XFS_ILOCK_EXCL));
1da177e4 805 }
014c2544 806 return error;
1da177e4
LT
807}
808
809/*
810 * Free a symlink that has blocks associated with it.
811 */
812STATIC int
813xfs_inactive_symlink_rmt(
814 xfs_inode_t *ip,
815 xfs_trans_t **tpp)
816{
817 xfs_buf_t *bp;
818 int committed;
819 int done;
820 int error;
821 xfs_fsblock_t first_block;
822 xfs_bmap_free_t free_list;
823 int i;
824 xfs_mount_t *mp;
825 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
826 int nmaps;
827 xfs_trans_t *ntp;
828 int size;
829 xfs_trans_t *tp;
830
831 tp = *tpp;
832 mp = ip->i_mount;
833 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
834 /*
835 * We're freeing a symlink that has some
836 * blocks allocated to it. Free the
837 * blocks here. We know that we've got
838 * either 1 or 2 extents and that we can
839 * free them all in one bunmapi call.
840 */
841 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
842 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
843 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
844 ASSERT(XFS_FORCED_SHUTDOWN(mp));
845 xfs_trans_cancel(tp, 0);
846 *tpp = NULL;
847 return error;
848 }
849 /*
850 * Lock the inode, fix the size, and join it to the transaction.
851 * Hold it so in the normal path, we still have it locked for
852 * the second transaction. In the error paths we need it
853 * held so the cancel won't rele it, see below.
854 */
855 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
856 size = (int)ip->i_d.di_size;
857 ip->i_d.di_size = 0;
858 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
859 xfs_trans_ihold(tp, ip);
860 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
861 /*
862 * Find the block(s) so we can inval and unmap them.
863 */
864 done = 0;
9d87c319 865 xfs_bmap_init(&free_list, &first_block);
e8c96f8c 866 nmaps = ARRAY_SIZE(mval);
1da177e4
LT
867 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
868 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
3e57ecf6 869 &free_list, NULL)))
1da177e4
LT
870 goto error0;
871 /*
872 * Invalidate the block(s).
873 */
874 for (i = 0; i < nmaps; i++) {
875 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
876 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
877 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
878 xfs_trans_binval(tp, bp);
879 }
880 /*
881 * Unmap the dead block(s) to the free_list.
882 */
883 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
3e57ecf6 884 &first_block, &free_list, NULL, &done)))
1da177e4
LT
885 goto error1;
886 ASSERT(done);
887 /*
888 * Commit the first transaction. This logs the EFI and the inode.
889 */
f7c99b6f 890 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
1da177e4
LT
891 goto error1;
892 /*
893 * The transaction must have been committed, since there were
894 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
895 * The new tp has the extent freeing and EFDs.
896 */
897 ASSERT(committed);
898 /*
899 * The first xact was committed, so add the inode to the new one.
900 * Mark it dirty so it will be logged and moved forward in the log as
901 * part of every commit.
902 */
903 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
904 xfs_trans_ihold(tp, ip);
905 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
906 /*
907 * Get a new, empty transaction to return to our caller.
908 */
909 ntp = xfs_trans_dup(tp);
910 /*
c41564b5 911 * Commit the transaction containing extent freeing and EFDs.
1da177e4
LT
912 * If we get an error on the commit here or on the reserve below,
913 * we need to unlock the inode since the new transaction doesn't
914 * have the inode attached.
915 */
1c72bf90 916 error = xfs_trans_commit(tp, 0);
1da177e4
LT
917 tp = ntp;
918 if (error) {
919 ASSERT(XFS_FORCED_SHUTDOWN(mp));
920 goto error0;
921 }
cc09c0dc
DC
922 /*
923 * transaction commit worked ok so we can drop the extra ticket
924 * reference that we gained in xfs_trans_dup()
925 */
926 xfs_log_ticket_put(tp->t_ticket);
927
1da177e4
LT
928 /*
929 * Remove the memory for extent descriptions (just bookkeeping).
930 */
931 if (ip->i_df.if_bytes)
932 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
933 ASSERT(ip->i_df.if_bytes == 0);
934 /*
935 * Put an itruncate log reservation in the new transaction
936 * for our caller.
937 */
938 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
939 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
940 ASSERT(XFS_FORCED_SHUTDOWN(mp));
941 goto error0;
942 }
943 /*
944 * Return with the inode locked but not joined to the transaction.
945 */
946 *tpp = tp;
947 return 0;
948
949 error1:
950 xfs_bmap_cancel(&free_list);
951 error0:
952 /*
953 * Have to come here with the inode locked and either
954 * (held and in the transaction) or (not in the transaction).
955 * If the inode isn't held then cancel would iput it, but
956 * that's wrong since this is inactive and the vnode ref
957 * count is 0 already.
958 * Cancel won't do anything to the inode if held, but it still
959 * needs to be locked until the cancel is done, if it was
960 * joined to the transaction.
961 */
962 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
963 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
964 *tpp = NULL;
965 return error;
966
967}
968
969STATIC int
970xfs_inactive_symlink_local(
971 xfs_inode_t *ip,
972 xfs_trans_t **tpp)
973{
974 int error;
975
976 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
977 /*
978 * We're freeing a symlink which fit into
979 * the inode. Just free the memory used
980 * to hold the old symlink.
981 */
982 error = xfs_trans_reserve(*tpp, 0,
983 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
984 0, XFS_TRANS_PERM_LOG_RES,
985 XFS_ITRUNCATE_LOG_COUNT);
986
987 if (error) {
988 xfs_trans_cancel(*tpp, 0);
989 *tpp = NULL;
014c2544 990 return error;
1da177e4
LT
991 }
992 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
993
994 /*
995 * Zero length symlinks _can_ exist.
996 */
997 if (ip->i_df.if_bytes > 0) {
998 xfs_idata_realloc(ip,
999 -(ip->i_df.if_bytes),
1000 XFS_DATA_FORK);
1001 ASSERT(ip->i_df.if_bytes == 0);
1002 }
014c2544 1003 return 0;
1da177e4
LT
1004}
1005
1da177e4
LT
1006STATIC int
1007xfs_inactive_attrs(
1008 xfs_inode_t *ip,
1009 xfs_trans_t **tpp)
1010{
1011 xfs_trans_t *tp;
1012 int error;
1013 xfs_mount_t *mp;
1014
579aa9ca 1015 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1da177e4
LT
1016 tp = *tpp;
1017 mp = ip->i_mount;
1018 ASSERT(ip->i_d.di_forkoff != 0);
e5720eec 1019 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1da177e4 1020 xfs_iunlock(ip, XFS_ILOCK_EXCL);
e5720eec
DC
1021 if (error)
1022 goto error_unlock;
1da177e4
LT
1023
1024 error = xfs_attr_inactive(ip);
e5720eec
DC
1025 if (error)
1026 goto error_unlock;
1da177e4
LT
1027
1028 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1029 error = xfs_trans_reserve(tp, 0,
1030 XFS_IFREE_LOG_RES(mp),
1031 0, XFS_TRANS_PERM_LOG_RES,
1032 XFS_INACTIVE_LOG_COUNT);
e5720eec
DC
1033 if (error)
1034 goto error_cancel;
1da177e4
LT
1035
1036 xfs_ilock(ip, XFS_ILOCK_EXCL);
1037 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1038 xfs_trans_ihold(tp, ip);
1039 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1040
1041 ASSERT(ip->i_d.di_anextents == 0);
1042
1043 *tpp = tp;
014c2544 1044 return 0;
e5720eec
DC
1045
1046error_cancel:
1047 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1048 xfs_trans_cancel(tp, 0);
1049error_unlock:
1050 *tpp = NULL;
1051 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1052 return error;
1da177e4
LT
1053}
1054
993386c1 1055int
1da177e4 1056xfs_release(
993386c1 1057 xfs_inode_t *ip)
1da177e4 1058{
993386c1 1059 xfs_mount_t *mp = ip->i_mount;
1da177e4
LT
1060 int error;
1061
42173f68 1062 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
1da177e4 1063 return 0;
1da177e4
LT
1064
1065 /* If this is a read-only mount, don't do this (would generate I/O) */
bd186aa9 1066 if (mp->m_flags & XFS_MOUNT_RDONLY)
1da177e4
LT
1067 return 0;
1068
2a82b8be 1069 if (!XFS_FORCED_SHUTDOWN(mp)) {
b3aea4ed
CH
1070 int truncated;
1071
2a82b8be
DC
1072 /*
1073 * If we are using filestreams, and we have an unlinked
1074 * file that we are processing the last close on, then nothing
1075 * will be able to reopen and write to this file. Purge this
1076 * inode from the filestreams cache so that it doesn't delay
1077 * teardown of the inode.
1078 */
1079 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1080 xfs_filestream_deassociate(ip);
1081
fbf3ce8d
CH
1082 /*
1083 * If we previously truncated this file and removed old data
1084 * in the process, we want to initiate "early" writeout on
1085 * the last close. This is an attempt to combat the notorious
1086 * NULL files problem which is particularly noticable from a
1087 * truncate down, buffered (re-)write (delalloc), followed by
1088 * a crash. What we are effectively doing here is
1089 * significantly reducing the time window where we'd otherwise
1090 * be exposed to that problem.
1091 */
09262b43 1092 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
df80c933 1093 if (truncated && VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0)
739bfb2a 1094 xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
fbf3ce8d
CH
1095 }
1096
1da177e4
LT
1097 if (ip->i_d.di_nlink != 0) {
1098 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
df80c933 1099 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
68bdb6ea 1100 ip->i_delayed_blks > 0)) &&
1da177e4 1101 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
dd9f438e
NS
1102 (!(ip->i_d.di_flags &
1103 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
92dfe8d2
DC
1104 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1105 if (error)
014c2544 1106 return error;
1da177e4
LT
1107 }
1108 }
1109
1110 return 0;
1111}
1112
1113/*
1114 * xfs_inactive
1115 *
1116 * This is called when the vnode reference count for the vnode
1117 * goes to zero. If the file has been unlinked, then it must
1118 * now be truncated. Also, we clear all of the read-ahead state
1119 * kept for the inode here since the file is now closed.
1120 */
993386c1 1121int
1da177e4 1122xfs_inactive(
993386c1 1123 xfs_inode_t *ip)
1da177e4 1124{
67fcaa73 1125 xfs_bmap_free_t free_list;
1da177e4
LT
1126 xfs_fsblock_t first_block;
1127 int committed;
1128 xfs_trans_t *tp;
1129 xfs_mount_t *mp;
1130 int error;
1131 int truncate;
1132
cf441eeb 1133 xfs_itrace_entry(ip);
1da177e4 1134
1da177e4
LT
1135 /*
1136 * If the inode is already free, then there can be nothing
1137 * to clean up here.
1138 */
cb4c8cc1 1139 if (ip->i_d.di_mode == 0 || is_bad_inode(VFS_I(ip))) {
1da177e4
LT
1140 ASSERT(ip->i_df.if_real_bytes == 0);
1141 ASSERT(ip->i_df.if_broot_bytes == 0);
1142 return VN_INACTIVE_CACHE;
1143 }
1144
1145 /*
1146 * Only do a truncate if it's a regular file with
1147 * some actual space in it. It's OK to look at the
1148 * inode's fields without the lock because we're the
1149 * only one with a reference to the inode.
1150 */
1151 truncate = ((ip->i_d.di_nlink == 0) &&
ba87ea69
LM
1152 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1153 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
1da177e4
LT
1154 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1155
1156 mp = ip->i_mount;
1157
bc4ac74a
CH
1158 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY))
1159 XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL);
1da177e4
LT
1160
1161 error = 0;
1162
1163 /* If this is a read-only mount, don't do this (would generate I/O) */
bd186aa9 1164 if (mp->m_flags & XFS_MOUNT_RDONLY)
1da177e4
LT
1165 goto out;
1166
1167 if (ip->i_d.di_nlink != 0) {
1168 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
df80c933 1169 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
68bdb6ea 1170 ip->i_delayed_blks > 0)) &&
dd9f438e
NS
1171 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1172 (!(ip->i_d.di_flags &
1173 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1174 (ip->i_delayed_blks != 0)))) {
92dfe8d2
DC
1175 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1176 if (error)
014c2544 1177 return VN_INACTIVE_CACHE;
1da177e4
LT
1178 }
1179 goto out;
1180 }
1181
1182 ASSERT(ip->i_d.di_nlink == 0);
1183
1184 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
014c2544 1185 return VN_INACTIVE_CACHE;
1da177e4
LT
1186
1187 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1188 if (truncate) {
1189 /*
1190 * Do the xfs_itruncate_start() call before
1191 * reserving any log space because itruncate_start
1192 * will call into the buffer cache and we can't
1193 * do that within a transaction.
1194 */
1195 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1196
d3cf2094
LM
1197 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1198 if (error) {
87ae3c24 1199 xfs_trans_cancel(tp, 0);
d3cf2094
LM
1200 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1201 return VN_INACTIVE_CACHE;
1202 }
1da177e4
LT
1203
1204 error = xfs_trans_reserve(tp, 0,
1205 XFS_ITRUNCATE_LOG_RES(mp),
1206 0, XFS_TRANS_PERM_LOG_RES,
1207 XFS_ITRUNCATE_LOG_COUNT);
1208 if (error) {
1209 /* Don't call itruncate_cleanup */
1210 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1211 xfs_trans_cancel(tp, 0);
1212 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
014c2544 1213 return VN_INACTIVE_CACHE;
1da177e4
LT
1214 }
1215
1216 xfs_ilock(ip, XFS_ILOCK_EXCL);
1217 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1218 xfs_trans_ihold(tp, ip);
1219
1220 /*
1221 * normally, we have to run xfs_itruncate_finish sync.
1222 * But if filesystem is wsync and we're in the inactive
1223 * path, then we know that nlink == 0, and that the
1224 * xaction that made nlink == 0 is permanently committed
1225 * since xfs_remove runs as a synchronous transaction.
1226 */
1227 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1228 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1229
1230 if (error) {
1231 xfs_trans_cancel(tp,
1232 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1233 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
014c2544 1234 return VN_INACTIVE_CACHE;
1da177e4
LT
1235 }
1236 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1237
1238 /*
1239 * If we get an error while cleaning up a
1240 * symlink we bail out.
1241 */
1242 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1243 xfs_inactive_symlink_rmt(ip, &tp) :
1244 xfs_inactive_symlink_local(ip, &tp);
1245
1246 if (error) {
1247 ASSERT(tp == NULL);
014c2544 1248 return VN_INACTIVE_CACHE;
1da177e4
LT
1249 }
1250
1251 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1252 xfs_trans_ihold(tp, ip);
1253 } else {
1254 error = xfs_trans_reserve(tp, 0,
1255 XFS_IFREE_LOG_RES(mp),
1256 0, XFS_TRANS_PERM_LOG_RES,
1257 XFS_INACTIVE_LOG_COUNT);
1258 if (error) {
1259 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1260 xfs_trans_cancel(tp, 0);
014c2544 1261 return VN_INACTIVE_CACHE;
1da177e4
LT
1262 }
1263
1264 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1265 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1266 xfs_trans_ihold(tp, ip);
1267 }
1268
1269 /*
1270 * If there are attributes associated with the file
1271 * then blow them away now. The code calls a routine
1272 * that recursively deconstructs the attribute fork.
1273 * We need to just commit the current transaction
1274 * because we can't use it for xfs_attr_inactive().
1275 */
1276 if (ip->i_d.di_anextents > 0) {
1277 error = xfs_inactive_attrs(ip, &tp);
1278 /*
1279 * If we got an error, the transaction is already
1280 * cancelled, and the inode is unlocked. Just get out.
1281 */
1282 if (error)
014c2544 1283 return VN_INACTIVE_CACHE;
1da177e4
LT
1284 } else if (ip->i_afp) {
1285 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1286 }
1287
1288 /*
1289 * Free the inode.
1290 */
9d87c319 1291 xfs_bmap_init(&free_list, &first_block);
1da177e4
LT
1292 error = xfs_ifree(tp, ip, &free_list);
1293 if (error) {
1294 /*
1295 * If we fail to free the inode, shut down. The cancel
1296 * might do that, we need to make sure. Otherwise the
1297 * inode might be lost for a long time or forever.
1298 */
1299 if (!XFS_FORCED_SHUTDOWN(mp)) {
1300 cmn_err(CE_NOTE,
1301 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1302 error, mp->m_fsname);
7d04a335 1303 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1da177e4
LT
1304 }
1305 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1306 } else {
1307 /*
1308 * Credit the quota account(s). The inode is gone.
1309 */
1310 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1311
1312 /*
78e9da77
DC
1313 * Just ignore errors at this point. There is nothing we can
1314 * do except to try to keep going. Make sure it's not a silent
1315 * error.
1da177e4 1316 */
78e9da77
DC
1317 error = xfs_bmap_finish(&tp, &free_list, &committed);
1318 if (error)
1319 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1320 "xfs_bmap_finish() returned error %d", error);
1321 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1322 if (error)
1323 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1324 "xfs_trans_commit() returned error %d", error);
1da177e4
LT
1325 }
1326 /*
1327 * Release the dquots held by inode, if any.
1328 */
1329 XFS_QM_DQDETACH(mp, ip);
1330
1331 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1332
1333 out:
1334 return VN_INACTIVE_CACHE;
1335}
1336
384f3ced
BN
1337/*
1338 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
1339 * is allowed, otherwise it has to be an exact match. If a CI match is found,
1340 * ci_name->name will point to a the actual name (caller must free) or
1341 * will be set to NULL if an exact match is found.
1342 */
993386c1 1343int
1da177e4 1344xfs_lookup(
993386c1 1345 xfs_inode_t *dp,
556b8b16 1346 struct xfs_name *name,
384f3ced
BN
1347 xfs_inode_t **ipp,
1348 struct xfs_name *ci_name)
1da177e4 1349{
eca450b7 1350 xfs_ino_t inum;
1da177e4
LT
1351 int error;
1352 uint lock_mode;
1da177e4 1353
cf441eeb 1354 xfs_itrace_entry(dp);
1da177e4
LT
1355
1356 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1357 return XFS_ERROR(EIO);
1358
1359 lock_mode = xfs_ilock_map_shared(dp);
384f3ced 1360 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
1da177e4 1361 xfs_iunlock_map_shared(dp, lock_mode);
eca450b7
CH
1362
1363 if (error)
1364 goto out;
1365
1366 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp, 0);
1367 if (error)
384f3ced 1368 goto out_free_name;
eca450b7
CH
1369
1370 xfs_itrace_ref(*ipp);
1371 return 0;
1372
384f3ced
BN
1373out_free_name:
1374 if (ci_name)
1375 kmem_free(ci_name->name);
1376out:
eca450b7 1377 *ipp = NULL;
1da177e4
LT
1378 return error;
1379}
1380
993386c1 1381int
1da177e4 1382xfs_create(
993386c1 1383 xfs_inode_t *dp,
556b8b16 1384 struct xfs_name *name,
3e5daf05
CH
1385 mode_t mode,
1386 xfs_dev_t rdev,
979ebab1 1387 xfs_inode_t **ipp,
1da177e4
LT
1388 cred_t *credp)
1389{
517b5e8c
CH
1390 int is_dir = S_ISDIR(mode);
1391 struct xfs_mount *mp = dp->i_mount;
1392 struct xfs_inode *ip = NULL;
1393 struct xfs_trans *tp = NULL;
556b8b16 1394 int error;
1da177e4
LT
1395 xfs_bmap_free_t free_list;
1396 xfs_fsblock_t first_block;
993386c1 1397 boolean_t unlock_dp_on_error = B_FALSE;
1da177e4
LT
1398 uint cancel_flags;
1399 int committed;
1400 xfs_prid_t prid;
517b5e8c
CH
1401 struct xfs_dquot *udqp = NULL;
1402 struct xfs_dquot *gdqp = NULL;
1da177e4 1403 uint resblks;
517b5e8c
CH
1404 uint log_res;
1405 uint log_count;
1da177e4 1406
cf441eeb 1407 xfs_itrace_entry(dp);
1da177e4 1408
517b5e8c
CH
1409 if (XFS_FORCED_SHUTDOWN(mp))
1410 return XFS_ERROR(EIO);
1411
eb9df39d 1412 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
1da177e4 1413 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
bc4ac74a 1414 dp, DM_RIGHT_NULL, NULL,
556b8b16 1415 DM_RIGHT_NULL, name->name, NULL,
3e5daf05 1416 mode, 0, 0);
1da177e4
LT
1417
1418 if (error)
1419 return error;
1da177e4
LT
1420 }
1421
365ca83d
NS
1422 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1423 prid = dp->i_d.di_projid;
1da177e4 1424 else
517b5e8c 1425 prid = dfltprid;
1da177e4
LT
1426
1427 /*
1428 * Make sure that we have allocated dquot(s) on disk.
1429 */
1430 error = XFS_QM_DQVOPALLOC(mp, dp,
9e2b2dc4 1431 current_fsuid(), current_fsgid(), prid,
517b5e8c 1432 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
1da177e4
LT
1433 if (error)
1434 goto std_return;
1435
517b5e8c
CH
1436 if (is_dir) {
1437 rdev = 0;
1438 resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
1439 log_res = XFS_MKDIR_LOG_RES(mp);
1440 log_count = XFS_MKDIR_LOG_COUNT;
1441 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
1442 } else {
1443 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
1444 log_res = XFS_CREATE_LOG_RES(mp);
1445 log_count = XFS_CREATE_LOG_COUNT;
1446 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1447 }
1da177e4 1448
1da177e4 1449 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
517b5e8c 1450
1da177e4
LT
1451 /*
1452 * Initially assume that the file does not exist and
1453 * reserve the resources for that case. If that is not
1454 * the case we'll drop the one we have and get a more
1455 * appropriate transaction later.
1456 */
517b5e8c
CH
1457 error = xfs_trans_reserve(tp, resblks, log_res, 0,
1458 XFS_TRANS_PERM_LOG_RES, log_count);
1da177e4
LT
1459 if (error == ENOSPC) {
1460 resblks = 0;
517b5e8c
CH
1461 error = xfs_trans_reserve(tp, 0, log_res, 0,
1462 XFS_TRANS_PERM_LOG_RES, log_count);
1da177e4
LT
1463 }
1464 if (error) {
1465 cancel_flags = 0;
517b5e8c 1466 goto out_trans_cancel;
1da177e4
LT
1467 }
1468
f7c66ce3 1469 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
993386c1 1470 unlock_dp_on_error = B_TRUE;
1da177e4 1471
517b5e8c
CH
1472 /*
1473 * Check for directory link count overflow.
1474 */
1475 if (is_dir && dp->i_d.di_nlink >= XFS_MAXLINK) {
1476 error = XFS_ERROR(EMLINK);
1477 goto out_trans_cancel;
1478 }
1da177e4 1479
517b5e8c 1480 xfs_bmap_init(&free_list, &first_block);
1da177e4
LT
1481
1482 /*
1483 * Reserve disk quota and the inode.
1484 */
1485 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1486 if (error)
517b5e8c 1487 goto out_trans_cancel;
1da177e4 1488
556b8b16
BN
1489 error = xfs_dir_canenter(tp, dp, name, resblks);
1490 if (error)
517b5e8c
CH
1491 goto out_trans_cancel;
1492
1493 /*
1494 * A newly created regular or special file just has one directory
1495 * entry pointing to them, but a directory also the "." entry
1496 * pointing to itself.
1497 */
1498 error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev, credp,
1499 prid, resblks > 0, &ip, &committed);
1da177e4
LT
1500 if (error) {
1501 if (error == ENOSPC)
517b5e8c
CH
1502 goto out_trans_cancel;
1503 goto out_trans_abort;
1da177e4 1504 }
1da177e4
LT
1505
1506 /*
1507 * At this point, we've gotten a newly allocated inode.
1508 * It is locked (and joined to the transaction).
1509 */
517b5e8c 1510 xfs_itrace_ref(ip);
579aa9ca 1511 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1da177e4
LT
1512
1513 /*
993386c1
CH
1514 * Now we join the directory inode to the transaction. We do not do it
1515 * earlier because xfs_dir_ialloc might commit the previous transaction
1516 * (and release all the locks). An error from here on will result in
1517 * the transaction cancel unlocking dp so don't do it explicitly in the
1518 * error path.
1da177e4 1519 */
979ebab1 1520 IHOLD(dp);
1da177e4 1521 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
993386c1 1522 unlock_dp_on_error = B_FALSE;
1da177e4 1523
556b8b16 1524 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
f6c2d1fa
NS
1525 &first_block, &free_list, resblks ?
1526 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1da177e4
LT
1527 if (error) {
1528 ASSERT(error != ENOSPC);
517b5e8c 1529 goto out_trans_abort;
1da177e4
LT
1530 }
1531 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1532 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1533
517b5e8c
CH
1534 if (is_dir) {
1535 error = xfs_dir_init(tp, ip, dp);
1536 if (error)
1537 goto out_bmap_cancel;
1538
1539 error = xfs_bumplink(tp, dp);
1540 if (error)
1541 goto out_bmap_cancel;
1542 }
1543
1da177e4
LT
1544 /*
1545 * If this is a synchronous mount, make sure that the
1546 * create transaction goes to disk before returning to
1547 * the user.
1548 */
517b5e8c 1549 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1da177e4 1550 xfs_trans_set_sync(tp);
1da177e4 1551
1da177e4
LT
1552 /*
1553 * Attach the dquot(s) to the inodes and modify them incore.
1554 * These ids of the inode couldn't have changed since the new
1555 * inode has been locked ever since it was created.
1556 */
1557 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
1558
1559 /*
1560 * xfs_trans_commit normally decrements the vnode ref count
1561 * when it unlocks the inode. Since we want to return the
1562 * vnode to the caller, we bump the vnode ref count now.
1563 */
1564 IHOLD(ip);
1da177e4 1565
f7c99b6f 1566 error = xfs_bmap_finish(&tp, &free_list, &committed);
517b5e8c
CH
1567 if (error)
1568 goto out_abort_rele;
1da177e4 1569
1c72bf90 1570 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1da177e4
LT
1571 if (error) {
1572 IRELE(ip);
517b5e8c 1573 goto out_dqrele;
1da177e4
LT
1574 }
1575
1576 XFS_QM_DQRELE(mp, udqp);
1577 XFS_QM_DQRELE(mp, gdqp);
1578
979ebab1 1579 *ipp = ip;
1da177e4
LT
1580
1581 /* Fallthrough to std_return with error = 0 */
517b5e8c
CH
1582 std_return:
1583 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
1584 XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE, dp, DM_RIGHT_NULL,
1585 ip, DM_RIGHT_NULL, name->name, NULL, mode,
1586 error, 0);
1da177e4 1587 }
517b5e8c 1588
1da177e4
LT
1589 return error;
1590
517b5e8c
CH
1591 out_bmap_cancel:
1592 xfs_bmap_cancel(&free_list);
1593 out_trans_abort:
1da177e4 1594 cancel_flags |= XFS_TRANS_ABORT;
517b5e8c
CH
1595 out_trans_cancel:
1596 xfs_trans_cancel(tp, cancel_flags);
1597 out_dqrele:
1da177e4
LT
1598 XFS_QM_DQRELE(mp, udqp);
1599 XFS_QM_DQRELE(mp, gdqp);
1600
993386c1
CH
1601 if (unlock_dp_on_error)
1602 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1603
1da177e4
LT
1604 goto std_return;
1605
517b5e8c 1606 out_abort_rele:
1da177e4
LT
1607 /*
1608 * Wait until after the current transaction is aborted to
1609 * release the inode. This prevents recursive transactions
1610 * and deadlocks from xfs_inactive.
1611 */
517b5e8c 1612 xfs_bmap_cancel(&free_list);
1da177e4
LT
1613 cancel_flags |= XFS_TRANS_ABORT;
1614 xfs_trans_cancel(tp, cancel_flags);
1615 IRELE(ip);
517b5e8c
CH
1616 unlock_dp_on_error = B_FALSE;
1617 goto out_dqrele;
1da177e4
LT
1618}
1619
1da177e4
LT
1620#ifdef DEBUG
1621int xfs_locked_n;
1622int xfs_small_retries;
1623int xfs_middle_retries;
1624int xfs_lots_retries;
1625int xfs_lock_delays;
1626#endif
1627
f7c66ce3
LM
1628/*
1629 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1630 * a different value
1631 */
1632static inline int
1633xfs_lock_inumorder(int lock_mode, int subclass)
1634{
1635 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
0f1145cc 1636 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
f7c66ce3 1637 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
0f1145cc 1638 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
f7c66ce3
LM
1639
1640 return lock_mode;
1641}
1642
1da177e4
LT
1643/*
1644 * The following routine will lock n inodes in exclusive mode.
1645 * We assume the caller calls us with the inodes in i_ino order.
1646 *
1647 * We need to detect deadlock where an inode that we lock
1648 * is in the AIL and we start waiting for another inode that is locked
1649 * by a thread in a long running transaction (such as truncate). This can
1650 * result in deadlock since the long running trans might need to wait
1651 * for the inode we just locked in order to push the tail and free space
1652 * in the log.
1653 */
1654void
1655xfs_lock_inodes(
1656 xfs_inode_t **ips,
1657 int inodes,
1da177e4
LT
1658 uint lock_mode)
1659{
1660 int attempts = 0, i, j, try_lock;
1661 xfs_log_item_t *lp;
1662
1663 ASSERT(ips && (inodes >= 2)); /* we need at least two */
1664
cfa853e4
CH
1665 try_lock = 0;
1666 i = 0;
1da177e4
LT
1667
1668again:
1669 for (; i < inodes; i++) {
1670 ASSERT(ips[i]);
1671
1672 if (i && (ips[i] == ips[i-1])) /* Already locked */
1673 continue;
1674
1675 /*
1676 * If try_lock is not set yet, make sure all locked inodes
1677 * are not in the AIL.
1678 * If any are, set try_lock to be used later.
1679 */
1680
1681 if (!try_lock) {
1682 for (j = (i - 1); j >= 0 && !try_lock; j--) {
1683 lp = (xfs_log_item_t *)ips[j]->i_itemp;
1684 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1685 try_lock++;
1686 }
1687 }
1688 }
1689
1690 /*
1691 * If any of the previous locks we have locked is in the AIL,
1692 * we must TRY to get the second and subsequent locks. If
1693 * we can't get any, we must release all we have
1694 * and try again.
1695 */
1696
1697 if (try_lock) {
1698 /* try_lock must be 0 if i is 0. */
1699 /*
1700 * try_lock means we have an inode locked
1701 * that is in the AIL.
1702 */
1703 ASSERT(i != 0);
f7c66ce3 1704 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
1da177e4
LT
1705 attempts++;
1706
1707 /*
1708 * Unlock all previous guys and try again.
1709 * xfs_iunlock will try to push the tail
1710 * if the inode is in the AIL.
1711 */
1712
1713 for(j = i - 1; j >= 0; j--) {
1714
1715 /*
1716 * Check to see if we've already
1717 * unlocked this one.
1718 * Not the first one going back,
1719 * and the inode ptr is the same.
1720 */
1721 if ((j != (i - 1)) && ips[j] ==
1722 ips[j+1])
1723 continue;
1724
1725 xfs_iunlock(ips[j], lock_mode);
1726 }
1727
1728 if ((attempts % 5) == 0) {
1729 delay(1); /* Don't just spin the CPU */
1730#ifdef DEBUG
1731 xfs_lock_delays++;
1732#endif
1733 }
1734 i = 0;
1735 try_lock = 0;
1736 goto again;
1737 }
1738 } else {
f7c66ce3 1739 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
1da177e4
LT
1740 }
1741 }
1742
1743#ifdef DEBUG
1744 if (attempts) {
1745 if (attempts < 5) xfs_small_retries++;
1746 else if (attempts < 100) xfs_middle_retries++;
1747 else xfs_lots_retries++;
1748 } else {
1749 xfs_locked_n++;
1750 }
1751#endif
1752}
1753
f9114eba
DC
1754/*
1755 * xfs_lock_two_inodes() can only be used to lock one type of lock
1756 * at a time - the iolock or the ilock, but not both at once. If
1757 * we lock both at once, lockdep will report false positives saying
1758 * we have violated locking orders.
1759 */
e1cccd91
CH
1760void
1761xfs_lock_two_inodes(
1762 xfs_inode_t *ip0,
1763 xfs_inode_t *ip1,
1764 uint lock_mode)
1765{
1766 xfs_inode_t *temp;
1767 int attempts = 0;
1768 xfs_log_item_t *lp;
1769
f9114eba
DC
1770 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1771 ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
e1cccd91
CH
1772 ASSERT(ip0->i_ino != ip1->i_ino);
1773
1774 if (ip0->i_ino > ip1->i_ino) {
1775 temp = ip0;
1776 ip0 = ip1;
1777 ip1 = temp;
1778 }
1779
1780 again:
1781 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
1782
1783 /*
1784 * If the first lock we have locked is in the AIL, we must TRY to get
1785 * the second lock. If we can't get it, we must release the first one
1786 * and try again.
1787 */
1788 lp = (xfs_log_item_t *)ip0->i_itemp;
1789 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1790 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
1791 xfs_iunlock(ip0, lock_mode);
1792 if ((++attempts % 5) == 0)
1793 delay(1); /* Don't just spin the CPU */
1794 goto again;
1795 }
1796 } else {
1797 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
1798 }
1799}
1800
993386c1 1801int
1da177e4 1802xfs_remove(
993386c1 1803 xfs_inode_t *dp,
556b8b16
BN
1804 struct xfs_name *name,
1805 xfs_inode_t *ip)
1da177e4 1806{
993386c1 1807 xfs_mount_t *mp = dp->i_mount;
1da177e4 1808 xfs_trans_t *tp = NULL;
8f112e3b 1809 int is_dir = S_ISDIR(ip->i_d.di_mode);
1da177e4
LT
1810 int error = 0;
1811 xfs_bmap_free_t free_list;
1812 xfs_fsblock_t first_block;
1813 int cancel_flags;
1814 int committed;
1da177e4
LT
1815 int link_zero;
1816 uint resblks;
8f112e3b 1817 uint log_count;
1da177e4 1818
cf441eeb 1819 xfs_itrace_entry(dp);
8f112e3b 1820 xfs_itrace_entry(ip);
1da177e4 1821
1da177e4
LT
1822 if (XFS_FORCED_SHUTDOWN(mp))
1823 return XFS_ERROR(EIO);
1824
eb9df39d 1825 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
556b8b16
BN
1826 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL,
1827 NULL, DM_RIGHT_NULL, name->name, NULL,
1828 ip->i_d.di_mode, 0, 0);
1da177e4
LT
1829 if (error)
1830 return error;
1831 }
1832
1da177e4 1833 error = XFS_QM_DQATTACH(mp, dp, 0);
8f112e3b
CH
1834 if (error)
1835 goto std_return;
1836
1837 error = XFS_QM_DQATTACH(mp, ip, 0);
1838 if (error)
1da177e4 1839 goto std_return;
1da177e4 1840
8f112e3b
CH
1841 if (is_dir) {
1842 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
1843 log_count = XFS_DEFAULT_LOG_COUNT;
1844 } else {
1845 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
1846 log_count = XFS_REMOVE_LOG_COUNT;
1847 }
1da177e4 1848 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
8f112e3b 1849
1da177e4
LT
1850 /*
1851 * We try to get the real space reservation first,
1852 * allowing for directory btree deletion(s) implying
1853 * possible bmap insert(s). If we can't get the space
1854 * reservation then we use 0 instead, and avoid the bmap
1855 * btree insert(s) in the directory code by, if the bmap
1856 * insert tries to happen, instead trimming the LAST
1857 * block from the directory.
1858 */
1859 resblks = XFS_REMOVE_SPACE_RES(mp);
1860 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
8f112e3b 1861 XFS_TRANS_PERM_LOG_RES, log_count);
1da177e4
LT
1862 if (error == ENOSPC) {
1863 resblks = 0;
1864 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
8f112e3b 1865 XFS_TRANS_PERM_LOG_RES, log_count);
1da177e4
LT
1866 }
1867 if (error) {
1868 ASSERT(error != ENOSPC);
8f112e3b
CH
1869 cancel_flags = 0;
1870 goto out_trans_cancel;
1da177e4
LT
1871 }
1872
e1cccd91 1873 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
1da177e4
LT
1874
1875 /*
1876 * At this point, we've gotten both the directory and the entry
1877 * inodes locked.
1878 */
5df78e73 1879 IHOLD(ip);
1da177e4 1880 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
82dab941
CH
1881
1882 IHOLD(dp);
1883 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1da177e4 1884
8f112e3b
CH
1885 /*
1886 * If we're removing a directory perform some additional validation.
1887 */
1888 if (is_dir) {
1889 ASSERT(ip->i_d.di_nlink >= 2);
1890 if (ip->i_d.di_nlink != 2) {
1891 error = XFS_ERROR(ENOTEMPTY);
1892 goto out_trans_cancel;
1893 }
1894 if (!xfs_dir_isempty(ip)) {
1895 error = XFS_ERROR(ENOTEMPTY);
1896 goto out_trans_cancel;
1897 }
1898 }
1899
9d87c319 1900 xfs_bmap_init(&free_list, &first_block);
556b8b16 1901 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
d4377d84 1902 &first_block, &free_list, resblks);
1da177e4
LT
1903 if (error) {
1904 ASSERT(error != ENOENT);
8f112e3b 1905 goto out_bmap_cancel;
1da177e4
LT
1906 }
1907 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1908
8f112e3b
CH
1909 if (is_dir) {
1910 /*
1911 * Drop the link from ip's "..".
1912 */
1913 error = xfs_droplink(tp, dp);
1914 if (error)
1915 goto out_bmap_cancel;
1916
1917 /*
2b7035fd 1918 * Drop the "." link from ip to self.
8f112e3b
CH
1919 */
1920 error = xfs_droplink(tp, ip);
1921 if (error)
1922 goto out_bmap_cancel;
1923 } else {
1924 /*
1925 * When removing a non-directory we need to log the parent
26c52951
DC
1926 * inode here. For a directory this is done implicitly
1927 * by the xfs_droplink call for the ".." entry.
8f112e3b
CH
1928 */
1929 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1da177e4
LT
1930 }
1931
8f112e3b 1932 /*
2b7035fd 1933 * Drop the link from dp to ip.
8f112e3b
CH
1934 */
1935 error = xfs_droplink(tp, ip);
1936 if (error)
1937 goto out_bmap_cancel;
1938
1939 /*
1940 * Determine if this is the last link while
1da177e4
LT
1941 * we are in the transaction.
1942 */
8f112e3b 1943 link_zero = (ip->i_d.di_nlink == 0);
1da177e4 1944
1da177e4
LT
1945 /*
1946 * If this is a synchronous mount, make sure that the
1947 * remove transaction goes to disk before returning to
1948 * the user.
1949 */
8f112e3b 1950 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1da177e4 1951 xfs_trans_set_sync(tp);
1da177e4 1952
f7c99b6f 1953 error = xfs_bmap_finish(&tp, &free_list, &committed);
8f112e3b
CH
1954 if (error)
1955 goto out_bmap_cancel;
1da177e4 1956
1c72bf90 1957 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
5df78e73 1958 if (error)
1da177e4 1959 goto std_return;
1da177e4 1960
2a82b8be
DC
1961 /*
1962 * If we are using filestreams, kill the stream association.
1963 * If the file is still open it may get a new one but that
1964 * will get killed on last close in xfs_close() so we don't
1965 * have to worry about that.
1966 */
8f112e3b 1967 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
2a82b8be
DC
1968 xfs_filestream_deassociate(ip);
1969
cf441eeb 1970 xfs_itrace_exit(ip);
8f112e3b 1971 xfs_itrace_exit(dp);
1da177e4 1972
1da177e4 1973 std_return:
eb9df39d 1974 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
8f112e3b
CH
1975 XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE, dp, DM_RIGHT_NULL,
1976 NULL, DM_RIGHT_NULL, name->name, NULL,
1977 ip->i_d.di_mode, error, 0);
1da177e4 1978 }
8f112e3b 1979
1da177e4
LT
1980 return error;
1981
8f112e3b 1982 out_bmap_cancel:
1da177e4
LT
1983 xfs_bmap_cancel(&free_list);
1984 cancel_flags |= XFS_TRANS_ABORT;
8f112e3b 1985 out_trans_cancel:
1da177e4
LT
1986 xfs_trans_cancel(tp, cancel_flags);
1987 goto std_return;
1da177e4
LT
1988}
1989
993386c1 1990int
1da177e4 1991xfs_link(
993386c1 1992 xfs_inode_t *tdp,
a3da7896 1993 xfs_inode_t *sip,
556b8b16 1994 struct xfs_name *target_name)
1da177e4 1995{
993386c1 1996 xfs_mount_t *mp = tdp->i_mount;
1da177e4 1997 xfs_trans_t *tp;
1da177e4
LT
1998 int error;
1999 xfs_bmap_free_t free_list;
2000 xfs_fsblock_t first_block;
2001 int cancel_flags;
2002 int committed;
1da177e4 2003 int resblks;
1da177e4 2004
cf441eeb 2005 xfs_itrace_entry(tdp);
a3da7896 2006 xfs_itrace_entry(sip);
1da177e4 2007
a3da7896 2008 ASSERT(!S_ISDIR(sip->i_d.di_mode));
1da177e4 2009
1da177e4
LT
2010 if (XFS_FORCED_SHUTDOWN(mp))
2011 return XFS_ERROR(EIO);
2012
eb9df39d 2013 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
1da177e4 2014 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
bc4ac74a
CH
2015 tdp, DM_RIGHT_NULL,
2016 sip, DM_RIGHT_NULL,
556b8b16 2017 target_name->name, NULL, 0, 0, 0);
1da177e4
LT
2018 if (error)
2019 return error;
2020 }
2021
2022 /* Return through std_return after this point. */
2023
2024 error = XFS_QM_DQATTACH(mp, sip, 0);
cb3f35bb
CH
2025 if (error)
2026 goto std_return;
2027
2028 error = XFS_QM_DQATTACH(mp, tdp, 0);
1da177e4
LT
2029 if (error)
2030 goto std_return;
2031
2032 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2033 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
556b8b16 2034 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
1da177e4
LT
2035 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2036 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2037 if (error == ENOSPC) {
2038 resblks = 0;
2039 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2040 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2041 }
2042 if (error) {
2043 cancel_flags = 0;
2044 goto error_return;
2045 }
2046
e1cccd91 2047 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
1da177e4
LT
2048
2049 /*
2050 * Increment vnode ref counts since xfs_trans_commit &
2051 * xfs_trans_cancel will both unlock the inodes and
2052 * decrement the associated ref counts.
2053 */
a3da7896
CH
2054 IHOLD(sip);
2055 IHOLD(tdp);
1da177e4
LT
2056 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2057 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2058
2059 /*
2060 * If the source has too many links, we can't make any more to it.
2061 */
2062 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2063 error = XFS_ERROR(EMLINK);
2064 goto error_return;
2065 }
2066
365ca83d
NS
2067 /*
2068 * If we are using project inheritance, we only allow hard link
2069 * creation in our tree when the project IDs are the same; else
2070 * the tree quota mechanism could be circumvented.
2071 */
2072 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2073 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
b1ecdda9 2074 error = XFS_ERROR(EXDEV);
365ca83d
NS
2075 goto error_return;
2076 }
2077
556b8b16
BN
2078 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
2079 if (error)
1da177e4
LT
2080 goto error_return;
2081
9d87c319 2082 xfs_bmap_init(&free_list, &first_block);
1da177e4 2083
556b8b16
BN
2084 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
2085 &first_block, &free_list, resblks);
1da177e4
LT
2086 if (error)
2087 goto abort_return;
2088 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1da177e4
LT
2089 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2090
2091 error = xfs_bumplink(tp, sip);
b71d300c 2092 if (error)
1da177e4 2093 goto abort_return;
1da177e4
LT
2094
2095 /*
2096 * If this is a synchronous mount, make sure that the
2097 * link transaction goes to disk before returning to
2098 * the user.
2099 */
2100 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2101 xfs_trans_set_sync(tp);
2102 }
2103
f7c99b6f 2104 error = xfs_bmap_finish (&tp, &free_list, &committed);
1da177e4
LT
2105 if (error) {
2106 xfs_bmap_cancel(&free_list);
2107 goto abort_return;
2108 }
2109
1c72bf90 2110 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
b71d300c 2111 if (error)
1da177e4 2112 goto std_return;
1da177e4
LT
2113
2114 /* Fall through to std_return with error = 0. */
2115std_return:
eb9df39d 2116 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
1da177e4 2117 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
bc4ac74a
CH
2118 tdp, DM_RIGHT_NULL,
2119 sip, DM_RIGHT_NULL,
556b8b16 2120 target_name->name, NULL, 0, error, 0);
1da177e4
LT
2121 }
2122 return error;
2123
2124 abort_return:
2125 cancel_flags |= XFS_TRANS_ABORT;
2126 /* FALLTHROUGH */
014c2544 2127
1da177e4
LT
2128 error_return:
2129 xfs_trans_cancel(tp, cancel_flags);
1da177e4
LT
2130 goto std_return;
2131}
b71d300c 2132
993386c1 2133int
1da177e4 2134xfs_symlink(
993386c1 2135 xfs_inode_t *dp,
556b8b16
BN
2136 struct xfs_name *link_name,
2137 const char *target_path,
3e5daf05 2138 mode_t mode,
3937be5b 2139 xfs_inode_t **ipp,
1da177e4
LT
2140 cred_t *credp)
2141{
993386c1 2142 xfs_mount_t *mp = dp->i_mount;
1da177e4 2143 xfs_trans_t *tp;
1da177e4
LT
2144 xfs_inode_t *ip;
2145 int error;
2146 int pathlen;
2147 xfs_bmap_free_t free_list;
2148 xfs_fsblock_t first_block;
993386c1 2149 boolean_t unlock_dp_on_error = B_FALSE;
1da177e4
LT
2150 uint cancel_flags;
2151 int committed;
2152 xfs_fileoff_t first_fsb;
2153 xfs_filblks_t fs_blocks;
2154 int nmaps;
2155 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
2156 xfs_daddr_t d;
556b8b16 2157 const char *cur_chunk;
1da177e4
LT
2158 int byte_cnt;
2159 int n;
2160 xfs_buf_t *bp;
2161 xfs_prid_t prid;
2162 struct xfs_dquot *udqp, *gdqp;
2163 uint resblks;
1da177e4 2164
3937be5b 2165 *ipp = NULL;
1da177e4
LT
2166 error = 0;
2167 ip = NULL;
2168 tp = NULL;
2169
cf441eeb 2170 xfs_itrace_entry(dp);
1da177e4
LT
2171
2172 if (XFS_FORCED_SHUTDOWN(mp))
2173 return XFS_ERROR(EIO);
2174
1da177e4
LT
2175 /*
2176 * Check component lengths of the target path name.
2177 */
2178 pathlen = strlen(target_path);
2179 if (pathlen >= MAXPATHLEN) /* total string too long */
2180 return XFS_ERROR(ENAMETOOLONG);
1da177e4 2181
eb9df39d 2182 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
bc4ac74a 2183 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp,
1da177e4 2184 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
556b8b16 2185 link_name->name, target_path, 0, 0, 0);
1da177e4
LT
2186 if (error)
2187 return error;
2188 }
2189
2190 /* Return through std_return after this point. */
2191
2192 udqp = gdqp = NULL;
365ca83d
NS
2193 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2194 prid = dp->i_d.di_projid;
1da177e4
LT
2195 else
2196 prid = (xfs_prid_t)dfltprid;
2197
2198 /*
2199 * Make sure that we have allocated dquot(s) on disk.
2200 */
2201 error = XFS_QM_DQVOPALLOC(mp, dp,
9e2b2dc4 2202 current_fsuid(), current_fsgid(), prid,
1da177e4
LT
2203 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2204 if (error)
2205 goto std_return;
2206
2207 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
2208 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2209 /*
2210 * The symlink will fit into the inode data fork?
2211 * There can't be any attributes so we get the whole variable part.
2212 */
2213 if (pathlen <= XFS_LITINO(mp))
2214 fs_blocks = 0;
2215 else
2216 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
556b8b16 2217 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
1da177e4
LT
2218 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
2219 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2220 if (error == ENOSPC && fs_blocks == 0) {
2221 resblks = 0;
2222 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
2223 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2224 }
2225 if (error) {
2226 cancel_flags = 0;
1da177e4
LT
2227 goto error_return;
2228 }
2229
f7c66ce3 2230 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
993386c1 2231 unlock_dp_on_error = B_TRUE;
1da177e4
LT
2232
2233 /*
2234 * Check whether the directory allows new symlinks or not.
2235 */
2236 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
2237 error = XFS_ERROR(EPERM);
2238 goto error_return;
2239 }
2240
2241 /*
2242 * Reserve disk quota : blocks and inode.
2243 */
2244 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2245 if (error)
2246 goto error_return;
2247
2248 /*
2249 * Check for ability to enter directory entry, if no space reserved.
2250 */
556b8b16
BN
2251 error = xfs_dir_canenter(tp, dp, link_name, resblks);
2252 if (error)
1da177e4
LT
2253 goto error_return;
2254 /*
2255 * Initialize the bmap freelist prior to calling either
2256 * bmapi or the directory create code.
2257 */
9d87c319 2258 xfs_bmap_init(&free_list, &first_block);
1da177e4
LT
2259
2260 /*
2261 * Allocate an inode for the symlink.
2262 */
3e5daf05 2263 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
1da177e4
LT
2264 1, 0, credp, prid, resblks > 0, &ip, NULL);
2265 if (error) {
2266 if (error == ENOSPC)
2267 goto error_return;
2268 goto error1;
2269 }
cf441eeb 2270 xfs_itrace_ref(ip);
1da177e4 2271
993386c1
CH
2272 /*
2273 * An error after we've joined dp to the transaction will result in the
2274 * transaction cancel unlocking dp so don't do it explicitly in the
2275 * error path.
2276 */
3937be5b 2277 IHOLD(dp);
1da177e4 2278 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
993386c1 2279 unlock_dp_on_error = B_FALSE;
1da177e4
LT
2280
2281 /*
2282 * Also attach the dquot(s) to it, if applicable.
2283 */
2284 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
2285
2286 if (resblks)
2287 resblks -= XFS_IALLOC_SPACE_RES(mp);
2288 /*
2289 * If the symlink will fit into the inode, write it inline.
2290 */
2291 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
2292 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
2293 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
2294 ip->i_d.di_size = pathlen;
2295
2296 /*
2297 * The inode was initially created in extent format.
2298 */
2299 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
2300 ip->i_df.if_flags |= XFS_IFINLINE;
2301
2302 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
2303 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
2304
2305 } else {
2306 first_fsb = 0;
2307 nmaps = SYMLINK_MAPS;
2308
2309 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
2310 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
2311 &first_block, resblks, mval, &nmaps,
3e57ecf6 2312 &free_list, NULL);
1da177e4
LT
2313 if (error) {
2314 goto error1;
2315 }
2316
2317 if (resblks)
2318 resblks -= fs_blocks;
2319 ip->i_d.di_size = pathlen;
2320 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2321
2322 cur_chunk = target_path;
2323 for (n = 0; n < nmaps; n++) {
2324 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
2325 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
2326 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
2327 BTOBB(byte_cnt), 0);
2328 ASSERT(bp && !XFS_BUF_GETERROR(bp));
2329 if (pathlen < byte_cnt) {
2330 byte_cnt = pathlen;
2331 }
2332 pathlen -= byte_cnt;
2333
2334 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
2335 cur_chunk += byte_cnt;
2336
2337 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
2338 }
2339 }
2340
2341 /*
2342 * Create the directory entry for the symlink.
2343 */
556b8b16
BN
2344 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
2345 &first_block, &free_list, resblks);
f6c2d1fa 2346 if (error)
1da177e4 2347 goto error1;
1da177e4
LT
2348 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2349 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2350
1da177e4
LT
2351 /*
2352 * If this is a synchronous mount, make sure that the
2353 * symlink transaction goes to disk before returning to
2354 * the user.
2355 */
2356 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2357 xfs_trans_set_sync(tp);
2358 }
2359
2360 /*
2361 * xfs_trans_commit normally decrements the vnode ref count
2362 * when it unlocks the inode. Since we want to return the
2363 * vnode to the caller, we bump the vnode ref count now.
2364 */
2365 IHOLD(ip);
2366
f7c99b6f 2367 error = xfs_bmap_finish(&tp, &free_list, &committed);
1da177e4
LT
2368 if (error) {
2369 goto error2;
2370 }
1c72bf90 2371 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1da177e4
LT
2372 XFS_QM_DQRELE(mp, udqp);
2373 XFS_QM_DQRELE(mp, gdqp);
2374
2375 /* Fall through to std_return with error = 0 or errno from
2376 * xfs_trans_commit */
2377std_return:
993386c1 2378 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
1da177e4 2379 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
bc4ac74a
CH
2380 dp, DM_RIGHT_NULL,
2381 error ? NULL : ip,
556b8b16
BN
2382 DM_RIGHT_NULL, link_name->name,
2383 target_path, 0, error, 0);
1da177e4
LT
2384 }
2385
3937be5b
CH
2386 if (!error)
2387 *ipp = ip;
1da177e4
LT
2388 return error;
2389
2390 error2:
2391 IRELE(ip);
2392 error1:
2393 xfs_bmap_cancel(&free_list);
2394 cancel_flags |= XFS_TRANS_ABORT;
2395 error_return:
2396 xfs_trans_cancel(tp, cancel_flags);
2397 XFS_QM_DQRELE(mp, udqp);
2398 XFS_QM_DQRELE(mp, gdqp);
2399
993386c1 2400 if (unlock_dp_on_error)
1da177e4 2401 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1da177e4
LT
2402
2403 goto std_return;
2404}
2405
1da177e4 2406int
993386c1
CH
2407xfs_set_dmattrs(
2408 xfs_inode_t *ip,
1da177e4 2409 u_int evmask,
993386c1 2410 u_int16_t state)
1da177e4 2411{
993386c1 2412 xfs_mount_t *mp = ip->i_mount;
1da177e4 2413 xfs_trans_t *tp;
1da177e4
LT
2414 int error;
2415
2416 if (!capable(CAP_SYS_ADMIN))
2417 return XFS_ERROR(EPERM);
2418
1da177e4
LT
2419 if (XFS_FORCED_SHUTDOWN(mp))
2420 return XFS_ERROR(EIO);
2421
2422 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
2423 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
2424 if (error) {
2425 xfs_trans_cancel(tp, 0);
2426 return error;
2427 }
2428 xfs_ilock(ip, XFS_ILOCK_EXCL);
2429 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2430
613d7043
CH
2431 ip->i_d.di_dmevmask = evmask;
2432 ip->i_d.di_dmstate = state;
1da177e4
LT
2433
2434 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2435 IHOLD(ip);
1c72bf90 2436 error = xfs_trans_commit(tp, 0);
1da177e4
LT
2437
2438 return error;
2439}
2440
993386c1 2441int
1da177e4 2442xfs_reclaim(
993386c1 2443 xfs_inode_t *ip)
1da177e4 2444{
1da177e4 2445
cf441eeb 2446 xfs_itrace_entry(ip);
1da177e4 2447
df80c933 2448 ASSERT(!VN_MAPPED(VFS_I(ip)));
1da177e4
LT
2449
2450 /* bad inode, get out here ASAP */
cb4c8cc1 2451 if (is_bad_inode(VFS_I(ip))) {
1da177e4
LT
2452 xfs_ireclaim(ip);
2453 return 0;
2454 }
2455
25e41b3d 2456 xfs_ioend_wait(ip);
1da177e4 2457
51c91ed5 2458 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
1da177e4 2459
42fe2b1f
CH
2460 /*
2461 * Make sure the atime in the XFS inode is correct before freeing the
2462 * Linux inode.
2463 */
2464 xfs_synchronize_atime(ip);
1da177e4 2465
4c60658e
DC
2466 /*
2467 * If we have nothing to flush with this inode then complete the
2468 * teardown now, otherwise break the link between the xfs inode and the
2469 * linux inode and clean up the xfs inode later. This avoids flushing
2470 * the inode to disk during the delete operation itself.
2471 *
2472 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
2473 * first to ensure that xfs_iunpin() will never see an xfs inode
2474 * that has a linux inode being reclaimed. Synchronisation is provided
2475 * by the i_flags_lock.
1da177e4
LT
2476 */
2477 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
2478 xfs_ilock(ip, XFS_ILOCK_EXCL);
2479 xfs_iflock(ip);
bf904248 2480 xfs_iflags_set(ip, XFS_IRECLAIMABLE);
1dc3318a 2481 return xfs_reclaim_inode(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
1da177e4 2482 }
11654513 2483 xfs_inode_set_reclaim_tag(ip);
1da177e4
LT
2484 return 0;
2485}
1da177e4
LT
2486
2487/*
2488 * xfs_alloc_file_space()
2489 * This routine allocates disk space for the given file.
2490 *
2491 * If alloc_type == 0, this request is for an ALLOCSP type
2492 * request which will change the file size. In this case, no
2493 * DMAPI event will be generated by the call. A TRUNCATE event
2494 * will be generated later by xfs_setattr.
2495 *
2496 * If alloc_type != 0, this request is for a RESVSP type
2497 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
2498 * lower block boundary byte address is less than the file's
2499 * length.
2500 *
2501 * RETURNS:
2502 * 0 on success
2503 * errno on error
2504 *
2505 */
ba0f32d4 2506STATIC int
1da177e4
LT
2507xfs_alloc_file_space(
2508 xfs_inode_t *ip,
2509 xfs_off_t offset,
2510 xfs_off_t len,
2511 int alloc_type,
2512 int attr_flags)
2513{
dd9f438e
NS
2514 xfs_mount_t *mp = ip->i_mount;
2515 xfs_off_t count;
1da177e4
LT
2516 xfs_filblks_t allocated_fsb;
2517 xfs_filblks_t allocatesize_fsb;
dd9f438e
NS
2518 xfs_extlen_t extsz, temp;
2519 xfs_fileoff_t startoffset_fsb;
1da177e4 2520 xfs_fsblock_t firstfsb;
dd9f438e
NS
2521 int nimaps;
2522 int bmapi_flag;
2523 int quota_flag;
1da177e4 2524 int rt;
1da177e4 2525 xfs_trans_t *tp;
dd9f438e
NS
2526 xfs_bmbt_irec_t imaps[1], *imapp;
2527 xfs_bmap_free_t free_list;
2528 uint qblocks, resblks, resrtextents;
2529 int committed;
2530 int error;
1da177e4 2531
cf441eeb 2532 xfs_itrace_entry(ip);
1da177e4
LT
2533
2534 if (XFS_FORCED_SHUTDOWN(mp))
2535 return XFS_ERROR(EIO);
2536
1da177e4
LT
2537 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
2538 return error;
2539
2540 if (len <= 0)
2541 return XFS_ERROR(EINVAL);
2542
957d0ebe
DC
2543 rt = XFS_IS_REALTIME_INODE(ip);
2544 extsz = xfs_get_extsz_hint(ip);
2545
1da177e4 2546 count = len;
1da177e4 2547 imapp = &imaps[0];
dd9f438e
NS
2548 nimaps = 1;
2549 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
1da177e4
LT
2550 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
2551 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
2552
2553 /* Generate a DMAPI event if needed. */
ba87ea69 2554 if (alloc_type != 0 && offset < ip->i_size &&
0f285c8a 2555 (attr_flags & XFS_ATTR_DMI) == 0 &&
eb9df39d 2556 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
1da177e4
LT
2557 xfs_off_t end_dmi_offset;
2558
2559 end_dmi_offset = offset+len;
ba87ea69
LM
2560 if (end_dmi_offset > ip->i_size)
2561 end_dmi_offset = ip->i_size;
bc4ac74a
CH
2562 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset,
2563 end_dmi_offset - offset, 0, NULL);
1da177e4 2564 if (error)
014c2544 2565 return error;
1da177e4
LT
2566 }
2567
2568 /*
dd9f438e 2569 * Allocate file space until done or until there is an error
1da177e4
LT
2570 */
2571retry:
2572 while (allocatesize_fsb && !error) {
dd9f438e
NS
2573 xfs_fileoff_t s, e;
2574
1da177e4 2575 /*
3ddb8fa9 2576 * Determine space reservations for data/realtime.
1da177e4 2577 */
dd9f438e 2578 if (unlikely(extsz)) {
1da177e4 2579 s = startoffset_fsb;
dd9f438e
NS
2580 do_div(s, extsz);
2581 s *= extsz;
2582 e = startoffset_fsb + allocatesize_fsb;
2583 if ((temp = do_mod(startoffset_fsb, extsz)))
2584 e += temp;
2585 if ((temp = do_mod(e, extsz)))
2586 e += extsz - temp;
2587 } else {
2588 s = 0;
2589 e = allocatesize_fsb;
2590 }
2591
2592 if (unlikely(rt)) {
2593 resrtextents = qblocks = (uint)(e - s);
2594 resrtextents /= mp->m_sb.sb_rextsize;
2595 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2596 quota_flag = XFS_QMOPT_RES_RTBLKS;
1da177e4 2597 } else {
dd9f438e
NS
2598 resrtextents = 0;
2599 resblks = qblocks = \
2600 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
2601 quota_flag = XFS_QMOPT_RES_REGBLKS;
1da177e4
LT
2602 }
2603
2604 /*
dd9f438e 2605 * Allocate and setup the transaction.
1da177e4
LT
2606 */
2607 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
dd9f438e
NS
2608 error = xfs_trans_reserve(tp, resblks,
2609 XFS_WRITE_LOG_RES(mp), resrtextents,
1da177e4
LT
2610 XFS_TRANS_PERM_LOG_RES,
2611 XFS_WRITE_LOG_COUNT);
1da177e4 2612 /*
dd9f438e 2613 * Check for running out of space
1da177e4
LT
2614 */
2615 if (error) {
2616 /*
2617 * Free the transaction structure.
2618 */
2619 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2620 xfs_trans_cancel(tp, 0);
2621 break;
2622 }
2623 xfs_ilock(ip, XFS_ILOCK_EXCL);
dd9f438e
NS
2624 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
2625 qblocks, 0, quota_flag);
1da177e4
LT
2626 if (error)
2627 goto error1;
2628
2629 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2630 xfs_trans_ihold(tp, ip);
2631
2632 /*
dd9f438e 2633 * Issue the xfs_bmapi() call to allocate the blocks
1da177e4 2634 */
9d87c319 2635 xfs_bmap_init(&free_list, &firstfsb);
541d7d3c 2636 error = xfs_bmapi(tp, ip, startoffset_fsb,
dd9f438e
NS
2637 allocatesize_fsb, bmapi_flag,
2638 &firstfsb, 0, imapp, &nimaps,
3e57ecf6 2639 &free_list, NULL);
1da177e4
LT
2640 if (error) {
2641 goto error0;
2642 }
2643
2644 /*
dd9f438e 2645 * Complete the transaction
1da177e4 2646 */
f7c99b6f 2647 error = xfs_bmap_finish(&tp, &free_list, &committed);
1da177e4
LT
2648 if (error) {
2649 goto error0;
2650 }
2651
1c72bf90 2652 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1da177e4
LT
2653 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2654 if (error) {
2655 break;
2656 }
2657
2658 allocated_fsb = imapp->br_blockcount;
2659
dd9f438e 2660 if (nimaps == 0) {
1da177e4
LT
2661 error = XFS_ERROR(ENOSPC);
2662 break;
2663 }
2664
2665 startoffset_fsb += allocated_fsb;
2666 allocatesize_fsb -= allocated_fsb;
2667 }
2668dmapi_enospc_check:
0f285c8a 2669 if (error == ENOSPC && (attr_flags & XFS_ATTR_DMI) == 0 &&
eb9df39d 2670 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
1da177e4 2671 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
bc4ac74a
CH
2672 ip, DM_RIGHT_NULL,
2673 ip, DM_RIGHT_NULL,
1da177e4
LT
2674 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
2675 if (error == 0)
2676 goto retry; /* Maybe DMAPI app. has made space */
2677 /* else fall through with error from XFS_SEND_DATA */
2678 }
2679
2680 return error;
2681
dd9f438e 2682error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
1da177e4 2683 xfs_bmap_cancel(&free_list);
dd9f438e
NS
2684 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
2685
2686error1: /* Just cancel transaction */
1da177e4
LT
2687 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2688 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2689 goto dmapi_enospc_check;
2690}
2691
2692/*
2693 * Zero file bytes between startoff and endoff inclusive.
2694 * The iolock is held exclusive and no blocks are buffered.
2fd6f6ec
LM
2695 *
2696 * This function is used by xfs_free_file_space() to zero
2697 * partial blocks when the range to free is not block aligned.
2698 * When unreserving space with boundaries that are not block
2699 * aligned we round up the start and round down the end
2700 * boundaries and then use this function to zero the parts of
2701 * the blocks that got dropped during the rounding.
1da177e4
LT
2702 */
2703STATIC int
2704xfs_zero_remaining_bytes(
2705 xfs_inode_t *ip,
2706 xfs_off_t startoff,
2707 xfs_off_t endoff)
2708{
2709 xfs_bmbt_irec_t imap;
2710 xfs_fileoff_t offset_fsb;
2711 xfs_off_t lastoffset;
2712 xfs_off_t offset;
2713 xfs_buf_t *bp;
2714 xfs_mount_t *mp = ip->i_mount;
2715 int nimap;
2716 int error = 0;
2717
2fd6f6ec
LM
2718 /*
2719 * Avoid doing I/O beyond eof - it's not necessary
2720 * since nothing can read beyond eof. The space will
2721 * be zeroed when the file is extended anyway.
2722 */
2723 if (startoff >= ip->i_size)
2724 return 0;
2725
2726 if (endoff > ip->i_size)
2727 endoff = ip->i_size;
2728
1da177e4 2729 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
71ddabb9 2730 XFS_IS_REALTIME_INODE(ip) ?
1da177e4 2731 mp->m_rtdev_targp : mp->m_ddev_targp);
c6422617
LM
2732 if (!bp)
2733 return XFS_ERROR(ENOMEM);
1da177e4
LT
2734
2735 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
2736 offset_fsb = XFS_B_TO_FSBT(mp, offset);
2737 nimap = 1;
541d7d3c 2738 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
3e57ecf6 2739 NULL, 0, &imap, &nimap, NULL, NULL);
1da177e4
LT
2740 if (error || nimap < 1)
2741 break;
2742 ASSERT(imap.br_blockcount >= 1);
2743 ASSERT(imap.br_startoff == offset_fsb);
2744 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
2745 if (lastoffset > endoff)
2746 lastoffset = endoff;
2747 if (imap.br_startblock == HOLESTARTBLOCK)
2748 continue;
2749 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2750 if (imap.br_state == XFS_EXT_UNWRITTEN)
2751 continue;
2752 XFS_BUF_UNDONE(bp);
2753 XFS_BUF_UNWRITE(bp);
2754 XFS_BUF_READ(bp);
9d87c319 2755 XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock));
1da177e4 2756 xfsbdstrat(mp, bp);
d64e31a2
DC
2757 error = xfs_iowait(bp);
2758 if (error) {
1da177e4
LT
2759 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
2760 mp, bp, XFS_BUF_ADDR(bp));
2761 break;
2762 }
2763 memset(XFS_BUF_PTR(bp) +
2764 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
2765 0, lastoffset - offset + 1);
2766 XFS_BUF_UNDONE(bp);
2767 XFS_BUF_UNREAD(bp);
2768 XFS_BUF_WRITE(bp);
2769 xfsbdstrat(mp, bp);
d64e31a2
DC
2770 error = xfs_iowait(bp);
2771 if (error) {
1da177e4
LT
2772 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
2773 mp, bp, XFS_BUF_ADDR(bp));
2774 break;
2775 }
2776 }
2777 xfs_buf_free(bp);
2778 return error;
2779}
2780
2781/*
2782 * xfs_free_file_space()
2783 * This routine frees disk space for the given file.
2784 *
2785 * This routine is only called by xfs_change_file_space
2786 * for an UNRESVSP type call.
2787 *
2788 * RETURNS:
2789 * 0 on success
2790 * errno on error
2791 *
2792 */
2793STATIC int
2794xfs_free_file_space(
2795 xfs_inode_t *ip,
2796 xfs_off_t offset,
2797 xfs_off_t len,
2798 int attr_flags)
2799{
2800 int committed;
2801 int done;
2802 xfs_off_t end_dmi_offset;
2803 xfs_fileoff_t endoffset_fsb;
2804 int error;
2805 xfs_fsblock_t firstfsb;
2806 xfs_bmap_free_t free_list;
1da177e4
LT
2807 xfs_bmbt_irec_t imap;
2808 xfs_off_t ioffset;
2809 xfs_extlen_t mod=0;
2810 xfs_mount_t *mp;
2811 int nimap;
2812 uint resblks;
673cdf5c 2813 uint rounding;
1da177e4
LT
2814 int rt;
2815 xfs_fileoff_t startoffset_fsb;
2816 xfs_trans_t *tp;
5fcbab35 2817 int need_iolock = 1;
1da177e4 2818
1da177e4
LT
2819 mp = ip->i_mount;
2820
cf441eeb 2821 xfs_itrace_entry(ip);
bd5a876a 2822
1da177e4
LT
2823 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
2824 return error;
2825
2826 error = 0;
2827 if (len <= 0) /* if nothing being freed */
2828 return error;
71ddabb9 2829 rt = XFS_IS_REALTIME_INODE(ip);
1da177e4
LT
2830 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
2831 end_dmi_offset = offset + len;
2832 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
2833
0f285c8a 2834 if (offset < ip->i_size && (attr_flags & XFS_ATTR_DMI) == 0 &&
eb9df39d 2835 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
ba87ea69
LM
2836 if (end_dmi_offset > ip->i_size)
2837 end_dmi_offset = ip->i_size;
bc4ac74a 2838 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip,
1da177e4
LT
2839 offset, end_dmi_offset - offset,
2840 AT_DELAY_FLAG(attr_flags), NULL);
2841 if (error)
014c2544 2842 return error;
1da177e4
LT
2843 }
2844
0f285c8a 2845 if (attr_flags & XFS_ATTR_NOLOCK)
5fcbab35 2846 need_iolock = 0;
9fa8046f 2847 if (need_iolock) {
1da177e4 2848 xfs_ilock(ip, XFS_IOLOCK_EXCL);
25e41b3d
CH
2849 /* wait for the completion of any pending DIOs */
2850 xfs_ioend_wait(ip);
9fa8046f 2851 }
5fcbab35 2852
e6a4b37f 2853 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
1da177e4 2854 ioffset = offset & ~(rounding - 1);
bd5a876a 2855
df80c933 2856 if (VN_CACHED(VFS_I(ip)) != 0) {
e6a4b37f
TS
2857 xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
2858 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
d3cf2094
LM
2859 if (error)
2860 goto out_unlock_iolock;
bd5a876a
CH
2861 }
2862
1da177e4
LT
2863 /*
2864 * Need to zero the stuff we're not freeing, on disk.
9da096fd 2865 * If it's a realtime file & can't use unwritten extents then we
1da177e4
LT
2866 * actually need to zero the extent edges. Otherwise xfs_bunmapi
2867 * will take care of it for us.
2868 */
62118709 2869 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
1da177e4 2870 nimap = 1;
541d7d3c 2871 error = xfs_bmapi(NULL, ip, startoffset_fsb,
3e57ecf6 2872 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
1da177e4
LT
2873 if (error)
2874 goto out_unlock_iolock;
2875 ASSERT(nimap == 0 || nimap == 1);
2876 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2877 xfs_daddr_t block;
2878
2879 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2880 block = imap.br_startblock;
2881 mod = do_div(block, mp->m_sb.sb_rextsize);
2882 if (mod)
2883 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
2884 }
2885 nimap = 1;
541d7d3c 2886 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
3e57ecf6 2887 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
1da177e4
LT
2888 if (error)
2889 goto out_unlock_iolock;
2890 ASSERT(nimap == 0 || nimap == 1);
2891 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2892 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2893 mod++;
2894 if (mod && (mod != mp->m_sb.sb_rextsize))
2895 endoffset_fsb -= mod;
2896 }
2897 }
2898 if ((done = (endoffset_fsb <= startoffset_fsb)))
2899 /*
2900 * One contiguous piece to clear
2901 */
2902 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
2903 else {
2904 /*
2905 * Some full blocks, possibly two pieces to clear
2906 */
2907 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
2908 error = xfs_zero_remaining_bytes(ip, offset,
2909 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
2910 if (!error &&
2911 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
2912 error = xfs_zero_remaining_bytes(ip,
2913 XFS_FSB_TO_B(mp, endoffset_fsb),
2914 offset + len - 1);
2915 }
2916
2917 /*
2918 * free file space until done or until there is an error
2919 */
2920 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2921 while (!error && !done) {
2922
2923 /*
91ebecc7
DC
2924 * allocate and setup the transaction. Allow this
2925 * transaction to dip into the reserve blocks to ensure
2926 * the freeing of the space succeeds at ENOSPC.
1da177e4
LT
2927 */
2928 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
91ebecc7 2929 tp->t_flags |= XFS_TRANS_RESERVE;
1da177e4
LT
2930 error = xfs_trans_reserve(tp,
2931 resblks,
2932 XFS_WRITE_LOG_RES(mp),
2933 0,
2934 XFS_TRANS_PERM_LOG_RES,
2935 XFS_WRITE_LOG_COUNT);
2936
2937 /*
2938 * check for running out of space
2939 */
2940 if (error) {
2941 /*
2942 * Free the transaction structure.
2943 */
2944 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2945 xfs_trans_cancel(tp, 0);
2946 break;
2947 }
2948 xfs_ilock(ip, XFS_ILOCK_EXCL);
2949 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
dd9f438e
NS
2950 ip->i_udquot, ip->i_gdquot, resblks, 0,
2951 XFS_QMOPT_RES_REGBLKS);
1da177e4
LT
2952 if (error)
2953 goto error1;
2954
2955 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2956 xfs_trans_ihold(tp, ip);
2957
2958 /*
2959 * issue the bunmapi() call to free the blocks
2960 */
9d87c319 2961 xfs_bmap_init(&free_list, &firstfsb);
541d7d3c 2962 error = xfs_bunmapi(tp, ip, startoffset_fsb,
1da177e4 2963 endoffset_fsb - startoffset_fsb,
3e57ecf6 2964 0, 2, &firstfsb, &free_list, NULL, &done);
1da177e4
LT
2965 if (error) {
2966 goto error0;
2967 }
2968
2969 /*
2970 * complete the transaction
2971 */
f7c99b6f 2972 error = xfs_bmap_finish(&tp, &free_list, &committed);
1da177e4
LT
2973 if (error) {
2974 goto error0;
2975 }
2976
1c72bf90 2977 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1da177e4
LT
2978 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2979 }
2980
2981 out_unlock_iolock:
2982 if (need_iolock)
2983 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
2984 return error;
2985
2986 error0:
2987 xfs_bmap_cancel(&free_list);
2988 error1:
2989 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2990 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
2991 XFS_ILOCK_EXCL);
2992 return error;
2993}
2994
2995/*
2996 * xfs_change_file_space()
2997 * This routine allocates or frees disk space for the given file.
2998 * The user specified parameters are checked for alignment and size
2999 * limitations.
3000 *
3001 * RETURNS:
3002 * 0 on success
3003 * errno on error
3004 *
3005 */
3006int
3007xfs_change_file_space(
993386c1 3008 xfs_inode_t *ip,
1da177e4
LT
3009 int cmd,
3010 xfs_flock64_t *bf,
3011 xfs_off_t offset,
1da177e4
LT
3012 int attr_flags)
3013{
993386c1 3014 xfs_mount_t *mp = ip->i_mount;
1da177e4
LT
3015 int clrprealloc;
3016 int error;
3017 xfs_fsize_t fsize;
1da177e4
LT
3018 int setprealloc;
3019 xfs_off_t startoffset;
3020 xfs_off_t llen;
3021 xfs_trans_t *tp;
0f285c8a 3022 struct iattr iattr;
1da177e4 3023
cf441eeb 3024 xfs_itrace_entry(ip);
1da177e4 3025
993386c1 3026 if (!S_ISREG(ip->i_d.di_mode))
1da177e4
LT
3027 return XFS_ERROR(EINVAL);
3028
1da177e4
LT
3029 switch (bf->l_whence) {
3030 case 0: /*SEEK_SET*/
3031 break;
3032 case 1: /*SEEK_CUR*/
3033 bf->l_start += offset;
3034 break;
3035 case 2: /*SEEK_END*/
ba87ea69 3036 bf->l_start += ip->i_size;
1da177e4
LT
3037 break;
3038 default:
3039 return XFS_ERROR(EINVAL);
3040 }
3041
3042 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
3043
3044 if ( (bf->l_start < 0)
3045 || (bf->l_start > XFS_MAXIOFFSET(mp))
3046 || (bf->l_start + llen < 0)
3047 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
3048 return XFS_ERROR(EINVAL);
3049
3050 bf->l_whence = 0;
3051
3052 startoffset = bf->l_start;
ba87ea69 3053 fsize = ip->i_size;
1da177e4
LT
3054
3055 /*
3056 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
3057 * file space.
3058 * These calls do NOT zero the data space allocated to the file,
3059 * nor do they change the file size.
3060 *
3061 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
3062 * space.
3063 * These calls cause the new file data to be zeroed and the file
3064 * size to be changed.
3065 */
3066 setprealloc = clrprealloc = 0;
3067
3068 switch (cmd) {
3069 case XFS_IOC_RESVSP:
3070 case XFS_IOC_RESVSP64:
3071 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
3072 1, attr_flags);
3073 if (error)
3074 return error;
3075 setprealloc = 1;
3076 break;
3077
3078 case XFS_IOC_UNRESVSP:
3079 case XFS_IOC_UNRESVSP64:
3080 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
3081 attr_flags)))
3082 return error;
3083 break;
3084
3085 case XFS_IOC_ALLOCSP:
3086 case XFS_IOC_ALLOCSP64:
3087 case XFS_IOC_FREESP:
3088 case XFS_IOC_FREESP64:
3089 if (startoffset > fsize) {
3090 error = xfs_alloc_file_space(ip, fsize,
3091 startoffset - fsize, 0, attr_flags);
3092 if (error)
3093 break;
3094 }
3095
0f285c8a
CH
3096 iattr.ia_valid = ATTR_SIZE;
3097 iattr.ia_size = startoffset;
1da177e4 3098
ea5a3dc8 3099 error = xfs_setattr(ip, &iattr, attr_flags);
1da177e4
LT
3100
3101 if (error)
3102 return error;
3103
3104 clrprealloc = 1;
3105 break;
3106
3107 default:
3108 ASSERT(0);
3109 return XFS_ERROR(EINVAL);
3110 }
3111
3112 /*
3113 * update the inode timestamp, mode, and prealloc flag bits
3114 */
3115 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
3116
3117 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
3118 0, 0, 0))) {
3119 /* ASSERT(0); */
3120 xfs_trans_cancel(tp, 0);
3121 return error;
3122 }
3123
3124 xfs_ilock(ip, XFS_ILOCK_EXCL);
3125
3126 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3127 xfs_trans_ihold(tp, ip);
3128
0f285c8a 3129 if ((attr_flags & XFS_ATTR_DMI) == 0) {
1da177e4
LT
3130 ip->i_d.di_mode &= ~S_ISUID;
3131
3132 /*
3133 * Note that we don't have to worry about mandatory
3134 * file locking being disabled here because we only
3135 * clear the S_ISGID bit if the Group execute bit is
3136 * on, but if it was on then mandatory locking wouldn't
3137 * have been enabled.
3138 */
3139 if (ip->i_d.di_mode & S_IXGRP)
3140 ip->i_d.di_mode &= ~S_ISGID;
3141
3142 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3143 }
3144 if (setprealloc)
3145 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
3146 else if (clrprealloc)
3147 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
3148
3149 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3150 xfs_trans_set_sync(tp);
3151
1c72bf90 3152 error = xfs_trans_commit(tp, 0);
1da177e4
LT
3153
3154 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3155
3156 return error;
3157}